Microsoft XML 2.5 SDK


 

xsl:when Element

[This is preliminary documentation and subject to change.]

Provides multiple conditional testing in conjunction with the xsl:choose and xsl:otherwise elements.

Syntax

<xsl:when

    expr="script-expression"

    language="language-name"

    test="pattern"  >

</xsl:when>

Attributes

expr

Script expression evaluating to a Boolean. If this expression returns TRUE, and the test pattern succeeds, the contents of xsl:when are placed in the output. If one of these tests fails, the next xsl:when or xsl:otherwise in the xsl:choose block is tested.

language

Active Scripting language used for the expression in the expr attribute. If left unspecified, the current scripting language is used, as specified by a language attribute on an ancestor. If no such attribute exists, JScript® (compatible with ECMA 262 language specification) is used. This attribute accepts the same values as the language attribute on the HTML SCRIPT element.

test

XSL pattern to be tested against the current context. The default value is ".", which indicates evaluation of the current node and thus is always true. For a complete description, see XSL Pattern Syntax.

Element Information

Number of occurrences Unlimited
Parent elements xsl:choose
Child elements xsl:apply-templates, xsl:attribute, xsl:choose, xsl:comment, xsl:copy, xsl:element, xsl:eval, xsl:for-each, xsl:if, xsl:pi, xsl:value-of
Requires closing tag Yes. XSL is an XML grammar and, like all XML grammars, all tags must have closing tags to satisfy the definition of well-formed.

Remarks

Describes one of the alternatives to be chosen by the xsl:choose. The default alternative is described by the xsl:otherwise element.

For simple conditional testing, use the xsl:if element.

Example

This example shows a template for "order" elements, and inserts an HR or BR before the order's contents based on the order's "total" element value. If the total is less than 10, a red HR will be generated; if the total is less than 20, a pink HR will be generated; otherwise a BR element will be created.

<xsl:template match="order">
  <xsl:choose>
    <xsl:when test="total[. $lt$ 10]">
      <HR STYLE="color:red"/>
    </xsl:when>
    <xsl:when test="total[. $lt$ 20]">
      <HR STYLE="color:pink"/>
    </xsl:when>
    <xsl:otherwise>
      <BR/>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:apply-templates />
</xsl:template>

See Also

Conditional Templates, Influencing Transformations Through Script