XML technologies / XSLT / Reference: XSLT 1.0 and 2.0 elements / when
(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:when contains a conditional block, instantiated only if the condition expressed in attribute test holds. Multiple conditions inserted into instruction xsl:choose using xsl:when choose between alternative template blocks.
Classification | Sub-instruction |
Function group | Conditional processing |
Introduced | XSLT 1.0 |
Position in the stylesheet and permitted contents:
xsl:when is a sub-instruction used only as an immediate child of xsl:choose. Its sequence constructor is instantiated when the condition in attribute test holds.
Attributes:
Standard attributes apply. Instruction xsl:when also requires attribute test.
test
Value | xpath-expression |
Usage | required |
Introduced | XSLT 1.0 |
The test attribute contains an XPath expression yielding a Boolean or something convertible to one. If it yields false, the condition fails and the block inside xsl:when is skipped. If it yields true, the condition holds and the block is instantiated.
Purpose:
The instruction xsl:when forms, within its enclosing xsl:choose instruction, a sequence of conditional blocks analogous to a switch statement in other languages. A xsl:when instruction with a block corresponds to one CASE in a SELECT group.
Only the xsl:when instruction whose test attribute is first satisfied runs. After its block, processing leaves xsl:choose; subsequent xsl:when instructions are ignored even if their test attributes would also be satisfied.
Only when none of the test conditions in the xsl:choose provided by xsl:when holds does xsl:otherwise run, if present, instantiating its block as the default branch of select. Otherwise, processing leaves xsl:choose without running any block.
A single xsl:when instruction within xsl:choose is exactly equivalent to an xsl:if instruction. However, combining xsl:choose and xsl:when permits extension, unlike xsl:if, by adding xsl:otherwise corresponding to ELSE in IF.
Note – Alternative XPath 2.0 constructs:
XPath 2.0 allows if ... then ... else constructs inside an expression, sometimes making xsl:choose unnecessary, at the cost of backward compatibility with XSLT 1.0/XPath 1.0.
Example – Position-dependent English ordinal suffixes:
...
<xsl:value-of select="position()"/>
<xsl:choose>
<xsl:when test="position()=1">st</xsl:when>
<xsl:when test="position()=2">nd</xsl:when>
<xsl:when test="position()=3">rd</xsl:when>
<xsl:otherwise>th</xsl:otherwise>
</xsl:choose>
...
Depending on position(), outputs 1st, 2nd, 3rd, 4th, etc.
Element definition:
XSLT 1.0:
<!-- Category: instruction -->
<xsl:when
test = boolean-expression>
<!-- Content: template -->
</xsl:when>
XSLT 2.0:
<!-- Category: instruction -->
<xsl:when
test = expression>
<!-- Content: sequence-constructor -->
</xsl:when>
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