Presentation is loading. Please wait.

Presentation is loading. Please wait.

XSLT Introduction.

Similar presentations


Presentation on theme: "XSLT Introduction."— Presentation transcript:

1 XSLT Introduction

2 XSLT XSL (eXtensible Stylesheet Language) is a styling language for XML. XSLT stands for XSL Transformations. This tutorial will teach you how to use XSLT to transform XML documents into other formats (like transforming XML into HTML).

3

4 <. xml version="1. 0". > <xsl:stylesheet version="1
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>     <h2>My CD Collection</h2>     <table border="1">       <tr bgcolor="#9acd32">         <th>Title</th>         <th>Artist</th>       </tr>       <xsl:for-each select="catalog/cd">         <tr>           <td><xsl:value-of select="title"/></td>           <td><xsl:value-of select="artist"/></td>         </tr>       </xsl:for-each>     </table>   </body>   </html> </xsl:template> </xsl:stylesheet> XSLT Example

5 XSLT Elements Reference
Description apply-imports Applies a template rule from an imported style sheet apply-templates Applies a template rule to the current element or to the current element's child nodes attribute Adds an attribute attribute-set Defines a named set of attributes call-template Calls a named template choose Used in conjunction with <when> and <otherwise> to express multiple conditional tests comment Creates a comment node in the result tree

6 Element Description copy Creates a copy of the current node (without child nodes and attributes) copy-of Creates a copy of the current node (with child nodes and attributes) decimal-format Defines the characters and symbols to be used when converting numbers into strings, with the format-number() function element Creates an element node in the output document fallback Specifies an alternate code to run if the processor does not support an XSLT element for-each Loops through each node in a specified node set if Contains a template that will be applied only if a specified condition is true import Imports the contents of one style sheet into another. Note: An imported style sheet has lower precedence than the importing style sheet include Includes the contents of one style sheet into another. Note: An included style sheet has the same precedence as the including style sheet

7 Element Description key Declares a named key that can be used in the style sheet with the key() function message Writes a message to the output (used to report errors) namespace-alias Replaces a namespace in the style sheet to a different namespace in the output number Determines the integer position of the current node and formats a number otherwise Specifies a default action for the <choose> element output Defines the format of the output document param Declares a local or global parameter preserve-space Defines the elements for which white space should be preserved processing-instruction Writes a processing instruction to the output sort Sorts the output strip-space Defines the elements for which white space should be removed

8 Element Description stylesheet Defines the root element of a style sheet template Rules to apply when a specified node is matched text Writes literal text to the output transform value-of Extracts the value of a selected node variable Declares a local or global variable when Specifies an action for the <choose> element with-param Defines the value of a parameter to be passed into a template

9 XSL(T) Languages XSLT is a language for transforming XML documents.
XPath is a language for navigating in XML documents. XQuery is a language for querying XML documents. What is XSLT? XSLT stands for XSL Transformations XSLT is the most important part of XSL XSLT transforms an XML document into another XML document XSLT uses XPath to navigate in XML documents XSLT is a W3C Recommendation

10 Create an XSL Style Sheet
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Create an XSL Style Sheet "cdcatalog.xsl"

11 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog> Link the XSL Style Sheet to the XML Document "cdcatalog.xml" "cdcatalog.xsl"

12 RESULT

13 XSLT <xsl:template> Element
An XSL style sheet consists of one or more set of rules that are called templates. A template contains rules to apply when a specified node is matched. The <xsl:template> Element The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

14 Create an XSL Style Sheet
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <tr>       <td>.</td>       <td>.</td>     </tr>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Create an XSL Style Sheet "cdcatalog.xsl"

15 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <catalog> <cd> <title>Empire Burlesque</title> <artist>Bob Dylan</artist> <country>USA</country> <company>Columbia</company> <price>10.90</price> <year>1985</year> </cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> <title>Still got the blues</title> <artist>Gary Moore</artist> <country>UK</country> <company>Virgin records</company> <price>10.20</price> <year>1990</year> <title>Eros</title> <artist>Eros Ramazzotti</artist> <country>EU</country> <company>BMG</company> <year>1997</year>

16 <cd> <title>One night only</title> <artist>Bee Gees</artist> <country>UK</country> <company>Polydor</company> <price>10.90</price> <year>1998</year> </cd> <title>Sylvias Mother</title> <artist>Dr.Hook</artist> <company>CBS</company> <price>8.10</price> <year>1973</year> <cd> <title>Maggie May</title> <artist>Rod Stewart</artist> <country>UK</country> <company>Pickwick</company> <price>8.50</price> <year>1990</year> </cd> <title>Romanza</title> <artist>Andrea Bocelli</artist> <country>EU</country> <company>Polydor</company> <price>10.80</price> <year>1996</year> <title>When a man loves a woman</title> <artist>Percy Sledge</artist> <country>USA</country> <company>Atlantic</company> <price>8.70</price> <year>1987</year>

17 <cd> <title>Black angel</title> <artist>Savage Rose</artist> <country>EU</country> <company>Mega</company> <price>10.90</price> <year>1995</year> </cd> <title>1999 Grammy Nominees</title> <artist>Many</artist> <country>USA</country> <company>Grammy</company> <price>10.20</price> <year>1999</year> <title>For the good times</title> <artist>Kenny Rogers</artist> <country>UK</country> <company>Mucik Master</company> <price>8.70</price> <cd> <title>Big Willie style</title> <artist>Will Smith</artist> <country>USA</country> <company>Columbia</company> <price>9.90</price> <year>1997</year> </cd> <title>Tupelo Honey</title> <artist>Van Morrison</artist> <country>UK</country> <company>Polydor</company> <price>8.20</price> <year>1971</year>

18 <title>Tupelo Honey</title>
<cd> <title>Soulsville</title> <artist>Jorn Hoel</artist> <country>Norway</country> <company>WEA</company> <price>7.90</price> <year>1996</year> </cd> <title>The very best of</title> <artist>Cat Stevens</artist> <country>UK</country> <company>Island</company> <price>8.90</price> <year>1990</year> <cd> <title>Big Willie style</title> <artist>Will Smith</artist> <country>USA</country> <company>Columbia</company> <price>9.90</price> <year>1997</year> </cd> <title>Tupelo Honey</title> <artist>Van Morrison</artist> <country>UK</country> <company>Polydor</company> <price>8.20</price> <year>1971</year>

19 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>.</td> </table> </body> </html> </xsl:template> </xsl:stylesheet> RESULT

20 XSLT Example Explained
Since an XSL style sheet is an XML document, it always begins with the XML declaration: <?xml version="1.0" encoding="UTF-8"?>. The next element, <xsl:stylesheet>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes). The <xsl:template> element defines a template. The match="/" attribute associates the template with the root of the XML source document. The content inside the <xsl:template> element defines some HTML to write to the output. The last two lines define the end of the template and the end of the style sheet. The result from this example was a little disappointing, because no data was copied from the XML document to the output. In the next chapter you will learn how to use the <xsl:value-of> element to select values from the XML elements.

21 XSLT <xsl:value-of> Element
The <xsl:value-of> element is used to extract the value of a selected node. The <xsl:value-of> Element The <xsl:value-of> element can be used to extract the value of an XML element and add it to the output stream of the transformation:

22 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <tr>       <td><xsl:value-of select="catalog/cd/title"/></td>       <td><xsl:value-of select="catalog/cd/artist"/></td>     </tr>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

23 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td><xsl:value-of select="catalog/cd/title"/></td> <td><xsl:value-of select="catalog/cd/artist"/></td> </table> </body> </html> </xsl:template> </xsl:stylesheet> RESULT

24 XSLT <xsl:value-of> Element
Example Explained Note: The select attribute, in the example above, contains an XPath expression. An XPath expression works like navigating a file system; a forward slash (/) selects subdirectories. The result from the example above was a little disappointing; only one line of data was copied from the XML document to the output. In the next chapter you will learn how to use the <xsl:for-each> element to loop through the XML elements, and display

25 XSLT <xsl:for-each> Element
The <xsl:for-each> element allows you to do looping in XSLT. The <xsl:for-each> Element The XSL <xsl:for-each> element can be used to select every XML element of a specified node-set: Note: The value of the select attribute is an XPath expression. An XPath expression works like navigating a file system; where a forward slash (/) selects subdirectories.

26 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

27 RESULT

28 XSLT <xsl:for-each> Element
Filtering the Output We can also filter the output from the XML file by adding a criterion to the select attribute in the <xsl:for-each> element. <xsl:for-each select="catalog/cd[artist='Bob Dylan']"> Legal filter operators are: =  (equal) != (not equal) < less than > greater than Take a look at the adjusted XSL style sheet:

29 XSLT <xsl:for-each> Element
<xsl:for-each select="catalog/cd[artist='Bob Dylan']">     <tr>       <td><xsl:value-of select="title"/></td>       <td><xsl:value-of select="artist"/></td>     </tr>     </xsl:for-each>

30 XSLT <xsl:sort> Element
The <xsl:sort> element is used to sort the output. Where to put the Sort Information To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each> element in the XSL file:

31 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">       <xsl:sort select="artist"/>       <tr>         <td><xsl:value-of select="title"/></td>         <td><xsl:value-of select="artist"/></td>       </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

32 Note: The select attribute indicates what XML element to sort on.
RESULT Note: The select attribute indicates what XML element to sort on.

33 XSLT <xsl:if> Element
The <xsl:if> element is used to put a conditional test against the content of the XML file. The <xsl:if> Element To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document. Syntax <xsl:if test="expression">   ...some output if the expression is true... </xsl:if>

34 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>       <th>Price</th>     </tr>     <xsl:for-each select="catalog/cd">       <xsl:if test="price > 10">         <tr>           <td><xsl:value-of select="title"/></td>           <td><xsl:value-of select="artist"/></td>           <td><xsl:value-of select="price"/></td>         </tr>       </xsl:if>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet> Note: The value of the required test attribute contains the expression to be evaluated. The code above will only output the title and artist elements of the CDs that has a price that is higher than 10.

35 XSLT <xsl:choose> Element
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. The <xsl:choose> Element Syntax <xsl:choose>   <xsl:when test="expression">     ... some output ...   </xsl:when>   <xsl:otherwise>     ... some output ....   </xsl:otherwise> </xsl:choose>

36 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th>Title</th>       <th>Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title"/></td>       <xsl:choose>         <xsl:when test="price > 10">           <td bgcolor="#ff00ff">           <xsl:value-of select="artist"/></td>         </xsl:when>         <xsl:otherwise>           <td><xsl:value-of select="artist"/></td>         </xsl:otherwise>       </xsl:choose>     </tr>     </xsl:for-each>   </table>   </body>   </html> </xsl:template> </xsl:stylesheet>

37 RESULT

38 XSLT <xsl:apply-templates> Element
The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. The <xsl:apply-templates> Element If we add a select attribute to the <xsl:apply-templates> element it will process only the child element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed. Look at the following XSL style sheet:

39 <. xml version="1. 0" encoding="UTF-8"
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <xsl:apply-templates/>   </body>   </html> </xsl:template> <xsl:template match="cd">   <p>   <xsl:apply-templates select="title"/>   <xsl:apply-templates select="artist"/>   </p> </xsl:template> <xsl:template match="title">   Title: <span style="color:#ff0000">   <xsl:value-of select="."/></span>   <br /> </xsl:template> <xsl:template match="artist">   Artist: <span style="color:#00ff00">   <xsl:value-of select="."/></span>   <br /> </xsl:template> </xsl:stylesheet>

40 RESULT

41 XSLT - On the Client XSLT can be used to transform the document to XHTML in your browser. A JavaScript Solution In the previous chapters we have explained how XSLT can be used to transform a document from XML to XHTML. We did this by adding an XSL style sheet to the XML file and let the browser do the transformation.Even if this works fine, it is not always desirable to include a style sheet reference in an XML file (e.g. it will not work in a non XSLT aware browser.) A more versatile solution would be to use a JavaScript to do the transformation. By using a JavaScript, we can: do browser-specific testing use different style sheets according to browser and user needs That is the beauty of XSLT! One of the design goals for XSLT was to make it possible to transform data from one format to another, supporting different browsers and different user needs.

42 XML File <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog>

43 XSL File <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th style="text-align:left">Title</th>       <th style="text-align:left">Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title" /></td>       <td><xsl:value-of select="artist" /></td>     </tr>     </xsl:for-each>   </table> </xsl:template> </xsl:stylesheet>

44 Transforming XML to XHTML in the Browser
<!DOCTYPE html> <html> <head> <script> function loadXMLDoc(filename) { if (window.ActiveXObject)   {   xhttp = new ActiveXObject("Msxml2.XMLHTTP");   } else   {   xhttp = new XMLHttpRequest();   } xhttp.open("GET", filename, false); try {xhttp.responseType = "msxml-document"} catch(err) {} // Helping IE11 xhttp.send(""); return xhttp.responseXML; }

45 Transforming XML to XHTML in the Browser
function displayResult() { xml = loadXMLDoc("cdcatalog.xml"); xsl = loadXMLDoc("cdcatalog.xsl"); // code for IE if (window.ActiveXObject || xhttp.responseType == "msxml-document")   {   ex = xml.transformNode(xsl);   document.getElementById("example").innerHTML = ex;   } // code for Chrome, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument)   {   xsltProcessor = new XSLTProcessor();   xsltProcessor.importStylesheet(xsl);   resultDocument = xsltProcessor.transformToFragment(xml, document);   document.getElementById("example").appendChild(resultDocument);   } } </script> </head> <body onload="displayResult()"> <div id="example" /> </body> </html>

46 RESULT

47 Transforming XML to XHTML in the Browser
The loadXMLDoc() function does the following: Create an XMLHttpRequest object Use the open() and send() methods of the XMLHttpRequest object to send a request to a server Get the response data as XML data The displayResult() function is used to display the XML file styled by the XSL file: Load XML and XSL files Test what kind of browser the user has If Internet Explorer: Use the transformNode() method to apply the XSL style sheet to the xml document Set the body of the current document (id="example") to contain the styled xml document If other browsers: Create a new XSLTProcessor object and import the XSL file to it Use the transformToFragment() method to apply the XSL style sheet to the xml document

48 XSLT - On the Server To make XML data available to all kind of browsers, we can transform the XML document on the SERVER and send it  back to the browser as XHTML. A Cross Browser Solution In the previous chapter we explained how XSLT can be used to transform a document from XML to XHTML in the browser. We used a JavaScript and an XML parser for the transformation. However, this will not work in a browser that doesn't have an XML parser.To make XML data available to all kind of browsers, we can transform the XML document on the server and send back to the browser as XHTML. That's another beauty of XSLT. One of the design goals for XSLT was to make it possible to transform data from one format to another on a server, returning readable data to all kinds of browsers.

49 XML File <?xml version="1.0" encoding="UTF-8"?> <catalog>   <cd>     <title>Empire Burlesque</title>     <artist>Bob Dylan</artist>     <country>USA</country>     <company>Columbia</company>     <price>10.90</price>     <year>1985</year>   </cd> . . </catalog>

50 XSL FILE <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl=" <xsl:template match="/">   <h2>My CD Collection</h2>   <table border="1">     <tr bgcolor="#9acd32">       <th style="text-align:left">Title</th>       <th style="text-align:left">Artist</th>     </tr>     <xsl:for-each select="catalog/cd">     <tr>       <td><xsl:value-of select="title" /></td>       <td><xsl:value-of select="artist" /></td>     </tr>     </xsl:for-each>   </table> </xsl:template> </xsl:stylesheet>

51 PHP Code: Transform XML to XHTML on the Server
<?php // Load XML file $xml = new DOMDocument; $xml->load('cdcatalog.xml'); // Load XSL file $xsl = new DOMDocument; $xsl->load('cdcatalog.xsl'); // Configure the transformer $proc = new XSLTProcessor; // Attach the xsl rules $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); ?>

52 ASP Code: Transform XML to XHTML on the Server
<% 'Load XML file set xml = Server.CreateObject("Microsoft.XMLDOM") xml.async = false xml.load(Server.MapPath("cdcatalog.xml")) 'Load XSL file set xsl = Server.CreateObject("Microsoft.XMLDOM") xsl.async = false xsl.load(Server.MapPath("cdcatalog.xsl")) 'Transform file Response.Write(xml.transformNode(xsl)) %>


Download ppt "XSLT Introduction."

Similar presentations


Ads by Google