apache > cocoon

- Home

# Advanced Control Flow - JPath

## JPath Logic Sheet

The JPath Logic Sheet is an XSP logic sheet that allows you to access data from a Cocoon Flowscript in an XSP page and inject it into a Cocoon pipeline. It provides a set of tags (similar to the those defined by XSLT) that allow you to iterate over Java collections (and Java or JavaScript arrays) and to test for the presence of optional or alternate bean properties. It is based on Apache JXPath.

Samples of this Logic Sheet are avaliable in the XSP implementation of the petstore block

## Tags

The JPath tags are defined in the namespace

    http://apache.org/xsp/jpath/1.0

### if

The `if` tag allows the conditional execution of its body according to value of its `test` attribute:

    <if test="XPathExpression">
      body
    </if>

Example:

    <jpath:if test="cart/numberOfItems = 0">
      Your cart is empty
    </jpath:if>

### choose

The `choose` tag performs conditional block execution by its embedded `when` sub tags. It renders the body of the first `when` tag whose `test` condition evaluates to true. If none of the `test` conditions of its nested `when` tags evaluate to `true`, then the body of its `otherwise` tag is evaluated, if present:

    <choose>
      <when test="XPathExpression">
        body
      </when>+
      <otherwise>
        body
      </otherwise>?
    </choose>

Example:

    <choose>
      <when test="not(user/loggedIn)">
         You're not logged in
      </when>
      <otherwise>
         You're already logged in
      </otherwise>
    </choose>

### value-of

The `value-of` tag evaluates an expression and outputs the result of the evaluation:

    <value-of select="XPathExpression"/>

Example:

    <value-of select="cart/numberOfItems">

### for-each

The `for-each` tag allows you to iterate over a collection of objects:

    <for-each select="XPathExpression">
      body
    </for-each>

When using XPath expressions within `for-each` the current element is the context node and can be referenced with XPath dot operator:

    .

Example:

    <for-each select="cart/cartItems[position() <= $count]">
       <td><value-of select="./productId"></td>
    </for-each>

### continuation

The `continuation` tag returns the id of the current web continuation of your Flowscript. You can refer to previous continuations by supplying the optional `level` attribute. Zero is the current level, `-1` refers to the previous continuation, and so on.

    <continuation [level="Number"]/>

Example:

    <xsp:attribute name="action"><xsp:expr><jpath:continuation/>+".form"</xsp:expr></xsp:attribute>

### set-lenient

Note
Since Cocoon 2.1.6

The `set-lenient` tag switch the lenient setting of jxpath. By default it is set to false. If set to true, xpaths that don't point to an object or a set of them will return null (Instead of that annoying exception). Saves all the checking if you can accept nulls and are sure there are no typos.

If the tag contains child elements it will scope the setting for the enclosed elements, otherwise it is a global setting. You can't nest <jpath:set-lenient> tags.

            <jpath:set-lenient lenient="true|false">

Example:

            <jpath:set-lenient lenient="true">

Errors and Improvements? If you see any errors or potential improvements in this document please help us: View, Edit or comment on the latest development version (registration required).