You are on page 1of 27

XSLT

eXtensible Stylesheet Language Transformations


CSS vs XSL
CSS = Style Sheets for HTML XSL = Style Sheets for XML

● predefined tags ● does not use predefined tags


● each tag is well understood ● each tag is not well understood
● browser knows how to display it ● browser does not know how to
display it

● XSL describes how the XML document should be displayed


● XSL - More Than a Style Sheet Language
XSL - More Than a Style Sheet Language

● XSLT - a language for transforming XML documents

● XPath - a language for navigating in XML documents

● XSL-FO - a language for formatting XML documents


What is XSLT?
● XSLT stands for XSL Transformations

● XSLT is the most important part of XSL

● XSLT transforms an XML document into another XML document

● XSLT uses XPath to navigate in XML documents

● XSLT is a W3C Recommendation


XSLT = XSL Transformations
● XSLT is far more sophisticated than CSS
● Add/remove, rearrange, sort, test elements
in output document

XSLT uses XPath to find information in an XML


document
How to transform...
● Correct Style Sheet Declaration
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

or
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

● Declare the XSLT namespace to get access to XSLT elements and


attributes

● Include the attribute version="1.0"


The Basic Structure of XSLT
The root element for an XSLT file is the <xsl:stylesheet> element.
(Note the xsl namespace.)

<?xml version="1.0" encoding="UTF-8" ?>


<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
… XSLT syntax goes here …
</xsl:stylesheet>
A Quick Refresher on
Namespaces

XSLT uses the xsl namespace, which is specified via:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

• Note that the xsl namespace prefix is used. It’s


namespace value must be the specific URL you see
above…
The Components of XSLT

• XSLT is expressed as an XML-formatted document.


• There are a number of common XSLT elements that specify how the XML data should
be transformed:

<xsl:template>
<xsl:for-each>
<xsl:value-of>
Start with a Raw XML Document
Source xml (cdcatalog.xml):
<?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>
Start with a Raw XML Document
XSL Style Sheet (cdcatalog.xsl):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<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>
</xsl:template>
</xsl:stylesheet>
Start with a Raw XML Document
Link the XSL Style Sheet to the XML Document
<?xml version="1.0" encoding ="UTF-8" ?>
<?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>
Start with a Raw XML Document
Result is:
Examining <xsl:template>

• An XSLT document can contain an arbitrary number of <xsl:template>


elements.
• Each <xsl:template> element must include a match attribute, whose
value is an XPath expression that indicates what XML nodes the
<xsl:template> tag will work with.
XSLT <xsl:template> Element
<?xml version="1.0" encoding=" UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>

● XSL style sheet consists of one or <xsl:template match="/">


<html>
more set of rules that are called <body>
templates. <h2>My CD Collection</h2>
● The <xsl:template> element defines a <table border="1">
<tr bgcolor="#9acd32">
template
<th>Title</th>
● The match="/" attribute associates the <th>Artist</th>
template with the root </tr>
<tr>
<td>.</td>
<td>.</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Examining <xsl:value-of>

• The <xsl:value-of> element accesses the text or attribute value of an


element.
• The <xsl:value-of> element requires a select attribute, which specifies
the name of the element whose text node to retrieve, or the attribute
name.
XSLT <xsl:value-of> Element
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
● Is used to extract the value of a <xsl:template match="/">
selected node. <h2>My CD Collection</h2>
● used to extract the value of an XML <table border="1">
element and add it to the output <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>
</xsl:template>
Examining <xsl:for-each>

• The <xsl:for-each> element iterates through all of the children elements


of a specified element. The <xsl:for-each> element has a select attribute
(like <xsl:value-of>), which specifies which element’s children to iterate
through.
XSLT <xsl:for-each> Element
● The <xsl:for-each> element allows
<xsl:template match="/"> you to do looping in XSLT.
<h2>My CD Collection</h2> ● used to select every XML element of
<table border="1"> a specified node-set
<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>
</html>
<xsl:template />
XSLT <xsl:for-each> Element
<xsl:template match="/"> Filtering the Output
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32"> Legal filter operators are:
<th>Title</th> ● = (equal)
<th>Artist</th> ● != (not equal)
</tr> ● > (less than)
<xsl:for-each select="catalog/cd[artist=’Bob ● < (greater than)
Dylan’]">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</html>
<xsl:template />
XSLT <xsl:sort> Element
● The <xsl:sort> element is used to sort
the output.
● To sort the output, simply add the
element inside the for-each element.

<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>
XSLT <xsl:if> Element
Syntax
<xsl:if test="expression">
...some output if the expression is true...
</xsl:if>

Example:
<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>
XSLT <xsl:choose> Element
Syntax Example
<xsl:choose> <xsl:for-each select="catalog/cd">
<xsl:when test="expression"> <tr>
... some output ... <td><xsl:value-of select="title"/></td>
</xsl:when> <xsl:choose>
<xsl:otherwise> <xsl:when test="price > 10">
... some output .... <td bgcolor="#ff00ff">
</xsl:otherwise> <xsl:value-of select="artist"/></td>
</xsl:choose> </xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
● Specifies a multiple conditional tests </xsl:otherwise>
against the content of nodes in </xsl:choose>
conjunction. </tr>
</xsl:for-each>
XSLT <xsl:choose> Element
Example
<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>
XSLT <xsl:apply-templates> Element

● The <xsl:apply-templates> element applies a template rule to the current


element or to the current element's child nodes.

● If we add a "select" attribute to the <xsl:apply-templates> element, it will


process only the child elements that matches the value of the attribute.
XSLT <xsl:apply-templates> Element
<?xml version="1.0" encoding="UTF-8"?> ...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:template match="/"> <xsl:value-of select="."/></span>
<html> <br />
<body> </xsl:template>
<h2>My CD Collection</h2>
<xsl:apply-templates/> <xsl:template match="artist">
</body> Artist: <span style="color:#00ff00">
</html> <xsl:value-of select="."/></span>
</xsl:template> <br />
</xsl:template>
<xsl:template match="cd">
<p> </xsl:stylesheet>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
...
XSLT <xsl:apply-templates> Element
<?xml version="1.0" encoding="UTF-8"?> ...
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="title">
Title: <span style="color:#ff0000">
<xsl:template match="/"> <xsl:value-of select="."/></span>
<html> <br />
<body> </xsl:template>
<h2>My CD Collection</h2>
<xsl:apply-templates/> <xsl:template match="artist">
</body> Artist: <span style="color:#00ff00">
</html> <xsl:value-of select="."/></span>
</xsl:template> <br />
</xsl:template>
<xsl:template match="cd">
<p> </xsl:stylesheet>
<xsl:apply-templates select="title"/>
<xsl:apply-templates select="artist"/>
</p>
</xsl:template>
...

You might also like