3.5 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.

3.5.1 Context Nodes

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 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[3] selects the 3rd line element node backwards from the context node.

Even when the nodeset attribute on bind contains such a reverse axis, however, the processing still happens in forward document order, including how nodes are counted with position( ). Due to the potential for confusion, it's probably best to completely avoid reverse axes in XForms.

3.5.2 Model Binding Expressions

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.

3.5.3 UI Binding Expressions

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.

3.5.4 Computations

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 summarizes all computed expression used in XForms.

Table 3-2. Computed expression in XForms

Element

Attribute

Description

Datatype

bind

readonly

Determines whether a node is read-only

boolean

bind

required

Determines whether a node is required

boolean

bind

relevant

Determines whether a node is relevant

boolean

bind

constraint

Provides a predicate that must be satisfied for validity

boolean

bind

calculate

Provides an automatically computed value for a node

string[1]

setvalue

value

Provides a one-time computed value for a node

string[2]

insert

at

Provides an index at which to insert new nodes

number

delete

at

Provides an index at which to delete nodes

number

output

value

Provides an automatically computed value for display

string

[1] 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.

[2] As with calculate, the XPath string might have additional lexical constraints.



XForms Essentials
Xforms Essentials
ISBN: 0596003692
EAN: 2147483647
Year: 2005
Pages: 117
Authors: Micah Dubinko

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net