xsl:choose Element
A version of this page is also available for
4/8/2010
Provides multiple conditional testing in conjunction with the <xsl:otherwise> and <xsl:when> elements.
Syntax
<xsl:choose>
</xsl:choose>
Attributes
None.
Element Information
Number of occurrences |
Unlimited |
Parent elements |
xsl:attribute, xsl:comment, xsl:copy, xsl:element, xsl:for-each, xsl:if, xsl:otherwise, xsl:param, xsl:processing-instruction, xsl:template, xsl:variable, xsl:when, xsl:with-param, output elements |
Child elements |
Remarks
The <xsl:when> children of the <xsl:choose> element are tested in order from top to bottom until a test attribute on one of these elements accurately describes conditions present in the source data or an <xsl:otherwise> element is reached. Once an <xsl:when> or <xsl:otherwise> element is chosen, the <xsl:choose> block is exited. No explicit break or exit statement is required.
For simple conditional testing, use the <xsl:if> element.
Example
This example shows a template for "order" elements and inserts an <HR>
or <BR>
before the order's contents, based on the order's "total" element value. If the total is less than 10, a red <HR>
will be generated; if the total is less than 20, a pink <HR>
will be generated; otherwise a <BR>
element will be created.
<xsl:template match="order">
<xsl:choose>
<xsl:when test="total < 10">
<HR STYLE="color:red"/>
</xsl:when>
<xsl:when test="total < 20">
<HR STYLE="color:pink"/>
</xsl:when>
<xsl:otherwise>
<BR/>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates />
</xsl:template>