XSLT <xsl:when>
❮ Complete XSLT Element Reference
定义和用法
The <xsl:when> element is used to specify an action for the <xsl:choose> element. The <xsl:when> element evaluates an expression, and if it returns true, an action is performed.
注意: <xsl:when> 元素与 <xsl:choose> 和 <xsl:otherwise> 结合使用,以表达多个条件测试。
语法
<xsl:when
test="boolean-expression">
<!-- Content: template -->
</xsl:when>
属性
Attribute | 值 | 描述 |
---|---|---|
test | boolean-expression | Required. Specifies a Boolean expression to be tested |
示例 1
下面的代码将在 CD 的价格高于 10 时,为 artist 列添加粉色背景色。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>我的 CD 收藏</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>标题</th>
<th>艺术家</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>
查看 XML 文件, 查看 XSL 文件, and 查看结果.
示例 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>
❮ Complete XSLT Element Reference