1 / 26

XSLT XSLT: eXtensible Stylesheet Language for Transformations

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:

kin
Télécharger la présentation

XSLT XSLT: eXtensible Stylesheet Language for Transformations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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)

  3. 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

  4. <?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

  5. <?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

  6. 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>

  7. 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>

  8. 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.

  9. 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>

  10. 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>

  11. 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

  12. <?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

  13. 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” />

  14. <?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

  15. <?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

  16. <?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

  17. <?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 &gt; 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>

  18. <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 &gt; 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

  19. <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 &gt; 10"> <td bgcolor="#ff00ff"> <xsl:value-of select="artist"/></td> </xsl:when> <xsl:when test="price &gt; 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

  20. 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

  21. <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>

  22. <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>

  23. <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

  24. Using oXygen XML Editor

  25. 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

  26. 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

More Related