XSLT 1 CONTENTS XSLT and uses XSLT example

  • Slides: 16
Download presentation
XSLT 1

XSLT 1

CONTENTS XSLT and uses XSLT example Simple XML How XSLT works? Template-match Value-of For-each

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? 2

XSLT AND USES XSLT stands for Extensible Stylesheet Language Transformations. XSLT is designed for

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 o 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 o 3

XSLT EXAMPLE 4

XSLT EXAMPLE 4

SAMPLE XML <? xml: stylesheet type="text/xsl" href="classical. xsl"? > <authors> <author period="Classical"> Specify XSL

SAMPLE XML <? xml: stylesheet type="text/xsl" href="classical. xsl"? > <authors> <author period="Classical"> Specify XSL for display <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> </authors> 5

SAMPLE XSL <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/TR/WD-xsl"> <xsl: template match="/"> XSL

SAMPLE XSL <xsl: stylesheet xmlns: xsl="http: //www. w 3. org/TR/WD-xsl"> <xsl: template match="/"> XSL version <HTML> <HEAD><TITLE>Authors</TITLE></HEAD> <BODY> <H 1>Composers from Austria</H 1> <TABLE BORDER="1"><TR><TH>Name</TH></TR> <xsl: for-each select="/authors/author[nationality='Austrian']"> XPath <TR><TD><xsl: value-of select="name" /></TD></TR> </xsl: for-each> </TABLE> </BODY> </HTML> </xsl: template> </xsl: stylesheet> 6

PUTTING IT TOGETHER The XSL was: <xsl: template match="/"> <html><body> <h 1> <xsl: value-of

PUTTING IT TOGETHER The XSL was: <xsl: template match="/"> <html><body> <h 1> <xsl: value-of select="message"/> </h 1> </body></html> </xsl: template> The <xsl: template match="/"> chooses the root The <html><body> <h 1> is written to the output file The contents of message is written to the output file The </h 1> </body></html> is written to the output file The resultant file looks like: <html><body> <h 1> Hiiii</h 1> </body></html> 7

HOW XSLT WORKS The XML text document is read in and stored as a

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 8

TEMPLATE MATCH <xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> <table border="1">

TEMPLATE MATCH <xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </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> 9

VALUE-OF <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each select="catalog/cd"> <tr>

VALUE-OF <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each select="catalog/cd"> <tr> <td> <xsl: value-of select="title"/> </td> <xsl: value-of select="artist"/> </td> </tr> </xsl: for-each> </table> 10

FOR-EACH AND SORT <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each

FOR-EACH AND SORT <table border="1"> <tr bgcolor="#9 acd 32"> <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> 11

IF <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each select="catalog/cd"> <xsl:

IF <table border="1"> <tr bgcolor="#9 acd 32"> <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> 12

CHOOSE WHEN <table border="1"> <tr bgcolor="#9 acd 32"> <th>Title</th> <th>Artist</th> </tr> <xsl: for-each select="catalog/cd">

CHOOSE WHEN <table border="1"> <tr bgcolor="#9 acd 32"> <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="#ff 00 ff"> <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> 13

APPLY-TEMPLATES <xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> <xsl: apply-templates/> </body>

APPLY-TEMPLATES <xsl: template match="/"> <html> <body> <h 2>My CD Collection</h 2> <xsl: apply-templates/> </body> </html> </xsl: template> <xsl: template match="title"> Title: <span style="color: #ff 0000"> <xsl: value-of select=". "/></span> </xsl: template> <xsl: template match="cd"> <xsl: template match="artist"> <p> Artist: <span <xsl: apply-templates style="color: #00 ff 00"> select="title"/> <xsl: value-of <xsl: apply-templates select="artist"/> select=". "/></span> </p> </xsl: template> </xsl: stylesheet> 14

WHERE XSLT CAN BE USED With an appropriate program, such as Xerces, XSLT can

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 15

THANK YOU 16

THANK YOU 16