NetBeansв„ў IDE Field Guide: Developing Desktop, Web, Enterprise, and Mobile Applications (2nd Edition)

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

Категории