Name

<xsl:choose>

Synopsis

<xsl:choose> ... </xsl:choose>

Description

The <xsl:choose> element, in conjunction with the elements <xsl:when> and <xsl:otherwise>, offers the ability to perform multiple condition tests. For example:

<xsl:template match="chapter/title">
  <xsl:choose>
    <xsl:when test=".[first-of-type()]">
        Start Here:
    </xsl:when>
    <xsl:otherwise>
        Then Read:
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates/>
</xsl:template>

This example matches against each of the qualifying <title> elements, but it must test each <title> element to determine how to format it. Here, the formatting used depends on whether the element is the first of its type or not. If it is, it applies the letters ”Start Here:“ before the first <title> element. Otherwise, the letters ”Then Read:“ are placed before the others.

Get XML Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.