370 likes | 486 Views
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
E N D
An XSLT stylesheet is an XML document defining a transformation for a class of XML documentsA stylesheet seperates contents and logical structure from presentation
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
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.
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.
xsl:text • <xsl:text ...> ... </...> as raw text but allows control of white-space stripping and escaping • <xsl:comment> ... </...> creates comment
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);
<?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>
<?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 . .
<?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>
Title Artist • Empire Burlesque Bob Dylan
<?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
<?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>
<?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>
<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>
<HTML> • <HEAD> </HEAD> • <BODY> • <H1>XSL</H1> • <H2>John Smith</H2> • </BODY> • </HTML>
XSL • John Smith
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>
HTML> • <HEAD> </HEAD> • <BODY> • <H2>John Smith</H2> • <H1>XSL</H1> • </BODY> • </HTML>
John Smith • XSL
<xslTutorial > • <dog name='Joe'> • <data weight='18 kg' color="black"/> • </dog> • </xslTutorial
data/@color • matches color attribute of data element which is a child of dog element
<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>
<HTML> • <HEAD> </HEAD> • <BODY> • <P> • <B>Dog: </B>Joe</P> • <P> • <B>Color: </B>black</P> </BODY> </HTML>
Dog: Joe • Color: black
<xslTutorial > • <name>John</name> • <name>Josua</name> • <name>Charles</name> • <name>Alice</name> • <name>Martha</name> • <name>George</name> • </xslTutorial>
<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
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>
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.
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
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>
Martha • Josua • John • Charles • George • Alice
<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>
Martha • Josua • John • Charles • George • Alice
<?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>
<?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>
<?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>………..