XSLT <if>

创建于 2024-12-03 / 25
字体: [默认] [大] [更大]

<xsl:if> 元素用于对XML文件的内容进行条件测试。


<xsl:if> 元素

要对XML文件的内容进行条件if测试,请添加<xsl:if>元素添加到XSL文档。

语法

<xsl:if test="expression">
  ...some output if the expression is true...
</xsl:if>

<xsl:if> 元素放在哪里

要添加条件测试,请添加<xsl:if>元素内部<xsl:for-each>XSL文件中的元素:

实例

<?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>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
      <th>Price</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>
          <td><xsl:value-of select="price"/></td>
        </tr>
      </xsl:if>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 尝试一下 »

注释: required 属性的值包含要计算的表达式。

上面的代码只输出价格高于10的CD的 title 和 artist 元素。



0 人点赞过