XSLT e Xtensible Stylesheet Language for Transformations a
XSLT: e. Xtensible Stylesheet Language for Transformations - a language for transforming XML documents into any textbased 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 formatting XML documents
How Does It Work: XSLT stylesheet XML document XSLT processor 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) Transformed XML/HTML document
Example source XML file: http: //www. w 3 schools. com/xsl/cdcatalog. xml XSLT stylesheet: http: //www. w 3 schools. com/xsl/cdcatalog. xsl Link the XSL Style Sheet to the XML Document: http: //www. w 3 schools. com/xsl/cdcatalog_with_xsl. xml Result XML file: http: //www. w 3 schools. com/xsl/cdcatalog_with_ex 1. xml
<? xml version="1. 0" encoding="ISO-8859 -1"? > Example source xml file: <catalog> <cd> http: //www. w 3 schools. com/xsl/cdcatalog. xml <title>Empire Burlesque</title> <artist>Bob Dylan</artist> XSLT stylesheet: <country>USA</country> <company>Columbia</company> http: //www. w 3 schools. com/xsl/cdcatalog. xsl <price>10. 90</price> Link the XSL Style Sheet to the XML Document: <year>1985</year> </cd> http: //www. w 3 schools. com/xsl/cdcatalog_with_xsl. xml. . . Result XML file: </catalog> http: //www. w 3 schools. com/xsl/cdcatalog_with_ex 1. xml
<? xml version="1. 0" encoding="ISO-8859 -1"? > Example source xml file: <xsl: stylesheet version="1. 0" http: //www. w 3 schools. com/xsl/cdcatalog. xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> XSLT stylesheet: <xsl: template match="/"> http: //www. w 3 schools. com/xsl/cdcatalog. xsl <html> Link the XSL Style Sheet to the XML Document: <body> <h 2>My CD Collection</h 2> http: //www. w 3 schools. com/xsl/cdcatalog_with_xsl. xml <table border="1"> Result XML file: <tr bgcolor="#9 acd 32"> <th align="left">Title</th> http: //www. w 3 schools. com/xsl/cdcatalog_with_ex 1. xml <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. w 3 schools. com/xsl/cdcatalog. xml XSLT stylesheet: http: //www. w 3 schools. com/xsl/cdcatalog. xsl Link the XSL Style Sheet to the XML Document: http: //www. w 3 schools. com/xsl/cdcatalog_with_xsl. xml Result XML file: <? xml version="1. 0" encoding="ISO-8859 -1"? > http: //www. w 3 schools. com/xsl/cdcatalog_with_ex 1. xml <? 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. w 3 schools. com/xsl/cdcatalog. xml XSLT stylesheet: http: //www. w 3 schools. com/xsl/cdcatalog. xsl Link the XSL Style Sheet to the XML Document: <html> http: //www. w 3 schools. com/xsl/cdcatalog_with_xsl. xml <body> Result XML file: <h 2>My CD Collection</h 2> <table border="1"><tbody> <tr bgcolor="#9 acd 32"> http: //www. w 3 schools. com/xsl/cdcatalog_with_ex 1. xml <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 template how to handle the specific elements in the elements 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. w 3. 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" <catalog> xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <cd> <title>Empire Burlesque</title> <xsl: template match="/"> <artist>Bob Dylan</artist> <html> <country>USA</country> <body> <company>Columbia</company> <h 2>My CD Collection</h 2> <price>10. 90</price> <table border="1"> <year>1985</year> <tr bgcolor="#9 acd 32"> </cd> <th>Title</th> … <th>Artist</th> </tr> </catalog> <xsl: for-each select="catalog/cd"> <tr> <td><xsl: value-of select="title"/></td> <td><xsl: value-of select="artist"/></td> My CD Collection </tr> </xsl: for-each> Title Artist </table> </body> Empire Burlesque Bob Dylan </html> </xsl: template> </xsl: stylesheet>
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” | “prefixed. Name” 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. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> My CD Collection <html> <body> Title <h 2>My CD Collection</h 2> Empire Burlesque <table border="1"> <tr bgcolor="#9 acd 32"> Greatest Hits <th>Title</th> The very best of <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> Artist Bob Dylan Dolly Parton Cat Stevens
<? xml version="1. 0" encoding=“UTF-8"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> My CD Collection <xsl: template match="/"> <html> <body> Title Artist <h 2>My CD Collection</h 2> Empire Burlesque Bob Dylan <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each select="catalog/cd[artist='Bob Dylan']"> <tr> <td><xsl: value-of select="title"/></td> filter <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"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> <xsl: template match="/"> My CD Collection <html> <body> Title Artist <h 2>My CD Collection</h 2> Empire Burlesque Bob Dylan <table border="1"> <tr bgcolor="#9 acd 32"> The very best of Cat Stevens <th>Title</th> Greatest Hits Dolly Parton <th>Artist</th> </tr> <xsl: for-each select="catalog/cd"> <xsl: sort select="artist"/> Sort <tr> <td><xsl: value-of select="title"/></td> On an element <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"? > <xsl: stylesheet version="1. 0" xmlns: xsl="http: //www. w 3. org/1999/XSL/Transform"> My CD Collection <xsl: template match="/"> Title Artist <html> <body> Empire Burlesque Bob Dylan <h 2>My CD Collection</h 2> <table border="1"> Greatest Hits Dolly Parton <tr bgcolor="#9 acd 32"> <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> if test <td><xsl: value-of select="artist"/></td> inside <xsl: for-each> </tr> </xsl: for-each> </xsl: if> </table> </body> </html> </xsl: template> </xsl: stylesheet>
<xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> My CD Collection <table border="1"> <tr bgcolor="#9 acd 32"> Title Artist <th>Title</th> Empire Burlesque Bob Dylan <th>Artist</th> </tr> The very best of Cat Stevens <xsl: for-each select="catalog/cd"> Greatest Hits Dolly Parton <tr> <td><xsl: value-of select="title"/></td> <xsl: choose> <xsl: when test="price > 10"> <td bgcolor="#ff 00 ff"> <xsl: value-of select="artist"/></td> </xsl: when> Multiple conditional test <xsl: otherwise> <td><xsl: value-of select="artist"/></td> </xsl: otherwise> </xsl: choose> </tr> </xsl: for-each> </table> </body> </html> </xsl: template> </xsl: stylesheet>
<xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> My CD Collection <table border="1"> <tr bgcolor="#9 acd 32"> Title Artist <th>Title</th> Empire Burlesque Bob Dylan <th>Artist</th> </tr> The very best of Cat Stevens <xsl: for-each select="catalog/cd"> Greatest Hits Dolly Parton <tr> <td><xsl: value-of select="title"/></td> <xsl: choose> <xsl: when test="price > 10"> <td bgcolor="#ff 00 ff"> <xsl: value-of select="artist"/></td> Multiple conditional test </xsl: when> 2 <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> …
XSL: apply-templates <xsl: apply-templates select=“node-set-expression” mode=“qualified. Name”> <!-- (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> <h 2>My CD Collection</h 2> <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: #ff 0000"> <xsl: value-of select=". "/> </span> <br /> </xsl: template> <xsl: template match="artist"> Artist: <span style="color: #00 ff 00"> <xsl: value-of select=". "/> </span> <br /> </xsl: template> </xsl: stylesheet> My CD Collection Title: Empire Burlesque Artist: Bob Dylan Title: Hide your heart Artist: Bonnie Tyler Title: Greatest Hits Artist: 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>
… <xsl: template match=“name”> <name first=“{first_name}” initial=“{middle_initial}” last=“{last_name}” /> /xsl: template> … <name> <first_name>Richard</first_name> <middle_initial>P</middle_initial> <last_name>Feynman</last_name> </name> <name first=“Richard” initial=“P” last=“”Feynman”/> Attribute Value Templates
Using o. Xygen XML Editor Sample files: http: //courseweb. lis. uiuc. edu/~hzhang 1/xslt/
Other Issues in Practice • XPath - a language for finding information in an XML document. • http: //www. w 3 schools. 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-nmspace 2. html
References: • This PPT is based on XSLT Tutorial at: http: //www. w 3 schools. 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. w 3 schools. com/xsl_w 3 celementref. asp
- Slides: 26