Next / Previous / Contents / Shipman's homepage

10.3. xsl:choose: The multiple-case construct

To produce output with two or more different cases, use the xsl:choose construct. It has one xsl:when element for each case, plus an optional xsl:otherwise element at the end for the “none of the above” case. Each xsl:when element has a test attribute whose value is an XPath expression; the first one that evaluates to a true value is processed.

Here's an example that outputs the content of the turtlechildren of the current node, and puts the first one in parentheses and the last one in square brackets.

    <xsl:for-each select="turtle">
      <xsl:choose>
        <xsl:when test="position() = 1">
          <xsl:text>(</xsl:text>
          <xsl:apply-templates select="."/>
          <xsl:text>)</xsl:text>
        </xsl:when>
        <xsl:when test="position() = count()">
          <xsl:text>[</xsl:text>
          <xsl:apply-templates select="."/>
          <xsl:text>]</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates select="."/>
        </xsl:otherwise>
      </xsl:choose>      
    </xsl:for-each>