Name
<xsl:when> — Defines one branch of an <xsl:choose>
element. It is equivalent to the Java case
statement.
Category
Subinstruction (<xsl:when>
always appears as a child of an <xsl:choose>
element)
Required Attributes
- test
Contains a boolean expression that is evaluated. If the expression evaluates to
true
, the contents of the<xsl:when>
element are processed; otherwise, the contents of the<xsl:when>
are ignored.
Optional Attributes
None.
Content
An XSLT template.
Appears in
The <xsl:choose>
element only.
Defined in
XSLT section 9.2, Conditional Processing with xsl:choose
.
Example
This example uses an <xsl:choose>
element and three <xsl:when>
elements to cycle through a set of values. Now we will generate rows of an HTML table for each <listitem>
:
<?xml version="1.0"?> <list xml:lang="en"> <title>Albums I've bought recently:</title> <listitem>The Sacred Art of Dub</listitem> <listitem>Only the Poor Man Feel It</listitem> <listitem>Excitable Boy</listitem> <listitem xml:lang="sw">Aki Special</listitem> <listitem xml:lang="en-gb">Combat Rock</listitem> <listitem xml:lang="zu">Talking Timbuktu</listitem> <listitem xml:lang="jz">The Birth of the Cool</listitem> </list>
In our stylesheet, we’ll generate table rows with the background colors of mintcream
, lavender
, whitesmoke
, and papayawhip
. For each <listitem>
in our source document, one of the <xsl:when>
elements (or the <xsl:otherwise>
element) generates the appropriate color.
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> ...
Get XSLT now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.