<xsl:choose>

XSLT Function - xsl:choose

The XSLT <xsl:choose> element provides a complex conditional test to use against the contents of an XML file.

<xsl:choose>, together with <xsl:when> and <xsl:otherwise> are used when you want to select one of a sequence of alternatives.

<xsl:choose> contains one or more <xsl:when> elements, each of which has a test condition. The contents of the first <xsl:when> element that is matched are evaluated for output.

If none of the <xsl:when> elements match, then the contents of the <xsl:otherwise> element is evaluated for output; or if no <xsl:otherwise> element is present, then nothing is output.

If a simple Boolean evaluation is required, then use <xsl:if> instead.


 

Syntax

<xsl:choose>
<xsl:when test="expression_1">
<!-- evaluated when expression_1 is true. -->
</xsl:when>
<xsl:when test="expression_2">
<!-- evaluated when expression_2 is true. -->
</xsl:when>
<!-- ... -->
<xsl:when test="expression_n">
<!-- evaluated when expression_n is true. -->
</xsl:when>
<xsl:otherwise>
<!-- evaluated when none of the preceding expressions are true.-->
</xsl:otherwise>
</xsl:choose>

<xsl:choose> contains one or more nested <xsl:when> elements and one optional <xsl:otherwise> element. Only the contents one <xsl:when> element is evaluated when its corresponding expression returns true; otherwise the <xsl:otherwise> content is evaluated if defined.

Elements

The <xsl:choose> construct supports the following elements as children:

Attributes

There are no attributes associated with the <xsl:choose> element.

Examples

For our examples we are using the fictional library as an input XML. Any XSL transform examples we define uses this XML as an input.

<?xml version="1.0" encoding="UTF-8"?>
<library>
<category name="dogs">
<book>
<name>All about dogs</name>
<author>Someone</author>
<isin>true</isin>
<daysuntilreturn>0</daysuntilreturn>
</book>
</category>
<category name="cats">
<book>
<name>All about cats</name>
<author>Someone</author>
<isin>false</isin>
<daysuntilreturn>3</daysuntilreturn>
</book>
</category>
</library>

How to process multiple conditional statements

The following XSLT transform shows how to evaluate multiple conditional statements and then process only the first matching condition. If no match is found then the otherwise is processed.

   1:  

   2: <?xml version="1.0" encoding="UTF-8"?>

   3: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   4:     <xsl:template match="category">

   5:         <xsl:choose>

   6:             <xsl:when test="starts-with(./@name, 'c')">Found a category starting with C: <xsl:value-of select="./@name"/>.</xsl:when>

   7:             <xsl:when test="starts-with(./@name, 'k')">Found a category starting with K: <xsl:value-of select="./@name"/>.</xsl:when>

   8:             <xsl:otherwise>Could not match for: <xsl:value-of select="./@name"/>.</xsl:otherwise>

   9:         </xsl:choose>

  10:     </xsl:template>

  11: </xsl:stylesheet>

The example transform evaluates each category name, and if the category name starts with an indicated letter, then output is provided for that match. If no match is found then the contents of the otherwise is outputted.

The result of the above is:

   1: <?xml version="1.0" encoding="UTF-8"?>

   2: Could not match for: dogs

   3: Found a category starting with C: cats

Posted in XSLT and tagged .

Leave a Reply