Easy Tutorial
❮ Xsl Editors Xsl Transformation ❯

XSLT <xsl:if> Element


The <xsl:if> element is used to place conditional tests on the content of an XML file.


<xsl:if> Element

To place a conditional test on the content of an XML file, add the <xsl:if> element to the XSL document.

Syntax


Where to Place the <xsl:if> Element

To add a conditional test, place the <xsl:if> element inside the <xsl:for-each> element in the XSL file:

Example

<?xml version="1.0" encoding="UTF-8"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

&lt;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>
    &lt;xsl:for-each select="catalog/cd">
      &lt;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>

Note: The test attribute, which is required, contains the expression that needs to be evaluated.

The above code will only output the title and artist elements of CDs with a price greater than 10.

❮ Xsl Editors Xsl Transformation ❯