How Does xsl:if Work in XSLT?
Conditional logic in XSLT allows developers to dynamically control
the output of an XML transformation based on specific criteria. The
<xsl:if> element acts as a straightforward,
single-branch conditional test: if the evaluated XPath expression
returns true, the enclosed template content is processed and rendered;
if false, the processor ignores the block entirely. Understanding its
syntax, common test patterns, and key limitations is essential for
writing clean, efficient XSLT stylesheets.
Basic Syntax and Structure
The <xsl:if> element requires a single mandatory
attribute: test. The value of this attribute is an XPath
expression evaluated in the context of the current node.
<xsl:if test="expression">
<!-- Content to output if condition is true -->
</xsl:if>When the XSLT engine encounters this element, it converts the result
of the test expression to a boolean value. If the boolean
evaluates to true, the child nodes within the
<xsl:if> tag are instantiated in the output tree.
Common Testing Patterns
The test attribute supports a wide variety of XPath
operations, from simple node-existence checks to complex comparative
statements.
- Node Existence: Testing whether an element or attribute is present.
<xsl:if test="discount">
<span>Discount available!</span>
</xsl:if>- Attribute Checks: Verifying attribute presence or values.
<xsl:if test="@status = 'active'">
<p>User is active.</p>
</xsl:if>- Numerical Comparisons: Comparing numbers requires
standard XML entity escapes for angle brackets. Use
<for less than and>for greater than.
<xsl:if test="price > 100">
<span class="premium">Premium Item</span>
</xsl:if>- Logical Operators: Combining multiple conditions
using
and,or, andnot().
<xsl:if test="stock > 0 and not(@discontinued)">
<button>Add to Cart</button>
</xsl:if>Boolean Evaluation Rules in XPath
Understanding how non-boolean values evaluate inside the
test attribute prevents common transformation bugs:
- Node-sets: Evaluates to
trueif the node-set is non-empty;falseif empty. - Strings: Evaluates to
trueif the string length is greater than 0;falseif the string is empty (""). - Numbers: Evaluates to
trueif the number is non-zero and notNaN;falsefor0orNaN.
Practical Example
Given the following XML input:
<catalog>
<book id="1">
<title>Learning XSLT</title>
<inStock>true</inStock>
<price>35.00</price>
</book>
<book id="2">
<title>Advanced XML</title>
<inStock>false</inStock>
<price>50.00</price>
</book>
</catalog>An XSLT template can selectively render in-stock items with special pricing notices:
<xsl:template match="book">
<div class="book-card">
<h3><xsl:value-of select="title"/></h3>
<xsl:if test="inStock = 'true'">
<span class="status in-stock">Available Now</span>
</xsl:if>
<xsl:if test="price > 40.00">
<span class="notice">Eligible for Free Shipping</span>
</xsl:if>
</div>
</xsl:template>The Single-Branch Limitation
The most significant characteristic of <xsl:if> is
the absence of an else or elseif branch. If a
transformation requires mutually exclusive conditions or default
fallback content, <xsl:choose> must be used
instead.
<xsl:choose>
<xsl:when test="inStock = 'true'">
<span>In Stock</span>
</xsl:when>
<xsl:otherwise>
<span>Out of Stock</span>
</xsl:otherwise>
</xsl:choose>Using <xsl:if> remains the preferred, concise
approach whenever an output should appear solely upon satisfying a
single, standalone condition.