XML Control Flow Tags — Functional Description

<x:forEach>
<x:if>
<x:choose> <x:when> <x:otherwise>


1. Introduction

The core set of XML tags provides the basic functionality to easily parse and access XML data. Another important piece of functionality is the ability to iterate over elements in an XML document, as well as conditionally process JSP code fragments depending on the result of an XPath expression. The actions presented in this document provide these capabilities.

2. <forEach>

The <forEach> action evaluates the given XPath expression and iterates over the result, setting the context node to each element in the iteration. For example:

<x:forEach select="$doc//author">
  <x:valueOf select="@name">
</x:forEach>

Optional attribute "var" makes it possible to save the current context node of the iteration in a JSP "page" attribute. The exported object has nested visibility.

3. <if>

The <if> action has a "select" attribute that specifies an XPath expression. The expression is evaluated and the resulting object is converted to a boolean according to the semantics of the XPath boolean function. The tag renders its body if the result is true. For example:

<x:if select="$customer/[location='UK']">
  UK based
</x:if>

4. <choose> <when> <otherwise>

The <choose> action selects one among a number of possible alternatives. It consists of a sequence of <x:when> elements followed by an optional <x:otherwise>. Each <x:when> element has a single attribute, select, which specifies an XPath expression. When a <x:choose> element is processed, each of the <x:when> elements has its expression evaluated in turn, and the resulting object is converted to a boolean according to the semantics of the XPath boolean function. The body of the first, and only the first, <x:when> whose result is true is rendered.

If none of the test conditions of nested <when> tags evaluates to true, then the body of an <otherwise> tag is evaluated, if present.

<x:choose>
  <x:when test="$customer/firstName">
    Hello <xtags:valueOf select="$customer/firstName"/>
  </x:when>
  <x:otherwise>
    Hello there friend
  </x:otherwise>
</x:choose>

5. XPath "boolean" function

As specified in the XPath spec, the boolean function converts its arguments to a boolean as follows:

6. Summary

XML Control Flow Tags
Element Sample usage
<forEach>
 select var

Evaluates the given XPath expression and iterates over the result, setting the context node to each element in the iteration.
<x:forEach select="$doc//author">
  <x:valueOf select="@name">
</x:forEach>

<if>
 select

Simple conditional tag, which evalutes its body if the supplied Xpath expression evaluates to true (as if by a call to the XPath "boolean" function).

<x:if select="$customer/[location='UK']">
  UK based
</x:if>

<choose>
<when>
 select

<otherwise>


Simple conditional tag that establishes a context for mutually exclusive conditional operations, marked by <when> and <otherwise>.

<x:choose>
  <x:when select="...">
    ...
  </x:when>

  <x:when select="...">
    ...
  </x:when>

  ...
  <x:otherwise>
    ...
  </x:otherwise>

</x:choose>