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.



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

<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.


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


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


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"?>
<category name="dogs">
<name>All about dogs</name>
<category name="cats">
<name>All about cats</name>

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.


   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