XSLT <choose>

Multiple conditional branches in XSLT

🔀 What is <xsl:choose>?

The <xsl:choose> element provides multiple conditional branches, similar to if-else or switch statements. It tests multiple conditions and executes the first matching one, with an optional default case.


<xsl:choose>
    <xsl:when test="price &lt; 50">Cheap</xsl:when>
    <xsl:otherwise>Expensive</xsl:otherwise>
</xsl:choose>
                                    

Understanding <choose>

The <xsl:choose> element works with <xsl:when> and <xsl:otherwise> to create complex conditional logic. It evaluates conditions in order and executes only the first true condition, making it perfect for multi-way decisions.

🔀

<xsl:choose>

Container for conditions

<xsl:choose>
  <!-- conditions -->
</xsl:choose>

<xsl:when>

Test specific conditions

<xsl:when test="...">
  <!-- content -->
</xsl:when>
🔄

<xsl:otherwise>

Default case (optional)

<xsl:otherwise>
  <!-- default -->
</xsl:otherwise>
🎯

First Match

Executes first true condition

<!-- Stops at first match -->

🔹 Basic Choose Example

Categorize products based on price ranges:

XML Data:

<?xml version="1.0"?>
<products>
    <product>
        <name>Laptop</name>
        <price>1200</price>
    </product>
    <product>
        <name>Mouse</name>
        <price>25</price>
    </product>
    <product>
        <name>Keyboard</name>
        <price>75</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:choose>
                <xsl:when test="price &gt; 1000">
                    <strong style="color: red;">Premium</strong>
                </xsl:when>
                <xsl:when test="price &gt; 50">
                    <span style="color: blue;">Standard</span>
                </xsl:when>
                <xsl:otherwise>
                    <span style="color: green;">Budget</span>
                </xsl:otherwise>
            </xsl:choose>
        </p>
    </xsl:for-each>
</xsl:template>

Output:

Laptop - $1200 - Premium

Mouse - $25 - Budget

Keyboard - $75 - Standard

🔹 Grade Classification

Assign letter grades based on numerical scores:

XML Data:

<students>
    <student>
        <name>Alice</name>
        <score>95</score>
    </student>
    <student>
        <name>Bob</name>
        <score>82</score>
    </student>
    <student>
        <name>Charlie</name>
        <score>68</score>
    </student>
</students>

XSLT Template:

<xsl:template match="students">
    <table border="1">
        <tr>
            <th>Name</th>
            <th>Score</th>
            <th>Grade</th>
        </tr>
        <xsl:for-each select="student">
            <tr>
                <td><xsl:value-of select="name"/></td>
                <td><xsl:value-of select="score"/></td>
                <td>
                    <xsl:choose>
                        <xsl:when test="score &gt;= 90">A</xsl:when>
                        <xsl:when test="score &gt;= 80">B</xsl:when>
                        <xsl:when test="score &gt;= 70">C</xsl:when>
                        <xsl:when test="score &gt;= 60">D</xsl:when>
                        <xsl:otherwise>F</xsl:otherwise>
                    </xsl:choose>
                </td>
            </tr>
        </xsl:for-each>
    </table>
</xsl:template>

Output:

Name Score Grade
Alice 95 A
Bob 82 B
Charlie 68 D

🔹 Testing Attributes

Make decisions based on attribute values:

XML Data:

<orders>
    <order status="shipped">
        <id>1001</id>
    </order>
    <order status="pending">
        <id>1002</id>
    </order>
    <order status="delivered">
        <id>1003</id>
    </order>
</orders>

XSLT Template:

<xsl:template match="orders">
    <ul>
        <xsl:for-each select="order">
            <li>
                Order #<xsl:value-of select="id"/> - 
                
                <xsl:choose>
                    <xsl:when test="@status='delivered'">
                        <span style="color: green;">✓ Delivered</span>
                    </xsl:when>
                    <xsl:when test="@status='shipped'">
                        <span style="color: blue;">📦 In Transit</span>
                    </xsl:when>
                    <xsl:when test="@status='pending'">
                        <span style="color: orange;">⏳ Processing</span>
                    </xsl:when>
                    <xsl:otherwise>
                        <span style="color: gray;">Unknown Status</span>
                    </xsl:otherwise>
                </xsl:choose>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>

Output:

  • Order #1001 - 📦 In Transit
  • Order #1002 - ⏳ Processing
  • Order #1003 - ✓ Delivered

🔹 Complex Conditions

Combine multiple conditions with logical operators:

XML Data:

<items>
    <item category="Electronics" inStock="yes" price="500"/>
    <item category="Books" inStock="yes" price="20"/>
    <item category="Electronics" inStock="no" price="300"/>
</items>

XSLT Template:

<xsl:template match="items">
    <xsl:for-each select="item">
        <p>
            <xsl:choose>
                <xsl:when test="@category='Electronics' and @inStock='yes' and @price &gt; 400">
                    <strong style="color: gold;">⭐ Premium Electronics Available</strong>
                </xsl:when>
                <xsl:when test="@category='Electronics' and @inStock='yes'">
                    <span style="color: blue;">Electronics In Stock</span>
                </xsl:when>
                <xsl:when test="@inStock='yes'">
                    <span style="color: green;">Available</span>
                </xsl:when>
                <xsl:otherwise>
                    <span style="color: red;">Out of Stock</span>
                </xsl:otherwise>
            </xsl:choose>
        </p>
    </xsl:for-each>
</xsl:template>

Output:

⭐ Premium Electronics Available

Available

Out of Stock

🔹 Choose vs If

Understanding when to use each:

Use <xsl:if> when:

  • You have a single condition to test
  • No alternative actions needed

Use <xsl:choose> when:

  • You have multiple conditions (if-else-if)
  • You need a default case (otherwise)
  • Only one condition should execute

💡 Key Points:

  • <xsl:choose> must contain at least one <xsl:when>
  • <xsl:otherwise> is optional but recommended
  • Only the first matching <xsl:when> executes
  • <xsl:otherwise> must come last if used
  • Perfect for multi-way decisions and categorization

🧠 Test Your Knowledge

What element provides the default case in <xsl:choose>?