xsl:choose


The <xsl:choose> instruction defines a choice between a number of alternatives.

If there are two alternatives it performs the equivalent of if-then-else in other languages; if there are more than two, it performs the equivalent of a switch or select statement.

Changes in 2.0

There are no changes to the syntax of <xsl:choose> in XSLT 2.0. However, by using <xsl:sequence> instructions within the <xsl:when> or <xsl: otherwise > branch, it is now possible to use <xsl:choose> in cases where the required result is an atomic value, or a reference to an existing node. In XSLT 1.0, the result always consisted of newly constructed nodes.

Format

 <xsl:choose>   <!-- Content: (xsl:when+, xsl:otherwise?) --> </xsl:choose> 

Position

<xsl:choose> is an instruction; it is always used within a sequence constructor.

Attributes

None.

Content

One or more <xsl:when> elements.

Optionally, an <xsl:otherwise> element, which must come last if it is present at all.

Effect

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

  • The first <xsl:when> element whose test expression is true is selected. Subsequent <xsl:when> elements are ignored whether or not their test expression is true. The test expression is evaluated to obtain its effective boolean value: The rules for this are given under <xsl:if> , on page 310.

  • 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> element, no element is selected, and the <xsl:choose> element therefore has no effect (it returns an empty sequence).

  • The selected child element (if any) is executed by evaluating its sequence constructor in the current context. So the effect is as if the relevant sequence constructor appeared in place of the <xsl:choose> instruction.

The test expression in <xsl:when> elements after the selected one is not evaluated.

Usage

The <xsl:choose> instruction is useful where there is a choice of two or more alternative courses of action. It thus performs the functions of both the if-then-else and switch or Select Case constructs found in other programming languages.

Using <xsl:choose> with a single <xsl:when> instruction and no <xsl:otherwise> is permitted, and means exactly the same as <xsl:if> . Some people suggest writing every <xsl:if> instruction this way, to save rewriting it later when you discover that you want an else branch after all.

When <xsl:choose> is used within the body of an <xsl:variable> (or <xsl:param> or <xsl: with-param > ) element, the effect is a conditional assignment: the relevant variable is assigned a different value depending on the conditions.

Examples

The following example returns the name of a state in the USA based on a two-letter abbreviation for the state. If the abbreviation is not that of a recognized state, it outputs the abbreviation itself.

  <xsl:choose>   <xsl:when test="state='AZ'">Arizona</xsl:when>   <xsl:when test="state='CA' ">California</xsl:when>   <xsl:when test="state='DC'">Washington DC</xsl:when>   ...   <xsl:otherwise><xsl:value-of select="state"/></xsl:otherwise>   </xsl:choose>  

The following example declares a variable called width and initializes its value to the width attribute of the current node, if there is one, or to 100 otherwise.

  <xsl:variable name="width" as="xs:integer">   <xsl:choose>   <xsl:when test="@width">   <xsl:sequence select="@width"/>   </xsl:when>   <xsl:otherwise>   <xsl:sequence select="100"/>   </xsl:otherwise>   </xsl:choose>   </xsl:variable>  

You might be tempted to write this as follows:

  <!--WRONG-->   <xsl:choose>   <xsl:when test="@width">   <xsl:variable name="width" select="@width"/>   </xsl:when>   <xsl:otherwise>   <xsl:variable name="width" select="100"/>   </xsl:otherwise>   </xsl:choose>   <!--WRONG-->  

This is legal XSLT, but it does not achieve the required effect. This is because both the variables called «width » have a scope that is bounded by the containing element, so they are inaccessible outside the <xsl:choose> instruction.

Everyone has personal preferences when coding. I tend to prefer constructs that are more compact than <xsl:choose> . I would probably write the above example as:

  <xsl:variable name="width" select="(@width, 100)[1]" as="xs:integer"/>  

See Also

  • <xsl:when> on page 438

  • <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