您现在的位置: 365建站网 > 建站教程 > XSLT 教程 > XSLT <xsl:if> 元素

XSLT <xsl:if> 元素

此节有 208 人学习过     参与评论

定义和用法

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

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

语法

</>code

  1. <xsl:if
  2. test="expression">
  3. <!-- Content: template -->
  4. </xsl:if>

属性

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

实例

例子 1

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

</>code

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4. <xsl:template match="/">
  5. <html>
  6. <body>
  7. <h2>My CD Collection</h2>
  8. <table border="1">
  9. <tr bgcolor="#9acd32">
  10. <th>Title</th>
  11. <th>Artist</th>
  12. </tr>
  13. <xsl:for-each select="catalog/cd">
  14. <xsl:if test="price &gt; 10">
  15. <tr>
  16. <td><xsl:value-of select="title"/></td>
  17. <td><xsl:value-of select="artist"/></td>
  18. </tr>
  19. </xsl:if>
  20. </xsl:for-each>
  21. </table>
  22. </body>
  23. </html>
  24. </xsl:template>
  25. </xsl:stylesheet>

查看 XML 文件查看 XSL 文件查看结果

例子 2

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

</>code

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <xsl:stylesheet version="1.0"
  3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4. <xsl:template match="/">
  5. <html>
  6. <body>
  7. <h2>My CD Collection</h2>
  8. <p>Titles:
  9. <xsl:for-each select="catalog/cd">
  10. <xsl:value-of select="title"/>
  11. <xsl:if test="position()!=last()">
  12. <xsl:text>, </xsl:text>
  13. </xsl:if>
  14. <xsl:if test="position()=last()-1">
  15. <xsl:text> and </xsl:text>
  16. </xsl:if>
  17. <xsl:if test="position()=last()">
  18. <xsl:text>!</xsl:text>
  19. </xsl:if>
  20. </xsl:for-each>
  21. </p>
  22. </body>
  23. </html>
  24. </xsl:template>
  25. </xsl:stylesheet>

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (208人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------
XSLT 教程目录

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号