XSLT <if>
Conditional processing in XSLT transformations
❓ What is <xsl:if>?
The <xsl:if> element tests a condition and processes content only when the condition is true. It's used for simple conditional logic in your XSLT transformations.
<!-- Display only if price is low -->
<xsl:if test="price < 50">
<p>On Sale!</p>
</xsl:if>
Understanding <if>
The <xsl:if> element evaluates a test condition using XPath expressions. If the condition is true, the content inside is processed. Unlike programming languages, XSLT's if has no else clause - use <xsl:choose> for that.
Test Attribute
Condition to evaluate
test="price > 100"
XPath Conditions
Use XPath expressions
test="@status='active'"
Boolean Logic
True/false evaluation
test="count(item) > 0"
No Else Clause
Only processes if true
<xsl:if test="...">
<!-- content -->
</xsl:if>
🔹 Basic Condition Example
Display a message only when a condition is met:
XML Data:
<?xml version="1.0"?>
<products>
<product>
<name>Laptop</name>
<price>899</price>
</product>
<product>
<name>Mouse</name>
<price>25</price>
</product>
</products>
XSLT Template:
<xsl:template match="products">
<xsl:for-each select="product">
<div>
<h3><xsl:value-of select="name"/></h3>
<p>Price: $<xsl:value-of select="price"/></p>
<xsl:if test="price < 100">
<p style="color: red;">🔥 Great Deal!</p>
</xsl:if>
</div>
</xsl:for-each>
</xsl:template>
Output:
Laptop
Price: $899
Mouse
Price: $25
🔥 Great Deal!
🔹 Testing Attributes
Check attribute values with conditional logic:
XML Data:
<users>
<user status="active">
<name>John Doe</name>
</user>
<user status="inactive">
<name>Jane Smith</name>
</user>
</users>
XSLT Template:
<xsl:template match="users">
<ul>
<xsl:for-each select="user">
<li>
<xsl:value-of select="name"/>
<xsl:if test="@status='active'">
<span style="color: green;"> ✓ Active</span>
</xsl:if>
</li>
</xsl:for-each>
</ul>
</xsl:template>
Output:
- John Doe ✓ Active
- Jane Smith
🔹 Comparison Operators
Use various operators to test conditions:
Common Operators:
- = - Equal to
- != - Not equal to
- < - Less than (use < for <)
- > - Greater than (use > for >)
- <= - Less than or equal
- >= - Greater than or equal
XML Data:
<students>
<student>
<name>Alice</name>
<grade>95</grade>
</student>
<student>
<name>Bob</name>
<grade>72</grade>
</student>
</students>
XSLT Template:
<xsl:template match="students">
<xsl:for-each select="student">
<p>
<xsl:value-of select="name"/>:
<xsl:value-of select="grade"/>
<xsl:if test="grade >= 90">
<strong> - Excellent!</strong>
</xsl:if>
<xsl:if test="grade < 75">
<em> - Needs Improvement</em>
</xsl:if>
</p>
</xsl:for-each>
</xsl:template>
Output:
Alice: 95 - Excellent!
Bob: 72 - Needs Improvement
🔹 Testing Node Existence
Check if a node exists before processing:
XML Data:
<books>
<book>
<title>XML Guide</title>
<author>John Smith</author>
</book>
<book>
<title>Mystery Novel</title>
</book>
</books>
XSLT Template:
<xsl:template match="books">
<xsl:for-each select="book">
<div>
<h4><xsl:value-of select="title"/></h4>
<xsl:if test="author">
<p>By: <xsl:value-of select="author"/></p>
</xsl:if>
<xsl:if test="not(author)">
<p><em>Author Unknown</em></p>
</xsl:if>
</div>
</xsl:for-each>
</xsl:template>
Output:
XML Guide
By: John Smith
Mystery Novel
Author Unknown
🔹 Logical Operators
Combine multiple conditions using and/or:
XML Data:
<products>
<product category="Electronics" inStock="yes">
<name>Laptop</name>
<price>899</price>
</product>
<product category="Electronics" inStock="no">
<name>Tablet</name>
<price>499</price>
</product>
</products>
XSLT Template:
<xsl:template match="products">
<xsl:for-each select="product">
<p>
<xsl:value-of select="name"/> -
$<xsl:value-of select="price"/>
<xsl:if test="@category='Electronics' and @inStock='yes'">
<span style="color: green;"> ✓ Available Now</span>
</xsl:if>
</p>
</xsl:for-each>
</xsl:template>
Output:
Laptop - $899 ✓ Available Now
Tablet - $499
💡 Key Points:
-
Use
testattribute to specify the condition - No else clause - use <xsl:choose> for if-else logic
-
Use
<for < and>for > in XML -
Combine conditions with
and/or -
Use
not()function to negate conditions