Please visit our sponsors !
The <xsl:choose> Element
Definition and Usage
The <xsl:choose> element is used in conjunction with <xsl:when> and <xsl:otherwise>
to express multiple conditional tests.
If no <xsl:when> is true, the content of <xsl:otherwise> is processed.
If no <xsl:when> is true, and no <xsl:otherwise> element is present, nothing
is created.
Tip: For simple conditional testing, use the <xsl:if> element instead.
Note: This element may have NON-standard behavior in IE 5.X!
Syntax
<xsl:choose>
<!-- Content:(xsl:when+,xsl:otherwise?) -->
</xsl:choose>
|
Attributes
None
Example 1
The code below will add a pink background-color to the artist column WHEN the
price of the cd is higher than 10.
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<xsl:choose>
<xsl:when test="price>'10'">
<td bgcolor="#ff00ff">
<xsl:value-of select="artist"/></td>
</xsl:when>
<xsl:otherwise>
<td><xsl:value-of select="artist"/></td>
</xsl:otherwise>
</xsl:choose>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
If you have Netscape 6 or IE 5 or higher you can view:
the XML file and
the XSL file.
View the result with Netscape 6 or
IE 6
Note: Unable to view the result in IE 5, because the
"http://www.w3.org/TR/WD-xsl" namespace does not understand the <xsl:choose> element.
Example 2
Declare a variable named "color". Set its value to
the color attribute of the current element. If the current element has no
color attribute, the value of "color" will be "green":
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>green</xsl:otherwise>
</xsl:choose>
</xsl:variable>
|
Jump to: Top of Page
or HOME or
Printer friendly page
Search W3Schools:
What Others Say About Us
Does the world know about us? Check out these places:
Dogpile
Alta Vista
MSN
Google
Excite
Lycos
Yahoo
Ask Jeeves
We Help You For Free. You Can Help Us!
W3Schools is for training only. We do not warrant its correctness or its fitness for use.
The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our
terms of use and
privacy policy.
Copyright 1999-2002 by Refsnes Data. All Rights Reserved
|