How to Use Switch Case in XSLT with xsl:choose?
Extensible Stylesheet Language Transformations (XSLT) does not
provide a native switch or case keyword found
in standard procedural languages like C, Java, or JavaScript. Instead,
XSLT accomplishes conditional branching and multi-case logic using the
<xsl:choose> element combined with
<xsl:when> and <xsl:otherwise>.
This structure allows developers to evaluate multiple boolean conditions
sequentially and execute template logic for the first matching
condition, with an optional fallback branch when no conditions are
satisfied.
Understanding the XSLT Conditional Structure
The mapping between standard switch-case syntax and XSLT conditional tags is direct:
switch (expression)corresponds to the enclosing<xsl:choose>element.case condition:corresponds to each individual<xsl:when test="...">element.default:corresponds to the<xsl:otherwise>element.break;is implicit; XSLT automatically exits the<xsl:choose>block once the first matching<xsl:when>condition evaluates to true.
Unlike languages that permit "fall-through" behavior unless
interrupted by a break statement, XSLT evaluates child
<xsl:when> elements strictly from top to bottom and
processes only the first one whose test attribute evaluates
to true.
Basic Syntax and Structure
The core syntax requires child elements to be nested properly within
the parent <xsl:choose> wrapper:
<xsl:choose>
<xsl:when test="boolean_expression_1">
<!-- Output or template logic for case 1 -->
</xsl:when>
<xsl:when test="boolean_expression_2">
<!-- Output or template logic for case 2 -->
</xsl:when>
<xsl:otherwise>
<!-- Fallback output if no cases match -->
</xsl:otherwise>
</xsl:choose>The <xsl:otherwise> element is optional, but if
included, it must always appear as the final child element within the
<xsl:choose> block.
Practical Example
Consider an XML source containing user profile data with account status codes:
<user>
<name>Jane Doe</name>
<status>A</status>
</user>To transform this status code into a readable label, you can implement the following XSLT template:
<xsl:template match="user">
<div class="user-card">
<p>Name: <xsl:value-of select="name"/></p>
<p>
Status:
<xsl:choose>
<xsl:when test="status = 'A'">
<span class="badge active">Active</span>
</xsl:when>
<xsl:when test="status = 'P'">
<span class="badge pending">Pending Approval</span>
</xsl:when>
<xsl:when test="status = 'S'">
<span class="badge suspended">Suspended</span>
</xsl:when>
<xsl:otherwise>
<span class="badge unknown">Unknown Status</span>
</xsl:otherwise>
</xsl:choose>
</p>
</div>
</xsl:template>Key Considerations and Best Practices
- Evaluation Order: Always place more specific
conditions before general ones. Because processing stops after the first
true
<xsl:when>, subsequent matches are ignored. - Complex Conditions: The
testattribute accepts full XPath expressions, allowing for complex logic using operators likeand,or, and XPath functions such asstarts-with(),contains(), ornormalize-space(). - Single vs. Multiple Conditions: For simple
single-condition checks with no alternate
elselogic, use<xsl:if test="...">instead of<xsl:choose>to keep template markup concise.