Working with xsl:choose in XSLT - Working with xsl:choose in XSLT: explanation
(Page 3 of 6 )
Let us understand the code step by step. Let us start with the following:
<xsl:template match="/">
<table border="1">
<xsl:apply-templates select="SQLData/Rows" />
</table>
</xsl:template>
From the above code fragment, you can easily understand that I am emitting a TABLE tag and the content of the TABLE tag is based on the template defined in each and every “SQLData/Rows.” The template for “SQLData/Rows” is defined as follows:
<xsl:template match="SQLData/Rows">
<tr>
<td>
<xsl:value-of select="Empno"/>
</td>
<td>
<xsl:value-of select="Ename"/>
</td>
<td>
<xsl:apply-templates select="Sal" />
</td>
<td>
<xsl:value-of select="Deptno"/>
</td>
</tr>
</xsl:template>
According to the above, you can understand that I am emitting a TR tag for every “SQLData/Rows.” Within the TR, I am also emitting a TD tag along with the content available in leaf nodes (like Empno, Ename etc.). The only exception is the following TD emission:
<td>
<xsl:apply-templates select="Sal" />
</td>
The above emits a TD tag but the content is based on the template defined separately for the “Sal” node. The template for the “Sal” node is defined as follows:
<xsl:template match="Sal">
<xsl:choose>
<xsl:when test="text() > '3500'">
<b>
<xsl:value-of select="."/>
</b>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The above template may be necessary as we need to test for a condition on “Sal.” From the above code fragment, you can understand that a single “xsl:choose” can be embedded with any number of conditions using “xsl:when.” The “text()” is nothing but the content of “Sal” (in this case).
The text gets compared with a value of 3500 and emits the text with bold tags if the condition evaluates to true. If the condition evaluates to false, “xsl:otherwise” gets executed and it emits only the content (without any bold tags).
Next: Emitting HTML together with CSS using XSLT >>
More ASP.NET Articles
More By Jagadish Chaterjee