Skip to content

xsl:choose

Multi-branch conditional. Tries each xsl:when in document order and runs the first one whose test is true; falls through to xsl:otherwise if none match.

<xsl:choose>
  <xsl:when test="...">...</xsl:when>
  <xsl:when test="...">...</xsl:when>
  <xsl:otherwise>...</xsl:otherwise>
</xsl:choose>

xsl:otherwise is optional — if it is omitted and no xsl:when matches, xsl:choose produces nothing. At least one xsl:when is required.

Children

Element Description
xsl:when A branch with a test attribute (XPath boolean). At least one is required.
xsl:otherwise Optional fall-through branch. Must come last.

Example

<xsl:choose>
  <xsl:when test="@type = 'book'">
    <book><xsl:value-of select="title"/></book>
  </xsl:when>
  <xsl:when test="@type = 'article'">
    <article><xsl:value-of select="title"/></article>
  </xsl:when>
  <xsl:otherwise>
    <unknown><xsl:value-of select="title"/></unknown>
  </xsl:otherwise>
</xsl:choose>