190 likes | 479 Vues
XSLT. Contents . XSLT and uses XSLT example Simple XML How XSLT works? Template-match Value-of For-each and sort If Choose-when Apply-templates Where XSLT can be used?. XSLT and Uses.
E N D
Contents • XSLT and uses • XSLT example • Simple XML • How XSLT works? • Template-match • Value-of • For-each and sort • If • Choose-when • Apply-templates • Where XSLT can be used?
XSLT and Uses • XSLT stands for Extensible Stylesheet Language Transformations. XSLT is designed for use as part of XSL, which is a stylesheet language for XML - Transforms one XML to another XML or other text such as HTML • Advantages: • Easy to merge data into XML data into a presentation • XSLT, when used in websites, allow strict separation between HTML and code-behind. • We use XSLT extensively for things like documentation, and making some complex configuration settings user-serviceable
SpecifyXSL for display Sample XML <?xml:stylesheet type="text/xsl" href="classical.xsl"?> <authors> <author period="Classical"> <name>Mozart</name> <nationality>Austrian</nationality> </author> <author period="Classical"> <name>Beethoven</name> <nationality>German</nationality> </author> <author period="Baroque"> <name>Bach</name> <nationality>German</nationality> </author> </authors>
XSL version XPath Sample XSL <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <HTML> <HEAD><TITLE>Authors</TITLE></HEAD> <BODY> <H1>Composers from Austria</H1> <TABLE BORDER="1"><TR><TH>Name</TH></TR> <xsl:for-each select="/authors/author[nationality='Austrian']"> <TR><TD><xsl:value-of select="name" /></TD></TR> </xsl:for-each> </TABLE> </BODY> </HTML> </xsl:template> </xsl:stylesheet>
Putting it together • The XSL was: <xsl:template match="/"> <html><body><h1> <xsl:value-of select="message"/> </h1> </body></html></xsl:template> • The <xsl:template match="/"> chooses the root • The <html><body> <h1> is written to the output file • The contents of message is written to the output file • The </h1> </body></html> is written to the output file • The resultant file looks like: <html><body> <h1> Hiiii</h1></body></html>
How XSLT works • The XML text document is read in and stored as a tree of nodes • The <xsl:template match="/"> template is used to select the entire tree • The rules within the template are applied to the matching nodes, thus changing the structure of the XML tree • Unmatched parts of the XML tree are not changed • After the template is applied, the tree is written out again as a text document
Template match <xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <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>
Value-of <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>
For-each and sort <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>
if <table border="1"> <tr bgcolor="#9acd32"> <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> <td><xsl:value-of select="artist"/></td> </tr></xsl:if> </xsl:for-each> </table>
Choose when <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 > 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>
Apply-templates <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>
Where XSLT can be used • With an appropriate program, such as Xerces, XSLT can be used to read and write files • A server can use XSLT to change XML files into HTML files before sending them to the client • A modern browser can use XSLT to change XML into HTML on the client side • Some applications which store data in XML use XSLT to present this data in a human-readable format. For example, Windows Live Messenger stores the trace of messages as XML, but when you open the history in WLM itself, it shows you a pretty table which in fact is HTML built through XSLT