Adding an <xsl:choose> element to your XSL file
You can use the XSL Conditional Processing wizard to create <xsl: choose> elements for XSL files. The <xsl: choose> element enables you to select an option from a sequence of alternatives.
It is composed as follows:
<xsl: choose> <xsl: when test ="Boolean expression"> <xsl: when test ="Boolean expression"> <xsl:otherwise> </xsl: choose>Each <xsl: when> element has a test condition. The test condition is a Boolean expression, which is an XPath expression that is converted to a Boolean value. The contents of the first <xsl: when> element whose test condition is true is output to a result tree.
As well, the <xsl: choose> element may have an optional <xsl:otherwise> element whose contents are output only if no test conditions of any <xsl: when> element are true. If no <xsl:otherwise> element exists and none of the test conditions in any of the <xsl: when> child elements are true, then the <xsl: choose> element does not produce any output.
The following is an example of an <xsl: choose> element:
<xsl: choose> <xsl: when test = "number [ . > 2000]">A big number</xsl:when> <xsl:otherwise>A small number</xsl:otherwise> </xsl:choose>If the number element in the target XML file contains a number value greater than 2,000, then the string "A big number" is inserted into the result tree. In any other case, "A small number" will be inserted.
The following instructions were written for the Resource perspective, but they will also work in many other perspectives.
To create an <xsl: choose> element,...
- Open the XSL file you want to add an <xsl: choose> element to.
- In the XSL editor, insert your cursor in the location you want the <xsl: choose> element added.
- Click the Snippets tab, open the XSL drawer, and then double-click Conditional logic with xsl:choose element. The XSL Conditional Processing wizard opens.
- Click Add When to add an <xsl: when> condition.
- Select it.
- In the Test field, enter your XPath expression. You can enter it manually or create it using the XPath expression wizard (click XPath to launch it).
- In the Content field, enter the content you want added to the result tree when the test condition comes true.
- Repeat steps 4-7 for any other <xsl: when> conditions you want to add.
- (Optional) Click Add Otherwise to add an <xsl:otherwise> condition.
- Select it. You cannot specify a Test condition.
- In the Content field, enter the content you want added to the result tree if none of the other <xsl: when> elements are true.
- The grayed out area shows the contents of <xsl: choose> statement. You cannot edit it directly there.
- To edit a condition, select it and edit the fields as necessary.
- To remove a condition, select it and click Remove.
- When you are finished with your <xsl:choose> element, click Finish.
The <xsl:choose> elements will be inserted into your XSL file.
Parent topic
Editing XSL files