Abbreviations


The XPath 1.0 location-path abbreviations are as follows:

  • You can omit child::.

  • A short form is available for a predicate that contains only a position value with an equal sign. The predicate [2] is a short form of [position()=2]. No short form is available for a predicate such as [position() < 2], which has an operator other than an equal sign. Also, if you use a Boolean operator, you must use the long form for the position value. The predicate [position()=1 or position()=5] is not expressed as [1 or 5], for example.

  • The at sign is used in front of an attribute name in a predicate but also functions as a short form of attribute::. In our main example, the following location path selects the string value 5.

     /Insured/@CustomerID 

  • The double virgule (//) lets you step through many levels of the tree, with the children of the context node potentially included as one of the selected nodes. The formal definition is /descendant-or-self::node()/.

    You can use the double virgule at the beginning of a location path or in the middle. Each of the next expressions includes all Vehicle nodes that are the first children of their parents.

     //Vehicle[1] /Insured//Vehicle[1] 

    In our case, each expression makes available the nodes for Honda Accord and Triumph Spitfire, displaying only the string value Honda Accord.

    If your XML source includes hundreds of lines and if you use the double virgule at the start of a location path rather than in the middle, the processing time may be too long for your purpose.

    The following example (not an abbreviation) may seem similar to //Vehicle[1], but selects only the first Vehicle node in the document.

     /descendant::Vehicle[1] 

    The displayed string value is Honda Accord.

  • A single period (.) is a short form for the context node; specifically, the period is a short form of self::node(), as noted earlier.

     /descendant::Make[.='Ford'] 

  • A double period (..) is a short form of parent::node(), which is the parent of the context node. The following example returns the five Vehicle nodes, starting with the one for Honda Accord.

     /descendant::Make/.. 

Examples with Descendants and Siblings

Some axis specifications cause behavior that you might not expect. Let's try examples with the descendant specification (which is straightforward) and with the following-sibling specification (which is less so).

The string value of the next expression refers to the third of the Insured node's Vehicle descendants whose Category value is Sport.

 /Insured/descendant::Vehicle[@Category='Sport'][3]/Make 

The string value is Porsche.

The string value of the following expression also is Porsche, which refers to the fifth Vehicle descendant of the Insured node.

 /Insured/descendant::Vehicle[last()]/Make 

The following expression selects two nodes - specifically, the second Vehicle descendant of each CarPolicy node. The displayed string value is Ford, from the first of the two selected nodes.

 /Insured/CarPolicy/descendant::Vehicle[last()]/Make 

We'll now introduce the union (|) operator, which selects nodes based on a prior and subsequent location path. The following expression retrieves one Vehicle node whose Category attribute value is Sedan and two whose Category attribute is Sport.

 /descendant::CarPolicy[1]/Vehicle[@Category='Sedan']/Make | /descen- dant::CarPolicy[2]/Vehicle[@Category='Sport']/Make 

The string value of the first retrieved node is Honda.

When you work with child or descendant elements, the position value [2] is the second child or descendent; but when you work with siblings, the position value [2] refers to the second sibling (forward or backward, depending on the axis specification). In the following expression, five Vehicle nodes are used at different times as the context node for the second-to-last location step (following-sibling::Vehicle), which selects three unique nodes.

 /Insured/CarPolicy/Vehicle/following-sibling::Vehicle/Make 

The string values of the selected Make nodes are as follows:

  • Ford is the only string value that is immediately available. That value is present because the expression selected all subsequent siblings of the first Vehicle child of the first CarPolicy node.

  • Buick is available if you use parentheses and the predicate [2], as in previous examples. That value is present because, in relation to the second CarPolicy node, the expression selected all subsequent siblings of the first Vehicle child.

  • Porsche is available if you use parentheses and the predicate [3]. That value is present because, in relation to the second CarPolicy node, the expression selected all subsequent siblings of the first and second Vehicle nodes and then removed the duplicate node that has string value Porsche. XPath 1.0 expressions never provide a duplicate node, although the option to retrieve duplicate nodes is available in XPath 2.0.

The following expression returns only one node because in only one case is a sibling a second sibling, two nodes away from a context node.

 /Insured/CarPolicy/Vehicle/following-sibling::Vehicle[2] 

The expression resolves to the string value Porsche Speedster, and the string value of the relevant context node is Triumph Spitfire.

Consider a variation:

 /Insured/descendant::Vehicle/following-sibling::Vehicle[2] 

The effect is precisely the same as that of the previous expression. The sibling relationships of the Vehicle nodes are not dependent on the CarPolicy context node. The node for Ford Mustang is never a sibling of the node for Triumph Spitfire, for example, no matter the location path.

Examples with a Reverse Axis

Let's explore the effect of using a reverse axis. As we said earlier, the meaning of reverse axis is that the context-position numbers are assigned in reverse XML-source order.

In the following case, the XPath processor returns the first-level ancestor node of each Make node.

 /descendant::Make/ancestor::*[1] 

The XPath processor returns five Vehicle nodes, each of which is the first ancestor of a Make node. The ancestor nodes are returned in XML-source order, not in reverse order. The string value of the first returned node is Honda Accord.

In the following case, the XPath processor returns the third-level ancestor node of each Make node.

 /descendant::Make/ancestor::*[3] 

The third-level ancestor node of every Make node is the Insured node. The string value is the concatenation of all text nodes in the XML source and contains the make and model of all five cars.

Last, notice the difference between our first ancestor example and the following one, which includes parentheses.

 (/descendant::Make/ancestor::*)[3] 

The parenthetical expression returns eight nodes (five Vehicle nodes, two CarPolicy nodes, and the Insured node). The predicate selects the third of those nodes in XML source order; in other words, it selects the first Vehicle node, whose string value is Honda Accord.

Location Steps in Summary

Now we can give you a big story in a few words. For a given location step, the XPath processor handles one context node at a time and fulfills four steps in relation to each context node:

  1. Selects all nodes in the axis specification.

  2. Retains each node that passes the node test.

  3. Assigns a number to each retained node in a way that reflects the XML-source order (for forward axes) or that reflects the opposite (for reverse axes).

  4. Processes each predicate in turn, in a loop that acts as follows:

    1. Reviews the Boolean expression.

      As described earlier, the predicate can include one or more values to be tested against position() and may include a value returned from last(). The XPath processor compares the test values against the numbers assigned in step 3. The value returned from last() is the highest number that was assigned in step 3.

    2. Retains all nodes for which the Boolean value in the predicate is true and removes the rest.

    3. Reassigns a number to each node, as needed to respond to the removal of nodes during step 4b.

XPath 1.0 always removes duplicate nodes, and XPath (whether 1.0 or 2.0) returns the nodes in XML-source order.




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