You are reading O'Reilly XForms Essentials by Micah Dubinko. (What is this?) - Buy XForms Essentials Online

How XPath is Used in XForms

Given a good understanding of what XPath is and how it works, it's pretty simple to see how it fits into the XForms architecture.

Every use of XPath in XForms involves a context node, usually with the effect of shortening the number of steps needed in the path expression. For example, if the instance data is a simple XHTML document:

<html:html xmlns:html="http://www.w3.org/1999/html">
  <html:head>
    <html:title>Mutant Registration Guidelines</html:title>
  </html:head>
  <html:body>
    <html:p>The White House announced today...</html:p>
  </html:body>
</html:html>

the default context node is the element node named html:html. Thus, to bind a form control to the document title, instead of the longer absolute path /html:html/html:head/html:title, a shorter relative path html:head/html:title could be used. The key difference between the two is that absolute paths contain a leading slash and the name of the root element. Since there can be only one root element, including its name in every path expressions isn't necessary—path expressions don't become ambiguous by leaving out what is really a redundant step along the path.

The default context node can be changed. Any element containing a binding expressions resets the context node for any child elements. Binding expressions include the ref attribute possibly with the model attribute, or alternatively the bind attribute. Either way, the expression selects a node-set from the instance data, and the first node, in document order, is used as the context node for child elements. Chapter 10, Form Accessibility, Design, and Troubleshooting shows a way to take advantage of this behavior to greatly simplify the use of XPath in XForms.

Within the markup for the XForms Model, there are a few things to be aware of regarding context nodes used for XPath expressions on the <bind> element. The nodeset attribute on this element selects an XPath node-set, applying certain properties such as calculate to each node. This means that the expression used will get evaluated multiple times, once for each node in the node-set. Upon each evaluation, the node being processed is the context node. This is most useful when a calculation appears in a repeating section. In the following example, each individual line item has a calculation that runs within the current line item only:

<!-- within each line item, calculate price times quantity -->
<xforms:bind nodeset="items/item/extension" calculate="../price * ../quantity"/>

Another use for this processing is to automatically number nodes in document order, using the XPath function position( ), which returns the "context position," or the position of the node being processed relative to the entire node-set. For example, the following would sequentially number every line element in the instance data, placing the result in an attribute named line-number:

<!-- apply sequential numbering to line elements -->
<xforms:bind nodeset="lines/line/@line-number" calculate="position(  )"/>