Next / Previous / Contents / Shipman's homepage

10.2. xsl:if: Conditional processing

Sometimes you want to perform an action only when a certain condition is met. The xsl:if tag does this conditional processing. This is XSLT's single-branch “if” construct; there is no two-branch form. If you want to perform one thing if a condition is true, and a different thing if that condition is false, you'll have to use the xsl:choose construct. There is one required attribute:


This XPath expression is evaluated. If it is true, the content of the xsl:if element is processed, otherwise that content is not processed.

Here's an example. This writes the value of variable head-count to the output, but only if it is an odd number:

    <xsl:if test="($head-count mod 2)=1">
      <xsl:value-of select="$head-count"/>