How to use conditional statements in XSL

XSL Conditional StatementsXSL provides support for conditional patterns, statements and values – much the same as any other programming language. The appropriate application of the available XSL functions allows you to implement each pattern.

You can execute an XSL transform with simple to more complex conditional patterns:

  • IF-THEN
  • IF-THEN-ELSE
  • SWITCH-CASE


The use of conditional patterns and values allow for the correct processing of problem cases. For reference refer to the following XSL functions and topics:

IF-THEN Pattern

The IF-THEN pattern is the simplest: Only if a certain condition is met, then only will the enclosed code execute. The XSL specification provides the <xsl:if> function to implement the IF-pattern.

   1:  

   2: <xsl:if test="expression>

   3: <!-- Executes the THEN part of the pattern here-->

   4: </xsl:if>

Where expression defines the conditional evaluation – in other words – the IF part of the pattern. The expression must result in a true-value in order for the THEN pattern to execute. The expression is defined as an XPATH function or statement.

IF-THEN-ELSE Pattern

The IF-THEN-ELSE pattern is one of the more recognizable programming constructs. The XSL specification provides the <xsl:choose> function to implement this pattern.

NOTE: The use of this function is counter-intuitive; you would expect the use <xsl:if> function: however, the <xsl:if> function does not support the ELSE pattern.

<xsl:choose> – and the related <xsl:when> and <xsl:otherwise> functions – supports the IF -THEN-ELSE pattern in the following manner:

   1: <xsl:choose>

   2:     <xsl:when test="expression">

   3:     <!-- Executes the THEN part of the pattern here-->

   4:     </xsl:when>

   5:     <xsl:otherwise>

   6:     <!-- Executes the ELSE part of the pattern here-->

   7:     </xsl:otherwise>

   8: </xsl:choose>

Where expression defines the conditional evaluation – in other words – the IF part of the pattern. The expression must result in a true-value in order for the THEN pattern to execute. The expression is defined as an XPATH function or statement.

If the expression does not evaluate to true, i.e. it is false. then the <xsl:otherwise> part of the construct executes – this is the ELSE-pattern.

SWITCH-CASE Pattern

The SWITCH-CASE pattern allows for the use of more complex conditional evaluations and then executing the first match. The ELSE-pattern is supported if none of the preceding evaluations match. To implement this pattern use the <xsl:choose> and related <xsl:when> and <xsl:otherwise> functions.

   1: <xsl:choose>

   2:     <xsl:when test="expression1">

   3:     <!-- Executes the CASE 1 here-->

   4:     </xsl:when>

   5:     <xsl:when test="expression2">

   6:     <!-- Executes the CASE 2 here-->

   7:     </xsl:when>

   8:     <!-- ... add when-statements for each applicable case -->

   9:     <xsl:when test="expressionN">

  10:     <!-- Executes the CASE N here-->

  11:     </xsl:when>

  12:     <xsl:otherwise>

  13:     <!-- Executes the ELSE part of the pattern here. This is the DEFAULT case.-->

  14:     </xsl:otherwise>

  15: </xsl:choose>

Posted in XSLT and tagged .

Leave a Reply