Understanding Location Steps and Paths

How do location paths work? We took a look at location paths in the overview in Chapter 1, where we saw that location paths look much like directory paths. For example, you might store section one of chapter one of part one of a novel in a directory with this path if you're using Windows:

 
 \novel\part1\section1\chapter1 

or this path if you're using Unix:

 
 /novel/part1/section1/chapter1 

The idea behind XPath location paths is very much the same except that, as we'll see in this chapter and the next , the XPath syntax can get much more complex and detailed than directory paths. Like directory paths, you build location paths out of individual steps, called location steps , separated by / or // . You use these steps to specify the data you want, just as you use successive directories in a file path to get to the location of a file.

For example, in the XPath location path /library/book/title[2]/text() , we begin with a / , which matches the root node of the document, followed by, in order:

  • The location step library , which matches the child <library> element of the root node

  • The location step book , which matches the child <book> element of the <library> element

  • The location step title[2] , which matches the second <title> element of the <book> element

  • text() , which matches the text in the <title> element

In fact, the XPath location path /library/book/title[2]/text() uses abbreviated XPath syntax, which we're going to see in this chapter. Here's the full version of this XPath location path, where the child element nature of each 00successive location step is spelled out:

 
 /child::library/child::book/child::title[position()=2]/child::text 

Okaynow let's get to the details.

The Parts of a Location Step

Each location step is made up of an axis , a node test , and zero or more predicates, like this (where the * symbol means "zero or more of"):

 
  axis  ::  node-test  [  predicate  ]* 

For example, in the location step child:: name [position() = 2] , child is the name of the axis (which you follow with :: ), name is the node test and [position() = 2] is the predicate. In this case, the child axis selects child nodes of the node you start at, which is the context node. We're going to take a look at axes, node tests, and predicates in detail in this chapter. This location step can also be abbreviated, using the abbreviated syntax we'll see in this chapter, as name[2] (this works because the child axis is the default axis, and when you use position predicates like [position() = 2] , XPath lets you abbreviate them as simply [2] ).

Location paths can be made up of one or more location steps, such as child::name[position() = 2]/child::firstName , which selects all the <firstName> elements that are children of the second <name> child element of the context node. This location path can also be abbreviated as name[2]/firstName .

When you start the location path (not a location step) with / or // , the location path is called an absolute location path because you're specifying the path from the root node of the XML document (just as starting a file path with / in Unix starts the path from the root directory and makes the path an absolute one). Otherwise, the location path is relative , starting with the context node.

For example, take a look at this node tree again:

 
 root                                           element: <library>                                            element: <book>                                         -------------------                                          element: <title>        element: <title>                                    text: "I Love XPath"        text: "XPath is the BEST" 

Beginning a location path with / (or // , as we'll see) makes that path start with the document's root node, making this an absolute location path and making the root node the context node for the first location step in the path. To move to the child <library> element, you add the location step library like this: /library (using unabbreviated syntax, that would be /child::library ). That makes the context node for the next location step the <library> element. To move to the <book> element starting from the <library> context node, you add book (or child::book ) to the location path like this: /library/book and so on, all the way down to the text in the second <title> element, which you reach with /library/book/title[2]/text() . These are all absolute location paths, starting from the root node.

On the other hand, if you've got a context node already setsome XPath software lets you specify context nodesyour location paths can be relative. For example, if you've set the context node to the <library> element, the relative location path book/title[2]/text() will locate the text in the second <title> element in the document.

Now it's time to get systematic about our study of location steps, which we're going to do by taking a look at what kind of axes, node tests, and predicates XPath are available.

XPath Axes

We're going to take a look at all 13 XPath 1.0 axes in this chapter. For example, in the location step child::planet , which refers to all <planet> elements that are children of the context node, child is the axis (and as you now know, you can also abbreviate this location step as planet , because child is the default axis). Here are all the XPath 1.0 axes:

  • The ancestor axis holds the ancestors of the context node; the ancestors of the context node are the parent of context node and the parent's parent and so forth, back to and including the root node.

  • The ancestor-or-self axis holds the context node and the ancestors of the context node.

  • The attribute axis holds the attributes of the context node.

  • The child axis holds the children of the context node.

  • The descendant axis holds the descendants of the context node. A descendant is a child or a child of a child and so on.

  • The descendant-or-self axis contains the context node and the descendants of the context node.

  • The following axis holds all nodes in the same document as the context node that come after the context node.

  • The following-sibling axis holds all the following siblings of the context node. A sibling is a node on the same level as the context node.

  • The namespace axis holds the namespace nodes of the context node.

  • The parent axis holds the parent of the context node.

  • The preceding axis contains all nodes that come before the context node.

  • The preceding-sibling axis contains all the preceding siblings of the context node. A sibling is a node on the same level as the context node.

  • The self axis contains the context node.

Each XPath location step must specify an axis (or use the default child axis), as in this location path we've already seen: /child::library/child::book/child::title[2]/child::text .

You can also see the various XPath axes listed in Table 3.1, which lists the support for these axes by XML processors in Internet Explorer. The XML processor in Internet Explorer is called MSXML; MSXML 2.0 was the version in Internet Explorer 5.0, early versions of Internet Explorer 6.0 used MSXML3, and the current version is MSXML4. If you're using the .NET platform, your version of MSXML is MSXML.NET.

Table 3.1. The XPath Axes

AXIS

ABBREVIATION

MSXML2

MSXML3

MSXML4

MSXML.NET

ancestor

   

x

x

x

ancestor-or-self

   

x

x

x

attribute

@

 

x

x

x

Child

(default)

x

x

x

x

descendant

//

x

x

x

x

descendant-or-self

 

x

x

x

x

following

   

x

x

x

following-sibling

 

x

x

x

 

namespace

   

x

x

x

Parent

..

x

x

x

x

preceding

   

x

x

x

preceding-sibling

 

x

x

x

 

Self

 

x

x

x

x

XPath Node Tests

When you use an axis in a location step, you're telling XPath where to look and identifying a set of nodes. A node test tells XPath which of the nodes in that set you're interested in.

There are a number of ways to create node tests. You can use names of nodes as node tests, or the wildcard * to select element or attribute nodes (note especially that * matches only elements and attributes, not just any kind of node). For example, the location step child::*/child::name selects all <name> elements that are grandchildren of the context node. To match attributes, you'd use the attribute axis like this: attribute::* . Besides node names and the wildcard character, you can also use these node tests in XPath 1.0:

  • The * wildcard character matches any element or attribute name.

  • A name matches a node with that name (for example, planet will match a <planet> element).

  • The comment() node test selects comment nodes.

  • The node() node test selects any type of node.

  • The processing-instruction() node test selects a processing instruction node. You can specify the name of the processing instruction to select in the parentheses.

  • The text() node test selects a text node.

You can see the XPath 1.0 node tests in Table 3.2, along with the XML processor version that supports them in the Internet Explorer.

Table 3.2. The XPath Node Tests

AXIS

MSXML2

MSXML3

MSXML4

MSXML.NET

*

x

X

x

x

name

x

X

x

x

comment()

x

X

x

x

node()

x

X

x

x

processing-instruction()

x

X

x

x

text()

x

x

x

x

The node test lets you specify what nodes you want to work with in an XPath location step. For example, take a look at our sample node tree:

 
 root                                           element: <library>                                            element: <book>                                         -------------------                                          element: <title>        element: <title>                                    text: "I Love XPath"        text: "XPath is the BEST" 

You can start at the root node with / , and then use the child axis and the node test library to move to the <library> elementgiving you the location path /child::library . You can see this at work in the XPath Visualiser in Figure 3.1.

Figure 3.1. Using the XPath location step /child::library .

graphics/03fig01.jpg

And you can move to the <book> element under the <library> element with another location step involving this child axis and a node test like this: /child:library/child:book . You can see what this looks like in the XPath Visualiser in Figure 3.2.

Figure 3.2. Using the XPath location step /child::library/child::book .

graphics/03fig02.jpg

The next level down in the node tree holds two <title> elements, however. What if we only want to work with the second one? If we used the location path /child::library/child::book/child::title , we'd match both <title> elements, so we need more than a node test herewe need to use a predicate.

XPath Predicates

The next part of a location step, which follows the node text, is the predicate. A location step doesn't need a predicate, but if you use a predicate, you can specify even more about the node or nodes you want to match.

You often use one of the built-in XPath functions in predicates. For example, take a look at the location step child::planet[position() = 2] . In this case, the predicate, which is always enclosed between [ and ] , is position() = 2 . This means that the value the built-in XPath function position() returns must indicate that this is the second <planet> child in order for the location step to match (this location step can also be abbreviated as planet[2] ). In this way, this predicate narrows down the search from the node-set of all <planet> children of the context node down to the second <planet> child.

Now we're in a position to select the second <title> element in our XML document that has this node tree:

 
 root                                           element: <library>                                            element: <book>                                         -------------------                                          element: <title>        element: <title>                                    text: "I Love XPath"        text: "XPath is the BEST" 

To move to the second <title> element, we can use the location path /child::library/child::book/child::title[2] , as you see in the XPath Visualiser in Figure 3.3 (this location path can also be abbreviated as /library/book/title[2] ).

Figure 3.3. Using the location step /child::library/child::book/child::title[2] .

graphics/03fig03.jpg

As you can see, expressions in predicates can let you narrow down the search from a whole node-set to just the nodes you're looking for.

Here's another node-test exampleto select the <title> element that contains the text "I Love XPath", you can use this XPath location path with the text() node test: /child::library/child::book/child::title[text()="I Love XPath"] , as you see in Figure 3.4.

Figure 3.4. Selecting a <title> element.

graphics/03fig04.jpg

Now we're able to construct XPath location paths like /child::library/child::book/child::title[2] using axes, node tests, and predicates. Each part of a location step, the axis, node test, and predicate, narrows down the set of nodes you're working with.

You can also build up XPath expressions much as you can location paths. For example, the text() node test will return the text in a node, so if you want to extract the text of the <title> element, you can use the XPath expression /child::library/child::book/child::title[2]/text() , which evaluates not to a node-set, but to a text string, "XPath is the BEST", as you can see in Figure 3.5.

Figure 3.5. Using /child::library/child::book/child::title[2]/text() .

graphics/03fig05.jpg

You can use multiple predicates in the same location stepfor example, say that we added a language attribute to each <planet> element in our planetary data XML document, as you see in ch03_01.xml (see Listing 3.1).

Listing 3.1 Adding a Language Attribute ( ch03_01.xml )
 <?xml version="1.0" encoding="utf-8"?> <planets>  <planet language="English">  <name>Mercury</name>         <mass units="(Earth = 1)">.0553</mass>         <day units="days">58.65</day>         <radius units="miles">1516</radius>         <density units="(Earth = 1)">.983</density>         <distance units="million miles">43.4</distance><!--At perihelion-->     </planet>  <planet language="English">  <name>Venus</name>         <mass units="(Earth = 1)">.815</mass>         <day units="days">116.75</day>         <radius units="miles">3716</radius>         <density units="(Earth = 1)">.943</density>         <distance units="million miles">66.8</distance><!--At perihelion-->     </planet>  <planet language="English">  <name>Earth</name>         <mass units="(Earth = 1)">1</mass>         <day units="days">1</day>         <radius units="miles">2107</radius>         <density units="(Earth = 1)">1</density>         <distance units="million miles">128.4</distance><!--At perihelion-->     </planet> </planets> 

What if we wanted to reach the second <planet> element that has a language attribute set to "English"? We could do that with a location path like this: /planets/planet[attribute::language = "English"][position() = 2] , as you see in Figure 3.6. In this way, you can handle multiple conditions with multiple predicates.

Figure 3.6. Using /planets/planet[attribute::language = "English"][position() = 2] .

graphics/03fig06.jpg

Some Examples of XPath Location Paths

There's nothing like seeing all this at work to understand what's going on, so here are a number of location path examples:

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

  • child::* Returns all element children ( * only matches elements, or attributes if you use it with the attribute axis) 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.

  • descendant::planet Returns the <planet> element descendants of the context node.

  • ancestor::planet Returns all <planet> ancestors of the context node.

  • ancestor-or-self::planet Returns the <planet> ancestors of the context node. If the context node is a <planet> as well, also returns the context node.

  • descendant-or-self::planet Returns the <planet> element descendants of the context node. If the context node is a <planet> as well, also returns the context node.

  • self::planet Returns the context node if it is a <planet> element.

  • 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).

  • /descendant::planet Returns all the <planet> elements in the document.

  • /descendant::planet/child::name Returns all the <name> elements that have a <planet> parent.

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

  • /descendant::planet[position() = 3] Returns the third <planet> element in the document.

  • child::planets/child::planet[position() = 4 ]/child::name[position() = 3] Returns the third <name> element of the fourth <planet> element of the <planets> element.

  • child::planet[position() > 3] Returns last() location path> last()> all the <planet> children of the context node after the first three.

  • preceding-sibling::name[position() = 2] Returns the second previous <name> sibling element of the context node.

  • child::planet[attribute:: color = "RED"] Returns all <planet> children of the context node that have a color attribute with value of "RED".

  • child::planet[attribute::color = "RED"][position() = 3] Returns the third <planet> child of the context node that has a color attribute with value of "RED".

  • child::planet[position() = 3][attribute::color="RED"] Returns the third <planet> child of the context node, only if that child has a color attribute with value of "RED".

  • child::planet[child::name] Returns the <planet> children of the context node that have <name> children.

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, and we'll look at that next.

Using XPath Abbreviated Syntax

There are a number of abbreviations you can take advantage of in XPath syntax. Here are the rules:

  • self::node() can be abbreviated as .

  • parent::node() can be abbreviated as ..

  • child:: nodename can be abbreviated as nodename

  • attribute:: nodename can be abbreviated as @ nodename

  • /descendant-or-self::node()/ can be abbreviated as //

You can also abbreviate predicate expressions like [position() = 3] as [3] . Using the abbreviated syntax makes XPath expressions a lot easier to write. For example, attribute::units can be abbreviated as @units , you can refer to the context node itself as simply . , and you can refer to the current node and any descendants as // .

The // syntax in particular is useful and important. Take a look at ch03_01.xml , for example, the XML document where we're storing planetary data. In that XML document, we have three <planet> elements as children of the main <planets> element:

 
 <planets>     <planet language="English">         <name>Mercury</name>         <mass units="(Earth = 1)">.0553</mass>         <day units="days">58.65</day>         .         .         .     <planet language="English">         <name>Venus</name>         <mass units="(Earth = 1)">.815</mass>         <day units="days">116.75</day>         .         .         .     <planet language="English">         <name>Earth</name>         <mass units="(Earth = 1)">1</mass>         <day units="days">1</day>         .         .         . 

To select all three <planet> elements, you can use the absolute XPath expression /planets/planet , which starts at the XML document's root node, finds the <planets> element, and then matches the three <planet> child elements. That's fine if you know exactly where in the XML document the elements you want are and so can specify a direct path to them.

But you can also use //planet to select all three <planet> elements, because //planet will find the <planet> elements by checking the root node and all descendants for <planet> elements. That's the power of // when you want to search for nodes that may be anywhere in a document, use // .

How about some examples of location paths using abbreviated syntax? Here are a number of examples:

  • planet Returns the <planet> element children of the context node.

  • * Returns all element children of the context node.

  • text() Returns all text node children of the context node.

  • @units Returns the units attribute of the context node.

  • @* Returns all the attributes of the context node.

  • planet[3] Returns the third <planet> child of the context node.

  • planet[first()] Returns the first <planet> child of the context node.

  • */planet Returns all <planet> grandchildren of the context node.

  • /planets/planet[3]/name[2] Returns the second <name> element of the third <planet> element of the <planets> element.

  • //planet Returns all the <planet> descendants of the root node.

  • planets//planet Returns the <planet> element descendants of the <planets> element children of the context node.

  • //planet/name Returns all the <name> elements that have a <planet> parent.

  • . Returns the context node itself.

  • .//planet Returns the <planet> element descendants of the context node.

  • .. Returns the parent of the context node.

  • ../@units Returns the units attribute of the parent of the context node.

  • planet[name] Returns the <planet> children of the context node that have <name> children.

  • planet[name="Venus"] Returns the <planet> children of the context node that have <name> children with text equal to "Venus".



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

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