XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / otherwise
(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
Sub-instruction xsl:otherwise identifies the template block run inside an xsl:choose element as an alternative to its xsl:when instructions. It runs when every test attribute of the preceding xsl:when instructions is false, corresponding to a switch or select-case default branch.
Classification | Sub-instruction |
Function group | Conditional processing |
Introduced | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:otherwise is an optional final child of xsl:choose following its xsl:when sub-instructions. Each xsl:choose container permits at most one xsl:otherwise element. Its template body is a sequence constructor of text, Literal Result Elements and arbitrary XSLT instructions.
Attributes:
Standard attributes apply. xsl:otherwise has no element-specific attributes.
Purpose:
The instruction xsl:otherwise is an optional final instruction inside an xsl:choose element. The block in xsl:otherwise runs if and only if, in xsl:choose (the block ), no preceding xsl:when condition holds. Instruction xsl:otherwise needs no condition because it runs in all other cases.
Functionally, this substitutes for the nonexistent XSLT instruction “xsl:else”, see example 1. The surrounding container xsl:choose must represent IF-ELSE, with exactly one xsl:when condition for IF and one xsl:otherwise instruction for ELSE. Using xsl:otherwise with xsl:if is impossible.
If the xsl:choose container has multiple xsl:when instructions, it resembles select-case with xsl:otherwise as default, example 2.
After the first satisfied xsl:when condition, processing normally leaves the xsl:choose element. Unlike earlier versions, XSLT 2.0 explicitly requires this. No break instruction is needed. Multiple xsl:when, as separate blocks, never run, even if multiple conditions hold; see xsl:when.
The xsl:otherwise block must be last and runs only if no preceding xsl:when condition holds.
Examples:
Example 1 – xsl:otherwise as an else branch:
<xsl:choose>
<xsl:when test="$bedingung">
<!-- if-Zweig -->
</xsl:when>
<xsl:otherwise>
<!-- else-Zweig -->
</xsl:otherwise>
</xsl:choose>
If the test condition of xsl:when fails, so $bedingung is false, the xsl:otherwise block runs.
Example 2 – xsl:otherwise as a select default:
<xsl:choose>
<xsl:when test="$bedingung1">
<!-- Case bedingung1 -->
</xsl:when>
<xsl:when test="$bedingung2">
<!-- Case bedingung2 -->
</xsl:when>
<xsl:when test="$bedingung3">
<!-- Case bedingung3 -->
</xsl:when>
<xsl:otherwise>
<!-- Case default -->
</xsl:otherwise>
</xsl:choose>
As the last member of a xsl:when chain, xsl:otherwise behaves like the default branch of a select-case structure.
Element definition:
XSLT 1.0:
<!-- Category: sub-instruction -->
<xsl:otherwise>
<!-- Content: template -->
</xsl:otherwise>
XSLT 2.0:
<!-- Category: sub-instruction -->
<xsl:otherwise>
<!-- Content: sequence-constructor -->
</xsl:otherwise>
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn