Trademark Logo XSLTC Design
<xsl:choose> / <xsl:when> / <xsl:otherwise>
Apache Foundation Xalan Project Xerces Project Web Consortium Oasis Open

<xsl:choose> / <xsl:when> / <xsl:otherwise>

(top)

Contents

(top)

Functionality

The <xsl:choose> element is used to determine one course of action based on a series of tests. Each test is done inside an <xsl:when> element. If a test succeeds, the body of the <xsl:when> element is executed. If no tests fail then a <xsl:otherwise> element can be used to specify a default action:

    <xsl:choose>
      <xsl:when test="element-available('some-extension')">
          ...
      </xsl:when>
      <xsl:when test="function-availabe('saxon:nodeset')">
          ...
      </xsl:when>
      <xsl:otherwise>
          ...
      </xsl:otherwise>
    </xsl:choose>

(top)

Implementation

The Choose class places all When child-nodes in a vector. The Choose class translates the "test"-attribute of all When nodes (in strict order) and chains them together in an if-else style. The expression that holds each test contains a true- and a false-list. These lists are vectors of branch targets that should be used if the test succeeds or fails, respectively. The first test's false-list is pointed to the start of the next test (ie. if the first test fails, then we run the next test). The last test's false-list points directly to the code for the body of the <xsl:otherwise> element.

Just as with the <xsl:if>-element, special care is taken for the element-available() and function-available() functions. These functions are evaluated at compile-time (this can be done since all parameters for these functions are literals) and the body of a <xsl:when> element is not compiled if we know that it will never be needed at runtime.

(top)