Latest web development tutorials

XSLT <xsl: choose> element

<Xsl: choose> element is used in conjunction <xsl: when> and <xsl: otherwise> to express multiple conditional tests.


<Xsl: choose> element

grammar

<xsl:choose>
<xsl:when test="expression">
... some output ...
</xsl:when>
<xsl:otherwise>
... some output ....
</xsl:otherwise>
</xsl:choose>


Place where the selection criteria

To insert the XML file for multiple test conditions, add to the XSL file <xsl: choose>, <xsl: when> and <xsl: otherwise> element:

Examples

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="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 &gt; 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>

try it"

The above code will be higher than the price of the CD 10 columns add a pink background color to "Artist".


Another example

This is another contains two <xsl: when> instance element:

Examples

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="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 &gt; 10">

<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:when test="price &gt; 9">

<td bgcolor="#cccccc">
<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>

try it"

The above code is higher than 10 to "Artist" column to add a pink background color in the price of the CD, and more than 9 and less than the price of the CD is equal to 10 to "Artist" column to add a gray background color.