Experimental: proposed new XSLT 4.0 instruction. Used to choose one of a number of alternative outputs, based on the value of a supplied expression.

Category: instruction
Content: ( xsl:when+ , xsl:otherwise? , xsl:fallback* )
Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element




Computes an atomic value that is compared with the values in the test attributes of the xsl:when children.

Saxon availability

Available in the XSLT 4.0 proposal. Requires Saxon-PE or Saxon-EE. Implemented experimentally since Saxon 11. Available for all platforms.

Notes on the Saxon implementation

Available from Saxon 11; requires syntax extensions to be enabled.


The select attribute contains an expression which must deliver a single atomic value (it is atomized if necessary). This value is compared with the values of the test conditions in each of the contained xsl:when elements in turn until the first match is found. If a match is found, that xsl:when element is evaluated; if no match is found, the xsl:otherwise element is evaluated; if there is no xsl:otherwise element, the xsl:switch instruction returns an empty sequence.

Unlike xsl:choose, the test condition in the xsl:when element is an expression that can evaluate to any sequence of atomic values. The comparison is done using the "=" operator, so the test succeeds if any of the values matches.

For more information, see Conditional instructions.


<xsl:switch select="@cat"> <xsl:when test="'F'">Fiction</xsl:when> <xsl:when test="'C'">Crime</xsl:when> <xsl:when test="'R', 'L'" select="my:f(@cat)"/> <xsl:otherwise>General</xsl:otherwise> </xsl:switch>

Links to W3C specifications

XSLT 4.0 Specification

See also