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.

<xsl:if test="discount">
    <span>Discount available!</span>
</xsl:if>
<xsl:if test="@status = 'active'">
    <p>User is active.</p>
</xsl:if>
<xsl:if test="price &gt; 100">
    <span class="premium">Premium Item</span>
</xsl:if>
<xsl:if test="stock &gt; 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:

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 &gt; 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.