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( )"/>
In XPath, certain axes follow the reverse of document order: ancestor, ancestor-or-self, preceding, and preceding-sibling. Thus, an expression like preceding-sibling::line selects the 3rd line element node backwards from the context node.
A Model binding expression always appears on the attribute nodeset of the element bind. The expression must be a Location Path that returns a node-set. That node-set, in turn, is the target of all the model item properties, such as required or readonly, specified on the same bind element.
A UI binding expression always appears on a form control or user interface related component of XForms. Like a Model binding expression, it must be a Location Path that returns a node-set. There two possible attribute names, ref and nodeset, which are used to differentiate between processing that takes into account only the first node and the entire node-set, respectively.
In other places in XForms, an XPath expression is used as a lightweight scripting language. Such expressions are defined as taking various return datatypes. This usage of XPath is called a computed expression. Table 3.2, “Computed expression in XForms ” summarizes all computed expression used in XForms.
Table 3.2. Computed expression in XForms
Determines whether a node is read-only
Determines whether a node is required
Determines whether a node is relevant
Provides a predicate that must be satisfied for validity
Provides an automatically computed value for a node
Provides a one-time computed value for a node
Provides an index at which to insert new nodes
Provides an index at which to delete nodes
Provides an automatically computed value for display
[a] A string as far as XPath is concerned, but must also match the XML Schema lexical datatype associated with that node to avoid causing form invalidation.
[b] As with calculate, the XPath string might have additional lexical constraints.