Introducing Location Paths

How do location paths work? As we're going to see in Chapter 3, location paths are much like the standard file directory paths that you're already familiar with. For example, you might store data on stocks for April 2004 in a directory with this path if you're using Windows:


or this path if you're using Unix:


That's much how a location path works. A location path consists of one or more location steps , separated by / or // . You use successive stepsseparated by / to get to the data you want, just as you use successive directories in a file path to narrow down the location of a file. Starting the whole location path with a / means that you're starting from the root node (the very beginning of the document, before anything else), and working from that point.

As we're going to see in Chapter 3, a location step, in turn , is made up of an axis , a node test , and zero or more predicates .

Here's an example. In the location step child::planet[position() = 5] , child is the name of the axis (which you follow with :: ), planet is the node test, and [position() = 5] is a predicate. In this case, the child axis selects child nodes of the node you start from. The node test, planet , will match <planet> elements. And the predicate consists of an XPath function, position , that must return a value of 5 to make the predicate true. Putting all that together, then, this location step selects the fifth <planet> child element.

You create location paths with one or more location steps, such as /child::planet[position() = 5]/child::name , which starts at the root node and selects all the <name> elements that are children of the fifth <planet> child element in the document.

The evaluation of each location step in a location path starts at its context node . The idea of a context node is an important one, because as you navigate deep into an XML document, each successive location step starts from a different location, which XPath calls the location step's context node. In the present example, / makes the context node the document's root node for the next location step, which is child::planet[position() = 5] . That location step in turn makes the context node the fifth <planet> child element for the next location step, which is child::name . The thing to remember is that the evaluation of each location step starts from its context node.

Here are a number of location path examples to get us startedwe're going to see how to create these in detail over the next three chapters, so don't try to memorize any of these now. But it's worth taking a look at these examples in overview:

  • child::planet returns the <planet> element children of the context node.

  • child::* returns all element children ( * only matches elements) of the context node.

  • child::text() returns all text node children of the context node.

  • child::node() returns all the children of the context node, no matter what their node type is.

  • attribute::units returns the units attribute of the context node.

  • child::name/descendant::planet returns the <planet> element descendants of the child <name> elements of the context node.

  • child::*/child::planet returns all <planet> grandchildren of the context node.

  • / returns the root node (that is, the parent of the document element).

  • child::planet[position() = 3] returns the third <planet> child of the context node.

  • child::planet[position() = last()] returns the last <planet> child of the context node.

As you can see, some of this syntax is pretty involved, and a little lengthy to type. However, there is an abbreviated form of XPath syntax, as we're going to see in Chapter 3.

XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: