Emerging Technology
Fall Semester, 2004 XSLT |
||
---|---|---|
© 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 16-Sep-04 |
CS 683 Emerging Technologies Fall Semester, 2004 Doc 9 XSLT
Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
CS 683 Fall 04 | Doc 9, XSLT Slide # 2 |
XSL Transformations (XSLT) Version 1.0
Learning XML , Erik Ray, O'Reilly, 2001
ZVON Miloslav Nic’s XSLT Tutorial, http://www.zvon.org/xxl/XSLTutorial/Output/index.html
ZVON Miloslav Nic’s & Jiri Jirat XPath Tutorial, http://www.zvon.org/xxl/XPathTutorial/General/examples.html
W3Schools XSLT Tutorial, http://www.w3schools.com/xsl/default.asp
CS 683 Fall 04 | Doc 9, XSLT Slide # 3 |
Extensible Stylesheet Language for Transformation (XSLT)
CS 683 Fall 04 | Doc 9, XSLT Slide # 4 |
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="hello.xsl"?> <greetings>Hello World</greetings>
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <title> <xsl:value-of select="greetings"/> </title> <body> <h1> <xsl:value-of select="greetings"/> </h1> </body> </html> </xsl:template> </xsl:stylesheet>
CS 683 Fall 04 Doc 9, XSLT Slide # 5
Place both files in the same directory
Open hello.xml with an XML/XSLT aware web browser
CS 683 Fall 04 | Doc 9, XSLT Slide # 6 |
Load the parcel xsl.pst. The following code assumes the files are in the same directory at the image. For more information read pages 529 to 533 of the VisualWorks Developers Guide.
xslRules := ( XSL.RuleDatabase new ) readFileNamed: 'hello.xsl'. parser := XMLParser new validate: false. xmlDocument := parser parse: 'hello.xml' asFilename readStream. transformedDocument := xslRules process: xmlDocument.
<html> <title>Hello World</title> <body> <h1>Hello World</h1> </body> </html>
CS 683 Fall 04 | Doc 9, XSLT Slide # 7 |
Download Xalan-Java ( http://xml.apache.org/xalan-j/)
Place the xalan-2.6.0.jar in your classpath
java org.apache.xalan.xslt.Process -in hello.xml -xsl hello.xsl
CS 683 Fall 04 | Doc 9, XSLT Slide # 8 |
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class SimpleTransform { public static void main(String[] args) throws TransformerException, TransformerConfigurationException, FileNotFoundException, IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(new StreamSource("hello.xsl")); transformer.transform( new StreamSource("hello.xml"), new StreamResult(new FileOutputStream("hello.html"))); } }
CS 683 Fall 04 | Doc 9, XSLT Slide # 9 |
<xsl:template> contains
CS 683 Fall 04 | Doc 9, XSLT Slide # 10 |
items/item[position()>1] matches any item element that has a items parent and that is not the first item child of its parent
CS 683 Fall 04 | Doc 9, XSLT Slide # 11 |
CS 683 Fall 04 | Doc 9, XSLT Slide # 12 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> </body></html> </xsl:template> |
<html> <body></body> </html> |
XSLT |
Result |
<xsl:template match="/"> <html> <xsl:value-of select="//d" /> </html> </xsl:template> |
<html> <body>Your Dog</body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 13 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:value-of select="/a/d" /> </body></html> </xsl:template> |
<html> <body>Your Dog</body> </html> |
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:value-of select="/a/c/d" /> </body></html> </xsl:template> |
<html> <body>My Dog</body> </html> |
XSLT |
Result |
<xsl:template match="/"> <html> <xsl:value-of select="d" /> </html> </xsl:template> |
<html></html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 14 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="b"> <h1> <xsl:apply-templates /> </h1> </xsl:template> <xsl:template match="d"> <b> <xsl:apply-templates /> </b> </xsl:template> |
<html> <body> <h1>My Cat</h1> <h1>B2</h1> <b>Your Dog</b> <b>My Dog</b> </body> </html> |
Both <xsl:apply-templates> and <xsl:value-of > are recursive
CS 683 Fall 04 | Doc 9, XSLT Slide # 15 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> <body></html> </xsl:template> <xsl:template match="d"> <b> <xsl:apply-templates /> </b> </xsl:template> |
<html> <body> My Cat B2 <b>Your Dog</b> <b>My Dog</b> </body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 16 |
<xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>
<xsl:template match="processing-instruction()|comment()"/>
Built-in rules only apply if there is no other match for an element
CS 683 Fall 04 | Doc 9, XSLT Slide # 17 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="d"> <b> <xsl:value-of select="." /> </b> </xsl:template> <xsl:template match="text()" /> |
<html> <body> <b>Your Dog</b> <b>My Dog</b> </body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 18 |
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="d"> <b> <xsl:apply-templates /> </b> </xsl:template> <xsl:template match="text()" /> |
<html> <body> <b></b> <b></b> </body> </html>
|
CS 683 Fall 04 | Doc 9, XSLT Slide # 19 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="d"> <b> <xsl:value-of select="." /> </b> </xsl:template> <xsl:template match="text()" /> |
<b>Your Dog</b> <b>My Dog</b> |
How is this rule applied to the d tags, which are nested inside the document?
CS 683 Fall 04 | Doc 9, XSLT Slide # 20 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="@*"> <p> <xsl:value-of select="." /> </p> </xsl:template> <xsl:template match="text()" /> |
<html> <body> <p>sam</p> <p>pete</p> </body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 21 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="b"> <p> <xsl:value-of select="@cat" /> = <xsl:value-of select="." /> </p> </xsl:template> <xsl:template match="text()" /> |
<html> <body> <p>sam = My Cat</p> <p> = B2</p> </body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 22 |
<a> <b cat="sam">My Cat</b> <b>B2</b> <d>Your Dog</d> <c> <d dog="pete">My Dog</d> </c> </a>
XSLT |
Result |
<xsl:template match="/"> <html><body> <xsl:apply-templates /> </body></html> </xsl:template> <xsl:template match="b"> <xsl:if test="@cat"> <p> <xsl:value-of select="@cat" /> = <xsl:value-of select="." /> </p> </xsl:if> </xsl:template> <xsl:template match="text()" /> |
<html> <body> <p>sam = My Cat</p> </body> </html> |
CS 683 Fall 04 | Doc 9, XSLT Slide # 23 |
Example from w3schools xsl on-line tutorial
http://www.w3schools.com/xsl/cdcatalog.xml
<?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> <cd> <title>Hide your heart</title> <artist>Bonnie Tyler</artist> <country>UK</country> <company>CBS Records</company> <price>9.90</price> <year>1988</year> </cd> <cd> <title>Greatest Hits</title> <artist>Dolly Parton</artist> <country>USA</country> <company>RCA</company> <price>9.90</price> <year>1982</year> </cd> </catalog>
CS 683 Fall 04 Doc 9, XSLT Slide # 24
<xsl:template match="/"> <html> <body> <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>
Example from w3schools xsl on-line tutorial
CS 683 Fall 04 | Doc 9, XSLT Slide # 25 |
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <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> </body> </html> </xsl:template> </xsl:stylesheet>
Example from w3schools xsl on-line tutorial
CS 683 Fall 04 | Doc 9, XSLT Slide # 26 |
<html> <body> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <tr> <td>Empire Burlesque</td> <td>Bob Dylan</td> </tr> <tr> <td>Hide your heart</td> <td>Bonnie Tyler</td> </tr> <tr> <td>Greatest Hits</td> <td>Dolly Parton</td> </tr> </table> </body> </html>
CS 683 Fall 04 | Doc 9, XSLT Slide # 27 |
<xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <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> </body> </html> </xsl:template>
Example from w3schools xsl on-line tutorial
CS 683 Fall 04 | Doc 9, XSLT Slide # 28 |
<xsl:template match="/"> <html> <body> <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> </body> </html> </xsl:template>
Example from w3schools xsl on-line tutorial
CS 683 Fall 04 | Doc 9, XSLT Slide # 29 |
<xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <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> </body> </html> </xsl:template>
Example from w3schools xsl on-line tutorial
CS 683 Fall 04 | Doc 9, XSLT Slide # 30 |
<xsl:template match="/"> <html> <body> <table border="1"> <tr bgcolor="#9acd32"> <th>Number</th> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:sort select="artist"/> <tr> <td> <xsl:number value="position()"/> </td> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.