EXtensible StyleSheet Language - Part 2
In our previous article:
http://dotnetspider.com/resources/43273-XSLT-eXtensible-StyleSheet-Language.aspx
we have seen the introduction to XSL and XSLT. We have seen some of the keywords used in XSLT such as <template>,<value-of>,<for-each>. In this article we are going to see few more keywords used in the XSLT documents such as: <sort>,<if>,<choose>,Apply
Learn about EXtensible StyleSheet Language - Part 2
Referring back to previous Employees xml file and the XSLT file created in the previous article, let us see how to perform sort using XSLT: We use the <xsl:sort> element to perform sort. To specify which xml element to use to perform sort we use "select" as shown below:
Here we are sorting based on "Name"
<xsl:template match="/">
<html>
<body>
<h2>Employee Information</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Name</th>
<th>City</th>
</tr>
<xsl:for-each select="Employees/Employee">
<xsl:sort select="Name"/>
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="City"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
We have added an <xsl:sort select="Name"/> element inside the <xsl:for-each> element in the XSLT file to perform sort.
<xsl:if>
The <xsl:if> element is used to put a conditional test against the content of the XML file. Add the <xsl:for-each> element in the XSLT file as shown below. The value of the required test attribute contains the expression to be evaluated. The result contains only those details which have city as Hyderabad.
<xsl:for-each select="Employees/Employee">
<xsl:if test="City = 'Hyderabad'">
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:value-of select="City"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
<xsl:choose>:
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests. In the below code example we are highlighting the City 'Hyderabad' with a BackGround color and other City Names are shown as it is.
<xsl:for-each select="Employees/Employee">
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<xsl:choose>
<xsl:when test="City = 'Hyderabad'">
<td bgcolor="#ff00ff"> <xsl:value-of select="City"/>
</td>
</xsl:when>
<xsl:otherwise>
<td>
<xsl:value-of select="City"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
The <xsl:apply-templates> element applies a template 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 element that matches the value of the attribute. We can use the select attribute to specify the order in which the child nodes are processed. In the below example we are applying template to the City and Name elements. We are processing first the Name elements then the City elements.
<xsl:template match="/">
<html>
<body>
<h2>Employee Information</h2>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="Employee">
<p>
<xsl:apply-templates select="City"/>
<xsl:apply-templates select="Name"/>
</p>
</xsl:template>
<xsl:template match="Name">
Name: <span style="color:#ff0000">
<xsl:value-of select="."/>
</span>
<br />
</xsl:template>
<xsl:template match="City">
City: <span style="color:#00ff00">
<xsl:value-of select="."/>
</span>
<br />
</xsl:template>
</xsl:stylesheet>