Welcome to The Coding College! In this article, we’ll dive into the <xsl:choose>
element, a core feature of XSLT that enables powerful conditional logic. If you’ve been using <xsl:if>
for basic conditions, you’ll find <xsl:choose>
particularly useful for handling more complex scenarios with multiple branches.
What Is the <xsl:choose>
Element?
The <xsl:choose>
element in XSLT is a multi-condition control structure. It allows you to define multiple conditional branches, similar to an if-else-if
or switch
statement in programming languages.
It consists of one or more <xsl:when>
elements and an optional <xsl:otherwise>
element:
<xsl:when>
defines a condition.<xsl:otherwise>
executes if none of the<xsl:when>
conditions are met.
Syntax
<xsl:choose>
<xsl:when test="XPath_expression">
<!-- Code to execute if the condition is true -->
</xsl:when>
<xsl:when test="XPath_expression">
<!-- Code to execute if the condition is true -->
</xsl:when>
<xsl:otherwise>
<!-- Code to execute if no conditions are met -->
</xsl:otherwise>
</xsl:choose>
How <xsl:choose>
Works
- Sequential Evaluation: Each
<xsl:when>
condition is evaluated in order. - First Match Wins: The first
<xsl:when>
condition that evaluates totrue
executes. - Fallback Logic: If no
<xsl:when>
conditions are met, the<xsl:otherwise>
block (if present) is executed.
Example: Simple Conditional Logic
Input XML:
<products>
<product>
<name>Laptop</name>
<price>999.99</price>
</product>
<product>
<name>Smartphone</name>
<price>699.99</price>
</product>
<product>
<name>Tablet</name>
<price>399.99</price>
</product>
</products>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Product Categories</h1>
<ul>
<xsl:for-each select="products/product">
<li>
<xsl:value-of select="name" /> -
<xsl:choose>
<xsl:when test="price > 800">
<strong>High-End</strong>
</xsl:when>
<xsl:when test="price > 500">
<em>Mid-Range</em>
</xsl:when>
<xsl:otherwise>
<span>Budget</span>
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Product Categories</h1>
<ul>
<li>Laptop - <strong>High-End</strong></li>
<li>Smartphone - <em>Mid-Range</em></li>
<li>Tablet - <span>Budget</span></li>
</ul>
</body>
</html>
Key Features of <xsl:choose>
1. Multiple Conditions
You can define as many <xsl:when>
elements as needed for your logic.
Example:
<xsl:choose>
<xsl:when test="price > 1000">
High-End
</xsl:when>
<xsl:when test="price > 500">
Mid-Range
</xsl:when>
<xsl:when test="price > 200">
Budget
</xsl:when>
</xsl:choose>
2. Fallback with <xsl:otherwise>
The <xsl:otherwise>
block acts as a “default” case.
Example:
<xsl:choose>
<xsl:when test="price > 500">Expensive</xsl:when>
<xsl:otherwise>Affordable</xsl:otherwise>
</xsl:choose>
If no <xsl:when>
conditions match, “Affordable” is displayed.
3. Nested <xsl:choose>
You can nest <xsl:choose>
elements for complex logic.
Example:
<xsl:choose>
<xsl:when test="category = 'electronics'">
<xsl:choose>
<xsl:when test="price > 1000">Premium Electronics</xsl:when>
<xsl:otherwise>Standard Electronics</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>Other Categories</xsl:otherwise>
</xsl:choose>
Advanced Example: Combining XPath and <xsl:choose>
Input XML:
<employees>
<employee>
<name>John</name>
<department>IT</department>
<salary>80000</salary>
</employee>
<employee>
<name>Jane</name>
<department>HR</department>
<salary>60000</salary>
</employee>
<employee>
<name>Smith</name>
<department>Finance</department>
<salary>40000</salary>
</employee>
</employees>
XSLT Stylesheet:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h1>Employee Categories</h1>
<ul>
<xsl:for-each select="employees/employee">
<li>
<xsl:value-of select="name" /> -
<xsl:choose>
<xsl:when test="department = 'IT'">
Tech Team
</xsl:when>
<xsl:when test="department = 'HR'">
Human Resources
</xsl:when>
<xsl:otherwise>
Other Departments
</xsl:otherwise>
</xsl:choose>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Output HTML:
<html>
<body>
<h1>Employee Categories</h1>
<ul>
<li>John - Tech Team</li>
<li>Jane - Human Resources</li>
<li>Smith - Other Departments</li>
</ul>
</body>
</html>
Best Practices
- Use
<xsl:choose>
for Complex Conditions: Replace multiple<xsl:if>
blocks with a cleaner<xsl:choose>
structure. - Always Include
<xsl:otherwise>
: Provide a fallback case for scenarios where none of the conditions are met. - Combine with XPath: Leverage XPath expressions in the
test
attribute for dynamic conditions.
Limitations of <xsl:choose>
- Sequential Evaluation Only: Conditions are checked in order, and the first match terminates further evaluation.
- Static Fallback Logic:
<xsl:otherwise>
executes only if no<xsl:when>
conditions are true. For more dynamic fallbacks, use parameters or templates.
Conclusion
The <xsl:choose>
element is a powerful tool for handling conditional logic in XSLT. It provides flexibility for creating structured, readable, and dynamic transformations of XML data. Whether you’re categorizing data, applying conditional formatting, or building complex workflows, <xsl:choose>
is essential for clean and efficient XSLT stylesheets.