Context


Let's return to the language itself.

The XPath processor evaluates a location path one step at a time, and each node that is selected during a given step provides a context, which is information that limits which nodes are available in the next step.

As suggested in Figure 6.1, if we descend the node tree /Insured/CarPolicy/Vehicle one generation at a time

  • a single Insured node provides a context that limits us to selecting (at most) the two CarPolicy nodes

  • the first of the CarPolicy nodes provides a context that limits us to selecting (at most) the first two Vehicle nodes

  • the second CarPolicy node provides a context that limits us to selecting (at most) the last three Vehicle nodes

image from book
Figure 6.1: Context

The node that provides a context at a given time is called the context node.

Perhaps the best way to understand a location path is to consider the location-path characters that act as operators. As we saw earlier, if an expression begins with a virgule, the search starts at the root node. The essential point, however, is that if the virgule is placed between one location step and the next (in CarPolicy/Vehicle, for example), the XPath processor selects in turn each node (in this case, each CarPolicy node) that was retained by the location step at the left of the virgule. The node selected at a given time is the context node. The processor then uses that node when evaluating the location step at the right of the virgule. The virgule completes its operation only when, for each context node, the XPath processor evaluates the right-side location step.

Similarly, the presence of a predicate (in Vehicle[@Category='Coupe'], for example) causes the XPath processor to select in turn each node (in this case, each Vehicle node) that was retained by the syntax that immediately precedes the predicate in the same location step. The selected node is the context node. The processor retains that node only if the predicate evaluates to true. The predicate completes its operation only when, for each context node, the XPath processor evaluates the predicate.

In both cases, the node used "in turn" is the context node.

Our definition of context node is the one you need as you explore location paths, but be aware that we're using a subset of the W3C definition, which states that the context node is the node being processed at a given time. The W3C definition and the explanations required to make it meaningful are geared to developers of XPath processors.

The word "context" is included in additional terms. In some cases, context position reflects the position of the context node in the sequence of possible context nodes. That position ranges from 1 to the context size, which is the number of nodes in the set of nodes that are each used (in turn) as a context node. In a broader sense, context position reflects the position of a node in a node set, and context size is the number of nodes in that set. We'll show the different uses of these terms as appropriate.

You can use a positional predicate, which is a predicate that restricts the addressed nodes based on the context position. For example, the following expression restricts the selection to the second Vehicle child within each CarPolicy node.

 /Insured/CarPolicy/Vehicle[position()=2] 

Those nodes represent Ford Mustang and Buick Skylark. As described later, however, the only string value that is displayed (if you're trying out these examples) is Ford Mustang.

If the comparison operator is an equal sign (=), an abbreviated form of a positional predicate is valid. The last expression can be stated as follows.

 /Insured/CarPolicy/Vehicle[2] 

Other operators are valid, too, as listed later. You can select the nodes at any position greater than 1, for example.

 /Insured/CarPolicy/Vehicle[position()> 1] 

Here, the returned nodes represent Ford Mustang, Buick Skylark, and Porsche Speedster. However, only Ford Mustang is displayed as a string value.

You can verify the number of returned nodes by using the XPath function count, which takes a node set as its only argument. The next example returns the value 3.

 count(/Insured/CarPolicy/Vehicle[position()> 1]) 




SOA for the Business Developer. Concepts, BPEL, and SCA
SOA for the Business Developer: Concepts, BPEL, and SCA (Business Developers series)
ISBN: 1583470654
EAN: 2147483647
Year: 2004
Pages: 157
Authors: Ben Margolis

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