1 / 37

XSLT – XML stylesheet Transformation

XSLT – XML stylesheet Transformation. An XSLT stylesheet is an XML document defining a transformation for a class of XML documents A stylesheet seperates contents and logical structure from presentation. XSLT. xsl:stylesheet

aida
Download Presentation

XSLT – XML stylesheet Transformation

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 – XML stylesheet Transformation

  2. An XSLT stylesheet is an XML document defining a transformation for a class of XML documentsA stylesheet seperates contents and logical structure from presentation

  3. XSLT • xsl:stylesheet • A stylesheet is represented by an xsl:stylesheet element in an XML document. • xsl:transform is allowed as a synonym for xsl:stylesheet. • The namespace http://www.w3.org/1999/XSL/Transform is used to recognize the XSL elements

  4. A template rule is specified with the xsl:template element. • The content of the xsl:template element is the template • The match attribute is a Pattern that identifies the source node or nodes to which the rule applies.

  5. select • A select attribute can be used to process nodes selected by an expression. • The value of the select attribute is an expression. • The xsl:value-of element is instantiated to create a text node in the result tree.

  6. xsl:text • <xsl:text ...> ... </...> as raw text but allows control of white-space stripping and escaping • <xsl:comment> ... </...> creates comment

  7. The xsl:for-each instruction contains a template, which is instantiated for each node selected by the expression specified by the select attribute. The select attribute is required • <xsl:for-each select="node-set expression"> template </...> • instantiate template for each node in node-set • <xsl:apply-templates select="node-set expression" .../> • apply pattern matching and template instantiation on selected nodes (default: all children);

  8. <?xml version="1.0"?> • <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>

  9. <?xml version="1.0" ?> • <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"><html><body> • <table border="1"><tr><th>Title</th><th>Artist</th></tr><tr><td>.</td><td>.</td> • </tr> • </table> • </body> • </html> </xsl:template> </xsl:stylesheet> Title Artist . .

  10. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"> • <html> <body> <table border="1"> • <tr> <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>

  11. Title Artist • Empire Burlesque Bob Dylan

  12. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"> • <html> <body> <table border="1"> • <tr> <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> • It will construct complete table

  13. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"> • <html> • <body> <table border="2" bgcolor="yellow"> <tr> • <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>

  14. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="/"> • <html> <body> <table border="2" bgcolor="yellow"> • <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="CATALOG/CD" order-by="+ 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>

  15. <xslTutorial > <title>XSL</title> <author>John Smith</author> </xslTutorial> <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > <xsl:templatematch="/"> <H1><xsl:value-ofselect="//title"/></H1> <H2><xsl:value-ofselect="//author"/></H2> </xsl:template> </xsl:stylesheet>

  16. <HTML> • <HEAD> </HEAD> • <BODY> • <H1>XSL</H1> • <H2>John Smith</H2> • </BODY> • </HTML>

  17. XSL • John Smith

  18. xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > • <xsl:templatematch="/"> • <H2><xsl:value-ofselect="//author"/></H2> • <H1><xsl:value-ofselect="//title"/></H1> • </xsl:template> • </xsl:stylesheet>

  19. HTML> • <HEAD> </HEAD> • <BODY> • <H2>John Smith</H2> • <H1>XSL</H1> • </BODY> • </HTML>

  20. John Smith • XSL

  21. <xslTutorial > • <dog name='Joe'> • <data weight='18 kg' color="black"/> • </dog> • </xslTutorial

  22. data/@color • matches color attribute of data element which is a child of dog element

  23. <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > • <xsl:templatematch="dog"> • <P><B><xsl:text> Dog: </xsl:text> </B> • <xsl:value-ofselect="@name"/></P> • <P><B><xsl:text> Color: </xsl:text> </B> • <xsl:value-ofselect="data/@color"/></P> • </xsl:template> • </xsl:stylesheet>

  24. <HTML> • <HEAD> </HEAD> • <BODY> • <P> • <B>Dog: </B>Joe</P> • <P> • <B>Color: </B>black</P> </BODY> </HTML>

  25. Dog: Joe • Color: black

  26. <xslTutorial > • <name>John</name> • <name>Josua</name> • <name>Charles</name> • <name>Alice</name> • <name>Martha</name> • <name>George</name> • </xslTutorial>

  27. <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > • <xsl:templatematch="/"> • <TABLE> • <xsl:for-eachselect="//name"> • <xsl:sortorder="ascending" select="."/> • <TR><TH> • <xsl:value-ofselect="."/></TH></TR> • </xsl:for-each> • </TABLE> • </xsl:template> • </xsl:stylesheet

  28. HTML> • <HEAD> </HEAD> • <BODY> • <TABLE> • <TR> • <TH>Alice</TH></TR> • <TR> • <TH>George</TH></TR> • <TR> • <TH>Charles</TH></TR> • <TR> • <TH>John</TH></TR> • <TR> • <TH>Josua</TH></TR> • <TR> • <TH>Martha</TH></TR></TABLE> </BODY> </HTML>

  29. Sorting is specified by adding xsl:sort elements as children of xsl:apply-templates or xsl:for-each. The first xsl:sort child specifies the primary sort key, the second xsl:sort child specifies the secondary sort key and so on. • When used in xsl:for-each, xsl:sort elements must occur first.

  30. order specifies whether the strings should be sorted in ascending or descending order; ascending specifies ascending order; descending specifies descending order; the default is ascending

  31. xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > • <xsl:templatematch="/"> • <TABLE> • <xsl:for-eachselect="//name"> • <xsl:sortorder="descending" select="."/> • <TR><TH><xsl:value-ofselect="."/></TH></TR> • </xsl:for-each> • </TABLE> • </xsl:template> • </xsl:stylesheet>

  32. Martha • Josua • John • Charles • George • Alice

  33. <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' > • <xsl:templatematch="/"> • <TABLE> • <xsl:apply-templatesselect="//name"> • <xsl:sortorder="descending" select="."/> • </xsl:apply-templates> • </TABLE> • </xsl:template> • <xsl:templatematch="name"> • <TR><TH><xsl:value-ofselect="."/></TH></TR> • </xsl:template> • </xsl:stylesheet>

  34. Martha • Josua • John • Charles • George • Alice

  35. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> • <html> <body> <table border="2" bgcolor="yellow"> <tr> <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>

  36. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> • <html> <body> <table border="2" bgcolor="yellow"> • <tr> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="CATALOG/CD"> • <xsl:if match=".[ARTIST='Bob Dylan']"> • <tr> <td><xsl:value-of select="TITLE"/></td> <td><xsl:value-of select="ARTIST"/></td> </tr> </xsl:if> </xsl:for-each> </table> </body> </html> • </xsl:template> • </xsl:stylesheet>

  37. <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> • <html> <body> • <table border="2" bgcolor="yellow"> <tr> <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 match=".[ARTIST='Bob Dylan']"> <td bgcolor="#ff0000"> <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>………..

More Related