目录搜索
XSLT 基础教程XSL 语言XSLT 简介XSLT 浏览器XSLT - 转换XSLT <xsl:template> 元素XSLT <xsl:value-of> 元素XSLT <xsl:for-each> 元素XSLT <xsl:sort> 元素XSLT <xsl:if> 元素XSLT <xsl:choose> 元素XSLT <xsl:apply-templates> 元素XSLT - 在客户端XSLT - 在服务器端XSLT - 编辑 XMLXML 编辑器XSLT 元素参考手册XSLT <xsl:apply-imports> 元素XSLT <xsl:apply-templates> 元素XSLT <xsl:attribute> 元素XSLT <xsl:attribute-set> 元素XSLT <xsl:call-template> 元素XSLT <xsl:choose> 元素XSLT <xsl:comment> 元素XSLT <xsl:copy> 元素XSLT <xsl:copy-of> 元素XSLT <xsl:decimal-format> 元素XSLT <xsl:element> 元素XSLT <xsl:fallback> 元素XSLT <xsl:for-each> 元素XSLT <xsl:if> 元素XSLT <xsl:import> 元素XSLT <xsl:include> 元素XSLT <xsl:key> 元素XSLT <xsl:message> 元素XSLT <xsl:namespace-alias> 元素XSLT <xsl:number> 元素XSLT <xsl:otherwise> 元素XSLT <xsl:output> 元素XSLT <xsl:param> 元素XSLT <xsl:preserve-space> 和 <xsl:strip-space> 元素XSLT <xsl:processing-instruction> 元素XSLT <xsl:sort> 元素XSLT <xsl:stylesheet> 和 <xsl:transform> 元素XSLT <xsl:template> 元素XSLT <xsl:text> 元素XSLT <xsl:value-of> 元素XSLT <xsl:value-of> 元素XSLT <xsl:variable> 元素XSLT <xsl:when> 元素XSLT <xsl:with-param> 元素XSLT 函数XSLT current() 函数XSLT document() 函数XSLT element-available() 函数XSLT format-number() 函数XSLT function-available() 函数XSLT generate-id() 函数XSLT key() 函数XSLT system-property() 函数XSLT unparsed-entity-uri() 函数
文字

XSLT <xsl:if> 元素



定义和用法

<xsl:if> 元素包含了一个模板,只有指定的条件成立时,才应用此模板。

提示:请把 <xsl:choose> 与 <xsl:when> 和 <xsl:otherwise> 协同使用来表达多重条件测试!


语法

<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>

属性

属性描述
testexpression必需。规定要测试的条件。

实例

当 CD 的价格高于 10 时,选取 title 和 artist 的值:

实例 1

<?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">
<xsl:if test="price &gt; 10">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 

显示每个 CD 的标题。如果不是最后一个或倒数第二个 CD,则在每个 CD-title 间插入 ", "。如果是最后一个 CD,则在标题后添加 "!"。如果是倒数第二个 CD,则在标题后添加 ", and ":

实例 2

<?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>
<p>Titles:
<xsl:for-each select="catalog/cd">
<xsl:value-of select="title"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()-1">
<xsl:text> and </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>!</xsl:text>
</xsl:if>
</xsl:for-each>
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
上一篇:下一篇: