xsl:when


The <xsl:when> element always appears as a child of <xsl:choose> . It defines a condition to be tested and the action to be performed if the condition is true.

Changes in 2.0

None.

Format

 <xsl:when   test = expression>   <!-- Content: sequence-constructor --> </xsl:when> 

Position

<xsl:when> is always a child element of <xsl:choose> . There must be at least one <xsl:when> element within an <xsl:choose> element.

Attributes

Name

Value

Meaning

test

mandatory

Expression

The boolean condition to be tested

Content

A sequence constructor.

Effect

The <xsl:choose> element is evaluated as follows :

  • The first <xsl:when> element whose test expression has an effective boolean value of true is selected; subsequent <xsl:when> elements are ignored regardless of the value of their test expression.

  • If none of the <xsl:when> elements has a test expression that is true , the <xsl: otherwise > element is selected. If there is no <xsl:otherwise> instruction, no element is selected.

  • The sequence constructor contained in the selected child element (if any) is evaluated in the current context: that is, the effect is as if the relevant sequence constructor appeared in place of the <xsl:choose> instruction.

Any XPath expression may be evaluated to obtain an effective boolean value. In brief, the rules are:

  • If the value of the expression is an empty sequence, the effective boolean value is false.

  • If the value of the expression is a single atomic value, the effective boolean value is false if the value is a zero-length string, the boolean value false , a number (integer, decimal, float, or double) equal to zero, or the special value NaN (not a number). Otherwise the effective boolean value is true .

  • If the value of the expression contains a node, the effective boolean value is true .

  • If the value of the expression is a sequence of two or more items, the effective boolean value is true .

The test expression in a <xsl:when> element after the selected one is not evaluated. This means it is safe to write code such as this:

  <xsl:choose>   <xsl:when test="not($x castable as xs:integer)">   <xsl:sequence select="-1"/>   </xsl:when>   <xsl:when test=''xs:integer($x) lt 0">   <xsl:sequence select="-1"/>   </xsl:when>   <xsl:otherwise>   <xsl:sequence select="xs:integer($x)"/>   </xsl:otherwise>   </xsl:choose>  

The conversion to an integer will not be attempted, and cannot therefore cause a failure, if the condition in the first <xsl:when> is true.

Usage and Examples

See <xsl:choose> on page 236

See Also

  • <xsl:choose> on page 236

  • <xsl:otherwise> on page 374

  • <xsl:if> on page 309




XSLT 2.0 Programmer's Reference
NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)
ISBN: 764569090
EAN: 2147483647
Year: 2003
Pages: 324

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net