xsl:choose
xsl:if
xsl:otherwise
xsl:param
xsl:value-of
xsl:variable
ELEMENT: xsl:when
<xsl:choose>
<xsl:when
test="expression"
>
...
</xsl:when>
...
<xsl:otherwise>
...
<xsl:otherwise>
</xsl:choose>
The
xsl:when
element is a child of the
xsl:choose
element.
The
xsl:choose
element is used to make a choice when there are two or more possible courses of action. It provides a means for conducting multiple condition testing.
The
xsl:choose
element must contain one or more
xsl:when
elements and can contain only one optional
xsl:otherwise
element (which must occur after all of the
xsl:when
elements). If the
xsl:choose
element only contains one
xsl:when
element, then for all practical purposes, it behaves just like the
xsl:if
element. When faced with three or more choices, these elements behave similar to an if-then-else statement (or a Select Case) as found in numerous other computer languages.
The purpose of
xsl:when
element is to contain a Boolean expression that can be tested. The test must return a value of either true or false. Each
xsl:when
element is examined in the order of occurrence. If and when the conditions of the test expression are satisfied (returns True), the code contained in that element is executed. Then the
xsl:choose
element is automatically exited and all further
xsl:when
elements are ignored and they are not tested. The optional
xsl:otherwise
element is also automatically ignored.
If none of the test conditions in any
xsl:when
element is satisfied (all return False), then the
xsl:otherwise
element is automatically selected (if it is present) and the code associated with that element is executed. If there is no
xsl:otherwise
element, then the
xsl:choose
element is exited.
This element is not a self-closing tag. The separate closing element is mandatory.
test="expression"
The mandatory
test
attribute is a Boolean expression that is the test condition to be satisfied. A True signifies that the test conditions have been met. A False signifies that the test conditions have not been met. We use the
DevGuru Staff List XML file
for our example with the following header:
<?xml-stylesheet type="text/xsl" href="xslt_example_when.xsl"?>
and we name it: xslt_example_when.xml
This example assigns font color based upon age.
Code for xslt_example_when.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="devguru_staff/programmer">
<xsl:choose>
<
xsl:when test="age < 30"
>
<span style="color:red;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
<
/xsl:when
>
<
xsl:when test="age < 40"
>
<span style="color:orange;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
<
/xsl:when
>
<
xsl:when test="age < 50"
>
<span style="color:green;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
<
/xsl:when
>
<
xsl:when test="age < 60"
>
<span style="color:blue;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
<
/xsl:when
>
<xsl:otherwise>
<span style="color:black;">
NAME: <xsl:value-of select="name" /> AGE: <xsl:value-of select="age" />
</span>
</xsl:otherwise>
</xsl:choose>
<br />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Output:
Click to view output in separate window
- requires Internet Explorer
Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information