260 likes | 390 Views
XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text-based format (e.g. HTML, plain text, another XML, etc.) - Why do we need to learn it? - A typical work flow:
E N D
XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text-based format (e.g. HTML, plain text, another XML, etc.) - Why do we need to learn it? - A typical work flow: legacy data source > XML > XSL transformation > application/Web browser XPath - a language for navigating in XML documents (XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents) XSL-FO - a language for formatting XML documents
How Does It Work: XSLT stylesheet XSLT processor Transformed XML/HTML document XML document An XSLT processor can be built into a web browser, or can be a standalone program run from the command (e.g. SAXON or Apache Xalan)
Example source XML file: http://www.w3schools.com/xsl/cdcatalog.xml XSLT stylesheet: http://www.w3schools.com/xsl/cdcatalog.xsl Link the XSL Style Sheet to the XML Document: http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml Result XML file: http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <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> Example source xml file: http://www.w3schools.com/xsl/cdcatalog.xml XSLT stylesheet: http://www.w3schools.com/xsl/cdcatalog.xsl Link the XSL Style Sheet to the XML Document: http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml Result XML file: http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th 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> </body> </html> </xsl:template> </xsl:stylesheet> Example source xml file: http://www.w3schools.com/xsl/cdcatalog.xml XSLT stylesheet: http://www.w3schools.com/xsl/cdcatalog.xsl Link the XSL Style Sheet to the XML Document: http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml Result XML file: http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
Example source xml file: http://www.w3schools.com/xsl/cdcatalog.xml XSLT stylesheet: http://www.w3schools.com/xsl/cdcatalog.xsl Link the XSL Style Sheet to the XML Document: http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml Result XML file: http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml <?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type=“text/xsl" href="cdcatalog.xsl"?> <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>
Example source XML file: http://www.w3schools.com/xsl/cdcatalog.xml XSLT stylesheet: http://www.w3schools.com/xsl/cdcatalog.xsl Link the XSL Style Sheet to the XML Document: http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml Result XML file: http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml <html> <body> <h2>My CD Collection</h2> <table border="1"><tbody> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <tr><td>Empire Burlesque</td> <td>Bob Dylan</td> </tr> … </tbody></table> </body> </html>
Template Rules • An XSL style sheet consists of one or more set of rules that are called templates. • Each template rule tells the XSLT processor how to handle the specific elements in the input XML document.
Template Rules (Example) <xsl:template match=“/”> root element <xsl:template match=“catalog/cd”> sub-element “cd” of “catalog” </xsl:template> … <xsl:template match=“cd”> title: <xsl:value-of select=“title”> value of element “title” </xsl:template>
Structure of a stylesheet <xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“1.0”> …… <xsl:template match="pattern"> \template> a template rule </xsl:template> / …… <- other templates> </xsl:stylesheet>
Processing Model • For a given input XML document, the output is obtained as follows: • The source tree is processed by processing the root node • - a node list is processed by processing each node in order and concatenating the results • - a single node is processed by: • finding the template rule with the matching pattern, and • creating result fragment
<?xml version="1.0" encoding=“UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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> <?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> My CD Collection
XSL:for-each <xsl:for-each select = “node-set-expression” </xsl:for-each> XSL:if <xsl:if test = “boolean-expression” </xsl:if> XSL:sort <xsl:sort select = “string-expression” data-type = “text” | “number” | “prefixedName” lang = “langcode” order = “ascending” | “descending” case-order = “upper-first” | “lower-first” />
<?xml version="1.0" encoding=“UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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> My CD Collection
<?xml version="1.0" encoding=“UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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[artist='Bob Dylan']"> <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> My CD Collection filter
<?xml version="1.0" encoding=“UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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> My CD Collection Sort On an element
<?xml version="1.0" encoding=“UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </xsl:if> </table> </body> </html> </xsl:template> </xsl:stylesheet> My CD Collection if test inside <xsl:for-each>
<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> My CD Collection Multiple conditional test
<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:when test="price > 9"> <td bgcolor="#cccccc"> <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> … My CD Collection Multiple conditional test 2
XSL:apply-templates <xsl:apply-templates select=“node-set-expression” mode=“qualifiedName”> <!-- (xsl:sort | xsl:with-param)* --> </xsl:apply-templates> Without xsl:sort child elements, the default is in document order; xsl:with-param child elements can be used to pass parameter values to the matched templates
<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> My CD Collection Title: Empire BurlesqueArtist: Bob Dylan Title: Hide your heartArtist: Bonnie Tyler Title: Greatest HitsArtist: Dolly Parton <xsl:apply-templates>
… <xsl:template match=“person”> <xsl:apply-templates select=“name”/> Born: <xsl:apply-templates select=“@born”/> Died: <xsl:apply-templates select=“@died”/> </xsl:template> … … Born: 1918 Died: 1988 … Select attribute value in <xsl:apply-templates>
<name> <first_name>Richard</first_name> <middle_initial>P</middle_initial> <last_name>Feynman</last_name> </name> … <xsl:template match=“name”> <name first=“{first_name}” initial=“{middle_initial}” last=“{last_name}” /> /xsl:template> … <name first=“Richard” initial=“P” last=“”Feynman”/> Attribute Value Templates
Other Issues in Practice • XPath - a language for finding information in an XML document. • http://www.w3schools.com/xpath/default.asp • XPath functions • Be aware of the “context” • Namespace (e.g. “dc:author”) • http://www.ibm.com/developerworks/xml/library/x-nmspace.html • http://www.ibm.com/developerworks/xml/library/x-nmspace2.html
References: • This PPT is based on XSLT Tutorial at: http://www.w3schools.com/xsl/ • Zvon XSL Tutorial: http://www.zvon.org/xxl/XSLTutorial/Output/index.html • XSLT reference: http://www.zvon.org/xxl/XSLTreference/Output/index.html http://www.w3schools.com/xsl/xsl_w3celementref.asp