IPT Chapter 4
IPT Chapter 4
Chapter 4
1 ||: By Beya
Integrative programming and technologies
2 ||: By Beya
Integrative programming and technologies
<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>
Example Explained
− Since an XSL style sheet is an XML document, it always begins with the
XML declaration: <?xml version="1.0" encoding="UTF-8"?>.
− The next element, <xsl:stylesheet>, defines that this document is an XSLT
style sheet document (along with the version number and XSLT
namespace attributes).
− The <xsl:template> element defines a template. The match="/" attribute
associates the template with the root of the XML source document.
− The content inside the <xsl:template> element defines some HTML to write
to the output.
− The last two lines define the end of the template and the end of the style
sheet.
Note: The select attribute, in the example above, contains an XPath expression.
An XPath expression works like navigating a file system; a forward slash (/)
selects subdirectories.
3 ||: By Beya
Integrative programming and technologies
• To add a conditional test, add the <xsl:if> element inside the <xsl:for-
each> element in the XSL file instead of <xsl:sort> element in the above
example :
Example
<?xml version="1.0" encoding="UTF-8"?>
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="price > 10">
<tr>
4 ||: By Beya
Integrative programming and technologies
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Note: The value of the required test attribute contains the expression to be
evaluated. The code above will only output the title and artist elements of the
CDs that has a price that is higher than 10.
Syntax
<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>
• To insert a multiple conditional test against the XML file, add the
<xsl:choose>, <xsl:when>, and <xsl:otherwise> elements to the XSL file:
Example
<?xml version="1.0" encoding="UTF-8"?>
5 ||: By Beya
Integrative programming and technologies
<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 code above will add a pink background-color to the "Artist" column WHEN
the price of the CD is higher than 10.
6 ||: By Beya
Integrative programming and technologies
Example
<?xml version="1.0" encoding="UTF-8"?>
<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">
7 ||: By Beya
Integrative programming and technologies
</xsl:stylesheet>
<state>
<name>California</name>
<population units="people">33871648</population><!--2000 census--
>
<capital>Sacramento</capital>
<bird>Quail</bird>
<flower>Golden Poppy</flower>
<area units="square miles">155959</area>
</state>
<state>
<name>Massachusetts</name>
<population units="people">6349097</population><!--2000 census-->
<capital>Boston</capital>
<bird>Chickadee</bird>
<flower>Mayflower</flower>
<area units="square miles">7840</area>
</state>
8 ||: By Beya
Integrative programming and technologies
<state>
<name>New York</name>
<population units="people">18976457</population><!--2000 census--
>
<capital>Albany</capital>
<bird>Bluebird</bird>
<flower>Rose</flower>
<area units="square miles">47214</area>
</state>
</states>
<xsl:template match="/states">
<HTML>
<HEAD>
<TITLE>
State Data
</TITLE>
</HEAD>
<BODY>
<H1>
State Data
</H1>
<TABLE BORDER="1">
<TR>
<TD>Name</TD>
<TD>Population</TD>
<TD>Capital</TD>
9 ||: By Beya
Integrative programming and technologies
<TD>Bird</TD>
<TD>Flower</TD>
<TD>Area</TD>
</TR>
<xsl:apply-templates/>
</TABLE>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="state">
<TR>
<TD><xsl:value-of select="name"/></TD>
<TD><xsl:apply-templates select="population"/></TD>
<TD><xsl:apply-templates select="capital"/></TD>
<TD><xsl:apply-templates select="bird"/></TD>
<TD><xsl:apply-templates select="flower"/></TD>
<TD><xsl:apply-templates select="area"/></TD>
</TR>
</xsl:template>
<xsl:template match="population">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@units"/>
</xsl:template>
<xsl:template match="capital">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="bird">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="flower">
10 ||: By Beya
Integrative programming and technologies
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="area">
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
<xsl:value-of select="@units"/>
</xsl:template>
</xsl:stylesheet>
Note the expression "." here. You use "." with the select attribute to specify the
current node. You can handle attributes very much like you handle elements. All
that's different is that you have to preface the attribute name with @. For example,
say that you want to recover the value of the units attributes in the <population>
and <area> elements of the XML example. To get the values of the units
attribute, you simply need to refer to it as @units.
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>
State Data
</TITLE>
</HEAD>
<BODY>
<H1>
State Data
</H1>
<TABLE BORDER="1">
<TR>
<TD>Name</TD>
<TD>Population</TD>
<TD>Capital</TD>
11 ||: By Beya
Integrative programming and technologies
<TD>Bird</TD>
<TD>Flower</TD>
<TD>Area</TD>
</TR>
<TR>
<TD>California</TD>
<TD>33871648 people</TD>
<TD>Sacramento</TD>
<TD>Quail</TD>
<TD>Golden Poppy</TD>
<TD>155959 square miles</TD>
</TR>
<TR>
<TD>Massachusetts</TD>
<TD>6349097 people</TD>
<TD>Boston</TD>
<TD>Chickadee</TD>
<TD>Mayflower</TD>
<TD>7840 square miles</TD>
</TR>
<TR>
<TD>New York</TD>
<TD>18976457 people</TD>
<TD>Albany</TD>
<TD>Bluebird</TD>
<TD>Rose</TD>
<TD>47214 square miles</TD>
</TR>
</TABLE>
</BODY>
</HTML>
12 ||: By Beya