Xslt Choose in XML
? Using <xsl:choose> in XSLT (XML)
<xsl:choose> is like an if-else if-else control structure in XSLT. It lets you apply conditional logic to your transformation based on XML data values.
How <xsl:choose> Works
Contains multiple
<xsl:when test="...">elements to test conditions.Optional
<xsl:otherwise>element for the default case if no conditions match.
Example
XML Input
<person> <age>20</age></person>XSLT with <xsl:choose>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="person"> <div> <xsl:choose> <xsl:when test="age < 18"> <p>Minor</p> </xsl:when> <xsl:when test="age >= 18 and age < 65"> <p>Adult</p> </xsl:when> <xsl:otherwise> <p>Senior</p> </xsl:otherwise> </xsl:choose> </div> </xsl:template></xsl:stylesheet>Explanation
Checks the
<age>value.Outputs "Minor" if age < 18.
Outputs "Adult" if age between 18 and 64.
Outputs "Senior" otherwise.
Resulting Output (HTML)
<div> <p>Adult</p></div>Summary
Use
<xsl:choose>for branching logic.Use multiple
<xsl:when>for conditions.Use
<xsl:otherwise>for default/fallback.
Would you like me to create a full working example with XML, XSLT, and instructions?