Upload
emory-powers
View
214
Download
1
Embed Size (px)
Citation preview
1
XSLT
2
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?
3
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: o Easy to merge data into XML data into a presentationo XSLT, when used in websites, allow strict separation between
HTML and code-behind.o We use XSLT extensively for things like documentation, and
making some complex configuration settings user-serviceable
4
XSLT EXAMPLE
5
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>
SpecifyXSL for display
6
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>
XSL version
XPath
7
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>
8
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
9
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>
10
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>
11
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>
12
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>
13
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>
14
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>
15
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
16
THANK YOU