1 / 37

XML Stylesheets

XML Stylesheets. Andy Clark. What Is It?. Extensible Stylesheet Language (XSL) Language for document transformation Transformation (XSLT) Converting XML to another form e.g. Use Xalan to convert to text, XML, HTML, etc… Formatting objects (XSL:FO) Layout of XML document

shae
Download Presentation

XML Stylesheets

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. XML Stylesheets Andy Clark 17 Apr 2002

  2. What Is It? • Extensible Stylesheet Language (XSL) • Language for document transformation • Transformation (XSLT) • Converting XML to another form • e.g. Use Xalan to convert to text, XML, HTML, etc… • Formatting objects (XSL:FO) • Layout of XML document • e.g. Use FOP to convert to PDF, etc… • Defined by W3C

  3. The Simplest Stylesheet (1 of 2) • Empty XSLT document 01 <?xml version=‘1.0’ encoding=‘UTF-16’?> 02 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 03 version=‘1.0’> 04 </xsl:stylesheet>

  4. The Simplest Stylesheet (2 of 2) • Empty XSLT document • Note: This will simply copy the text content of the input document to the output. 01 <?xml version=‘1.0’ encoding=‘UTF-16’?> 02 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 03version=‘1.0’> 04</xsl:stylesheet>

  5. What Are Its Features? • Features • Templates • Map input patterns to output • Conditionals • Loops • Functions • Extensions

  6. Templates • Transformation rules • Selects nodes from input document • Uses XPath expressions • Specifies output • Can be text, XML, HTML (xml with html tags) • Template modes • Allows multiple templates to have same rules • Named templates • Call templates by name

  7. Simple Template (1 of 4) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template>

  8. Simple Template (2 of 4) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template>

  9. Simple Template (3 of 4) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template>

  10. Simple Template (4 of 4) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template>

  11. Simple Template+ (1 of 5) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template> • <xsl:template match=‘book’> • <H1> <xsl:value-of select=‘title’/> </H1> • </xsl:template>

  12. Simple Template+ (2 of 5) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template> • <xsl:template match=‘book’> • <H1> <xsl:value-of select=‘title’/> </H1> • </xsl:template>

  13. Simple Template+ (3 of 5) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template> • <xsl:template match=‘book’> • <H1> <xsl:value-of select=‘title’/> </H1> • </xsl:template>

  14. Simple Template+ (4 of 5) • Match document root • <xsl:template match=‘/’> • <HTML> • <BODY> • <xsl:apply-templates select=‘child::*’/> • </BODY> • </HTML> • </xsl:template> • <xsl:template match=‘book’> • <H1> <xsl:value-of select=‘title’/> </H1> • </xsl:template>

  15. Simple Template+ (5 of 5) • Input • Output 01 <book isdn=‘1234-5678-90’> 02 <title>Care and Feeding of Wombats</title> 03 <chapter/> nn </book> 01 <HTML> 02 <BODY> 03 <H1>Care and Feeding of Wombats</H1> 04 </BODY> 05 </HTML>

  16. Conditionals • If statement • <xsl:if test='expression'> ... </xsl:if> • Switch statement • <xsl:choose> • <xsl:when test='expression'> ... </xsl:when> • <xsl:otherwise> ... </xsl:otherwise> • </xsl:choose> • Predicates • foo[@bar="value"]

  17. Loops • For statement • <xsl:for-each select='expression'> • <xsl:sort select='expression'/> • … • </xsl:for-each>

  18. XPath Functions • Node-set functions • e.g. position(), last(), local-name(), etc… • String functions • e.g. string(), contains(), substring(), etc… • Boolean functions • e.g. boolean(), not(), etc… • Number functions • e.g. number(), sum(), round(), etc…

  19. XSLT Functions • Multiple source documents • e.g. document(“document2.xml”) • Keys • Number formatting • Additional functions • e.g. current(), system-property(), etc…

  20. Example Transformation (0 of 14) • Source • Destination 01 <order> 02 <item code=‘BK123’> 03 <name>Care and Feeding of Wombats</name> 04 <price currency=‘USD’>42.00</price> 05 </item> 06 </order> 01 <HTML> 02 <BODY> 03 <TABLE border=‘1’> 04 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 05 <TR> 06 <TD>BK123 - <U>Care and Feeding of Wombats</U></TD> 07 <TD>$42.00</TD> 08 </TR> 09 </TABLE> 10 </BODY> 11 </HTML>

  21. Example Transformation (1 of 14) • Match <order> element 01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 02 version=‘1.0’> 03 04 <xsl:template match=‘/order’> 05 <HTML> 06 <BODY> 07 <TABLE border=‘1’> 08 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 09 <xsl:for-each select=‘item’> 10 <xsl:apply-templates/> 11 </xsl:for-each> 12 </TABLE> 13 </BODY> 14 </HTML> 15 </xsl:template>

  22. Example Transformation (2 of 14) • Match <order> element 01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 02version=‘1.0’> 03 04 <xsl:template match=‘/order’> 05 <HTML> 06 <BODY> 07 <TABLE border=‘1’> 08 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 09 <xsl:for-each select=‘item’> 10 <xsl:apply-templates/> 11 </xsl:for-each> 12 </TABLE> 13 </BODY> 14 </HTML> 15 </xsl:template>

  23. Example Transformation (3 of 14) • Match <order> element 01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 02 version=‘1.0’> 03 04 <xsl:template match=‘/order’> 05 <HTML> 06 <BODY> 07 <TABLE border=‘1’> 08 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 09 <xsl:for-each select=‘item’> 10 <xsl:apply-templates/> 11 </xsl:for-each> 12 </TABLE> 13 </BODY> 14 </HTML> 15 </xsl:template>

  24. Example Transformation (4 of 14) • Match <order> element 01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 02 version=‘1.0’> 03 04 <xsl:template match=‘/order’> 05<HTML> 06 <BODY> 07 <TABLE border=‘1’> 08 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 09 <xsl:for-each select=‘item’> 10 <xsl:apply-templates/> 11 </xsl:for-each> 12 </TABLE> 13 </BODY> 14 </HTML> 15 </xsl:template>

  25. Example Transformation (5 of 14) • Match <order> element 01 <xsl:stylesheet xmlns:xsl=‘http://www.w3.org/1999/XSL/Transform’ 02 version=‘1.0’> 03 04 <xsl:template match=‘/order’> 05 <HTML> 06 <BODY> 07 <TABLE border=‘1’> 08 <TR> <TH>Item</TH> <TH>Price</TH> </TR> 09 <xsl:for-each select=‘item’> 10 <xsl:apply-templates/> 11 </xsl:for-each> 12 </TABLE> 13 </BODY> 14 </HTML> 15 </xsl:template>

  26. Example Transformation (6 of 14) • Match <item> element 17 <xsl:template match=‘item’> 18 <TR> 19 <TD> 20 <xsl:value-of select=‘@code’/> - 21 <U> <xsl:value-of select=‘name’/> </U> 22 </TD> 23 <xsl:apply-templates select=‘price’/> 24 </TR> 25 </xsl:template>

  27. Example Transformation (7 of 14) • Match <item> element 17 <xsl:template match=‘item’> 18 <TR> 19 <TD> 20 <xsl:value-of select=‘@code’/> - 21 <U> <xsl:value-of select=‘name’/> </U> 22 </TD> 23 <xsl:apply-templates select=‘price’/> 24 </TR> 25 </xsl:template>

  28. Example Transformation (8 of 14) • Match <item> element 17 <xsl:template match=‘item’> 18 <TR> 19 <TD> 20 <xsl:value-of select=‘@code’/> - 21 <U> <xsl:value-of select=‘name’/> </U> 22 </TD> 23 <xsl:apply-templates select=‘price’/> 24 </TR> 25 </xsl:template>

  29. Example Transformation (9 of 14) • Match <item> element 17 <xsl:template match=‘item’> 18 <TR> 19 <TD> 20 <xsl:value-of select=‘@code’/> - 21 <U> <xsl:value-of select=‘name’/> </U> 22 </TD> 23 <xsl:apply-templates select=‘price’/> 24 </TR> 25 </xsl:template>

  30. Example Transformation (10 of 14) • Match <price> element 27 <xsl:template match=‘price’> 28 <TD> 29 <xsl:choose> 30 <xsl:when test=‘text()=“JPN”’> ¥ </xsl:when> 31 <xsl:otherwise> $ </xsl:otherwise> 32 </xsl:choose> 33 <xsl:value-of select=‘text()’/> 34 </TD> 35 </xsl:template> 36 37 </xsl:stylesheet>

  31. Example Transformation (11 of 14) • Match <price> element 27 <xsl:template match=‘price’> 28 <TD> 29 <xsl:choose> 30 <xsl:when test=‘text()=“JPN”’> ¥ </xsl:when> 31 <xsl:otherwise> $ </xsl:otherwise> 32 </xsl:choose> 33 <xsl:value-of select=‘text()’/> 34 </TD> 35 </xsl:template> 36 37 </xsl:stylesheet>

  32. Example Transformation (12 of 14) • Match <price> element 27 <xsl:template match=‘price’> 28 <TD> 29 <xsl:choose> 30 <xsl:when test=‘text()=“JPN”’> ¥ </xsl:when> 31 <xsl:otherwise> $ </xsl:otherwise> 32 </xsl:choose> 33 <xsl:value-of select=‘text()’/> 34 </TD> 35 </xsl:template> 36 37 </xsl:stylesheet>

  33. Example Transformation (13 of 14) • Match <price> element 27 <xsl:template match=‘price’> 28 <TD> 29 <xsl:choose> 30 <xsl:when test=‘text()=“JPN”’> ¥ </xsl:when> 31 <xsl:otherwise> $ </xsl:otherwise> 32 </xsl:choose> 33 <xsl:value-of select=‘text()’/> 34 </TD> 35 </xsl:template> 36 37 </xsl:stylesheet>

  34. Example Transformation (14 of 14) • Output

  35. Rendering XML in Browsers • Latest browsers (e.g. IE 6.0+) have support for XSLT • Insert “xml-stylesheet” processing instruction • <?xml-stylesheet type=‘text/xsl’ href=‘style.xsl’?> • Output

  36. Useful Links • XPath 1.0 Specification • http://www.w3.org/TR/xpath • XSLT 1.0 Specification • http://www.w3.org/TR/xslt

  37. XML Stylesheets Andy Clark

More Related