apache > cocoon

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


The JPath tags are defined in the namespace



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

<if test="XPathExpression">


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


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:

  <when test="XPathExpression">


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


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

<value-of select="XPathExpression"/>


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


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

<for-each select="XPathExpression">

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



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


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"]/>


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


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


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