XML Fundamentals Lecture Notes Week 11
Lets continue talking about XSLT
The <xsl:for-each> Element
The XSL <xsl:for-each> element can be used to select every XML element of a
specified node set.
<?xml version=1.0 encoding=ISO-8859-1?>
<xsl:stylesheet version=1.0
xmlns:xsi=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>
The result of the transformation will look something like this (minus the borders and
color):
My CD Collection
Title
Empire Burlesque
Hide your heart
And so on
Artist
Bob Dylan
Bonnie Tyler
Now lets Filter the Output
We can filter the output from an XML file by adding a criterion to the select attribute in
the <xsl:for-each> element:
<xsl:for-each select=catalog/cd[artist=Bob Dylan]>
Legal filter operators are:
-
= (equal)
!= (not equal)
< (less than)
> (greater than)
Take a look at this adjusted XSL style sheet:
<?xml version=1.0 encoding=ISO-8859-1?>
<xsl:stylesheet version=1.0
xmlns:xsi=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>
The result will now look like this (minus the borders and color):
:
My CD Collection
Title
Empire Burlesque
Artist
Bob Dylan
The <xsl:sort> Element
The <xsl:sort> element is used to sort the output.
To output the XML file as an XHTML file, and sort it at the same time, simply add a sort
element inside the for-each element in your XSL file:
<?xml version=1.0 encoding=ISO-8859-1?>
<xsl:stylesheet version=1.0
xmlns:xsi=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>
The select attribute indicates what XML element to sort on.
The output will now be sorted by artist.
The <xsl:if> Element
The <xsl:if> element contains a template that will be applied only if a specified
condition is true.
To put a conditional if test against the content of the file, simply add an <xsl:if> element
to your XSL document like this:
<xsl:if test=price > 10>
some output
</xsl:if>
The value of the required test attribute contains the expression to be evaluated>
<?xml version=1.0 encoding=ISO-8859-1?>
<xsl:stylesheet version=1.0
xmlns:xsi=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:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The code above only selects the title and artist IF the price of the cd is higher than 10.
The <xsl:choose> Element
The <xsl:choose> element is used in conjunction with <xsl:when> and
<xsl:otherwise> to express multiple conditional tests.
To insert a conditional choose test against the content of the XML file, simply add the
<xsl:choose:, <xsl:when>, and <xsl:otherwise> elements to your XSL document like
this:
<xsl:choose>
<xsl:when test=price > 10>
some code
</xsl:when>
<xsl:otherwise>
some code
</xsl:otherwise>
</xsl:choose>
Look at this adjusted XSL style sheet:
<?xml version=1.0 encoding=ISO-8859-1?>
<xsl:stylesheet version=1.0
xmlns:xsi=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>
<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>
The above code will add a pink background color to the artist column WHEN the price of
the cd is higher than 10.
There is much, much more to cover on the topic of XSL, but I think we have covered
enough of the basics to be able to use it, so I will end this topic here.