XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / choose

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 6.)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

 

The instruction xsl:choose selects from a group of template instructions the one whose condition, set with xsl:when, holds. If none holds, the sequence constructor of optional xsl:otherwise runs. Without it, nothing happens.

Classification

Instruction

Function group

Conditional processing

Introduced

XSLT 1.0

Position in the stylesheet and permitted contents:

xsl:choose is an instruction usable only directly or indirectly inside a template instruction block xsl:template.

Contents may include one or more xsl:when elements and at most one final xsl:otherwise element. Literal Result Elements are not allowed directly in xsl:choose.

Attributes:

Only standard attributes apply; no element-specific attributes exist.

Purpose:

An xsl:choose element is never empty. It contains at least one xsl:when element or several consecutive xsl:when elements. Each xsl:when has a required attribute test containing an XPath expression evaluating to either true or false. Only the sequence constructor of the first xsl:when instruction whose test condition holds is executed. (Note: Unlike earlier versions, the specification explicitly says that subsequent satisfied xsl:when are not evaluated.)

After the xsl:when elements, at most one xsl:otherwise element provides the default sequence constructor when no preceding xsl:when condition holds. The xsl:other­wise element needs no condition and represents ELSE in IF-ELSE. If no xsl:when condition holds and no xsl:otherwise element exists, the entire block xsl:choose has no effect.

Note – Alternative xsl:if for simple conditions
An xsl:choose element with one xsl:when condition has exactly the effect of an xsl:if instruction. Unlike that instruction, a condition made with xsl:choose can be extended, resembling IF-ELSE-IF or SWITCH in other languages.

Examples:

Example 1 – Testing an attribute value:

<xsl:choose>
  <xsl:when test="kennzeichen='PM'">Potsdam Mittelmark</xsl:when>
  <xsl:when test="kennzeichen='MOL'">Märkisch Oberland</xsl:when>
  <xsl:when test="kennzeichen='B'">Berlin</xsl:when>
  ...
  <xsl:otherwise>
    <xsl:value-of select="kennzeichen"/>
  </xsl:otherwise>
</xsl:choose>

The example outputs a town or district name based on its vehicle-registration abbreviation. If unrecognized, the abbreviation itself is output.

Example 2 – xsl:choose inside xsl:variable:

<xsl:variable name="breite">
  <xsl:choose>
    <xsl:when test="@breite">
      <xsl:value-of select="@breite"/>
    </xsl:when>
    <xsl:otherwise>100</xsl:otherwise>
  </xsl:choose> 
</xsl:variable>

The second example declares variable breite. Inside the xsl:choose block, a xsl:when test checks whether the current node has a breite attribute. If so, its value is assigned to variable breite. Otherwise, xsl:otherwise assigns the variable default value 100.

Element definition:

XSLT 1.0:

<!-- Category: instruction -->
<xsl:choose>
     <!-- Content: (xsl:when+, xsl:otherwise?) -->
</xsl:choose>

XSLT 2.0:

<!-- Category: instruction -->
<xsl:choose>
     <!-- Content: (xsl:when+, xsl:otherwise?) -->
</xsl:choose>

   

  

back to top