|
11.10 XML Flow Control ActionsThe XML flow control actions mimic the flow control actions from the JSTL Core library, except that the XML flow control actions operate on XPath expressions instead of EL expressions. See "General-Purpose Actions" on page 469, and see "Conditional Actions" on page 474 for more information about the corresponding actions from the Core library. Table 11.24 lists the XML flow control actions. For each of the actions listed in Table 11.24, there is a corresponding action from the Core library.
Syntax:[32]
Syntax #1: Without a body, stores the test result in a scoped variable
Syntax #2: With a body that's evaluated if the test condition is true.
Description: The <x:if> action evaluates an XPath expression and coerces the result to a boolean value. If that value is true, the <x:if> action evaluates its body content, if present; if not, the body content is ignored. You can also store the boolean result of that expression in a scoped variable that you specify with the var attribute, and optionally, the scope attribute. The <c:if> action works the same way, except that you specify an EL expression instead of an XPath expression. Attributes:
Constraints and Error Handling:
In a Nutshell: As with the <c:if> action, you can store the result of the expression in a scoped variable. You specify the name of that scoped variable with the var attribute and its scope with the scope attribute.
Syntax:
Description: The <x:choose> action provides a context for mutually exclusive conditions. The body of this action can only contain whitespace, one or more nested <x:when> actions, and an optional <x:otherwise> action. Attributes: none Constraints and Error Handling:
In a Nutshell: The body of an <x:choose> action can contain multiple <x:when> actions and an optional <x:otherwise> action. The body content of the first <x:when> action whose select attribute represents a true XPath expression is evaluated, and the other <x:when> actions and optional <x:otherwise> action are ignored. If none of the <x:when> actions have a true XPath expression, the body content of the optional <x:otherwise> action, if present, is evaluated.
Syntax:
Description: The body content of an <x:when> action is evaluated if that action's XPath expression�specified with the select attribute�is true and the action is the first <x:when> action�contained in an <x:choose> action�whose XPath expression evaluates to true. Attributes:
Constraints and Error Handling:
In a Nutshell: You specify an XPath expression with the select attribute, which <x:when> coerces to a boolean value. If that value is true and the <x:when> action is the first <x:when> action whose XPath expression evaluates to true, its body content is evaluated.
Syntax:
Description: The body content of an <x:otherwise> action is evaluated only if none of the preceding <x:when> actions contained in the same <x:choose> action evaluated to true. The action:default behavior]><x:otherwise> action, like the <c:otherwise> action, represents default behavior for mutually exclusive conditional statements. Attributes: none Constraints and Error Handling:
Syntax:[33]
Description: The <x:forEach> action iterates over a node-set returned from an XPath expression. The body content of the <x:forEach> action is evaluated for every node in the node-set. Attributes:
Constraints and Error Handling:
In a Nutshell: Like <c:forEach>, the <x:forEach> action iterates over a collection of objects. For <x:forEach>, that collection is a node-set that is the result of an XPath expression. The <x:forEach> action is a poor cousin of the <c:forEach> action. The latter offers six attributes and numerous amenities such as iterating over a range of items in a collection, iterating over integer values, and accessing a status variable. The <x:forEach> action, by contrast, offers only two attributes and none of the amenities listed above that are provided by the <c:forEach> action. It's important to understand that <x:forEach> sets the context node for XPath expressions inside its body content; for example, the following code fragment iterates over nodes from an XML Rolodex:
In the preceding code fragment, every time the <x:forEach> action iterates over its body, it resets the context node. This feature simplifies XPath expressions within the body of <x:forEach> because they don't have to specify an absolute path. Because <x:forEach> sets the context node, the need to specify the var attribute, which specifies the name of a scoped variable that refers to the current item of the iteration, is greatly diminished. |
|
No comments:
Post a Comment