xslt xslt: extensible stylesheet language for transformations - a language for transforming xml...
TRANSCRIPT
![Page 1: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/1.jpg)
XSLT
XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text-based format (e.g. HTML, plain text, another XML, etc.)
- Why do we need to learn it? - A typical work flow: legacy data source > XML > XSL transformation > application/Web browser
XPath - a language for navigating in XML documents (XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents)
XSL-FO - a language for formatting XML documents
![Page 2: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/2.jpg)
How Does It Work:
XSLT stylesheet
XML documentXSLT
processorTransformed XML/HTML
document
An XSLT processor can be built into a web browser, or can be a standalone program run from the command (e.g. SAXON or Apache Xalan)
![Page 3: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/3.jpg)
Example source XML file:
http://www.w3schools.com/xsl/cdcatalog.xml
XSLT stylesheet:
http://www.w3schools.com/xsl/cdcatalog.xsl
Link the XSL Style Sheet to the XML Document:
http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml
Result XML file:
http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
![Page 4: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/4.jpg)
Example source xml file:Example source xml file:
http://www.w3schools.com/xsl/cdcatalog.xml
XSLT stylesheet:
http://www.w3schools.com/xsl/cdcatalog.xsl
Link the XSL Style Sheet to the XML Document:
http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml
Result XML file:
http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>
![Page 5: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/5.jpg)
Example source xml file:http://www.w3schools.com/xsl/cdcatalog.xmlXSLT stylesheet:XSLT stylesheet:http://www.w3schools.com/xsl/cdcatalog.xslLink the XSL Style Sheet to the XML Document:http://www.w3schools.com/xsl/cdcatalog_with_xsl.xmlResult XML file:http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<?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> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">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>
![Page 6: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/6.jpg)
Example source xml file:http://www.w3schools.com/xsl/cdcatalog.xmlXSLT stylesheet:http://www.w3schools.com/xsl/cdcatalog.xslLink the XSL Style Sheet to the XML Document:Link the XSL Style Sheet to the XML Document:http://www.w3schools.com/xsl/cdcatalog_with_xsl.xmlResult XML file:http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type=“text/xsl" href="cdcatalog.xsl"?>
<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>
![Page 7: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/7.jpg)
Example source XML file:
http://www.w3schools.com/xsl/cdcatalog.xml
XSLT stylesheet:
http://www.w3schools.com/xsl/cdcatalog.xsl
Link the XSL Style Sheet to the XML Document:
http://www.w3schools.com/xsl/cdcatalog_with_xsl.xml
Result XML file:Result XML file:
http://www.w3schools.com/xsl/cdcatalog_with_ex1.xml
<html><body> <h2>My CD Collection</h2> <table border="1"><tbody> <tr bgcolor="#9acd32"> <th align="left">Title</th> <th align="left">Artist</th> </tr> <tr><td>Empire Burlesque</td> <td>Bob Dylan</td> </tr> … </tbody></table></body></html>
![Page 8: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/8.jpg)
Template Rules
• An XSL style sheet consists of one or more set of rules that are called templatestemplates.
• Each templatetemplate rule tells the XSLT processor how to handle the specific elementselements in the input XML document.
![Page 9: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/9.jpg)
Template Rules (Example)
<xsl:template match=“/”> root element
<xsl:template match=“catalog/cd”> sub-element “cd” of “catalog”
</xsl:template>
…
<xsl:template match=“cd”>
title: <xsl:value-of select=“title”> value of element “title”
</xsl:template>
![Page 10: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/10.jpg)
Structure of a stylesheet
<xsl:stylesheet xmlns:xsl=“http://www.w3.org/1999/XSL/Transform” version=“1.0”> ……
<xsl:template match="pattern"> \ template > a template rule </xsl:template> / …… <- other templates></xsl:stylesheet>
![Page 11: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/11.jpg)
Processing Model
For a given input XML document, the output is obtained as follows: - The source tree is processed by processing the root node
- a node list is processed by processing each node in order and concatenating the results
- a single node is processed by: finding the template rule with the matching pattern, and creating result fragment
![Page 12: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/12.jpg)
<?xml version="1.0" encoding=“UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding=“UTF-8"?>
<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>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
![Page 13: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/13.jpg)
XSL:for-each <xsl:for-each select = “node-set-expression” </xsl:for-each>
XSL:if <xsl:if
test = “boolean-expression” </xsl:if>
XSL:sort <xsl:sort
select = “string-expression” data-type = “text” | “number” | “prefixedName” lang = “langcode” order = “ascending” | “descending” case-order = “upper-first” | “lower-first” />
![Page 14: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/14.jpg)
<?xml version="1.0" encoding=“UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template>
</xsl:stylesheet>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
Greatest Hits Dolly Parton
The very best of Cat Stevens
![Page 15: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/15.jpg)
<?xml version="1.0" encoding=“UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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[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>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
filter
![Page 16: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/16.jpg)
<?xml version="1.0" encoding=“UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
</xsl:stylesheet>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
The very best of Cat Stevens
Greatest Hits Dolly Parton
SortOn an element
![Page 17: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/17.jpg)
<?xml version="1.0" encoding=“UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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:if test="price > 10"> <tr> <td><xsl:value-of select="title"/></td> <td><xsl:value-of select="artist"/></td> </tr> </xsl:for-each> </xsl:if> </table> </body> </html> </xsl:template> </xsl:stylesheet>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
Greatest Hits Dolly Parton
if testinside <xsl:for-each>
![Page 18: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/18.jpg)
<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> </xsl:stylesheet>
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
The very best of Cat Stevens
Greatest Hits Dolly Parton
Multiple conditional test
![Page 19: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/19.jpg)
<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:when test="price > 9"> <td bgcolor="#cccccc"> <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> …
My CD Collection
Title Artist
Empire Burlesque Bob Dylan
The very best of Cat Stevens
Greatest Hits Dolly Parton
Multiple conditional test2
![Page 20: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/20.jpg)
XSL:apply-templates <xsl:apply-templates
select=“node-set-expression”
mode=“qualifiedName”>
<!-- (xsl:sort | xsl:with-param)* -->
</xsl:apply-templates>
Without xsl:sort child elements, the default is in document order;
xsl:with-param child elements can be used to pass parameter values to the matched templates
![Page 21: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/21.jpg)
<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>
<xsl:apply-templates>
My CD Collection
Title: Empire BurlesqueArtist: Bob Dylan
Title: Hide your heartArtist: Bonnie Tyler
Title: Greatest HitsArtist: Dolly Parton
![Page 22: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/22.jpg)
…
<xsl:template match=“person”> <xsl:apply-templates select=“name”/> Born: <xsl:apply-templates select=“@born”/> Died: <xsl:apply-templates select=“@died”/></xsl:template>…
Select attribute value in<xsl:apply-templates>
…Born: 1918Died: 1988…
![Page 23: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/23.jpg)
…
<xsl:template match=“name”> <name first=“{first_name}” initial=“{middle_initial}” last=“{last_name}” />/xsl:template>…
Attribute Value Templates
<name> <first_name>Richard</first_name> <middle_initial>P</middle_initial> <last_name>Feynman</last_name> </name>
<name first=“Richard” initial=“P” last=“”Feynman”/>
![Page 24: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/24.jpg)
Using oXygen XML Editor
![Page 25: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/25.jpg)
Other Issues in Practice
•XPath - a language for finding information in an XML document.
• http://www.w3schools.com/xpath/default.asp
• XPath functions
• Be aware of the “context”
• Namespace (e.g. “dc:author”)• http://www.ibm.com/developerworks/xml/library/x-nmspace.html
• http://www.ibm.com/developerworks/xml/library/x-nmspace2.html
![Page 26: XSLT XSLT: eXtensible Stylesheet Language for Transformations - a language for transforming XML documents into any text- based format (e.g. HTML, plain](https://reader034.vdocuments.us/reader034/viewer/2022052701/56649dbd5503460f94aaf3c1/html5/thumbnails/26.jpg)
References:• This PPT is based on XSLT Tutorial at:
http://www.w3schools.com/xsl/
• Zvon XSL Tutorial: http://www.zvon.org/xxl/XSLTutorial/Output/index.html
• XSLT reference: http://www.zvon.org/xxl/XSLTreference/Output/index.html
http://www.w3schools.com/xsl/xsl_w3celementref.asp