Creating Path ExpressionsAs in XPath 1.0, you can use a path expression to locate nodes. In fact, XPath 2.0 carries over just about all the same syntax for path expressions from XPath 1.0, although, of course, node-sets are now called sequences and there are other changes, such as deprecating (making obsolete) the namespace axis. As in XPath 1.0, path expressions are made up of location steps. In turn , location steps are made up of an axis, a node test, and zero or more predicates: axis :: node-test [ predicate ]* If you start a location path with / or // , the location path is an absolute location path because you're a specifying the path from the root node of the XML document. Otherwise, the location path is relative, starting with the context node. XPath 2.0 AxesEach XPath 2.0 path expression must specify an axis (or use the default child axis), as in this location path: /library/book/title[2]/text . The XPath 2.0 axes are the same as the XPath 1.0 axes. Note in particular that the namespace axis is now deprecated in XPath 2.0. That means that it's in a kind of limbo, preparatory to being phased out. Whether or not the software package you're using supports the namespace axis is now up to the package itselfit no longer has to. XPath 2.0 Node TestsWhen you use an axis in a location step, you're telling XPath 2.0 where to look and identifying a set of nodes. As in XPath 1.0, a node test tells XPath 2.0 which of the nodes in that set you're interested in. There are two ways of creating node tests in XPath 2.0. You can use node names as node tests, along with a wildcard character, * , as in XPath 1.0. Or you can use "kind" tests like comment() , text() , and so on, as also in XPath 1.0. Here's an overview of the kinds of node tests you can use in XPath 2.0note that element() , attribute() , and document-node() are new in XPath 2.0:
In XPath 2.0, you can pass parameters to the kind node tests element() and attribute() . Here are some examples (for more on type annotations, see the discussion on them in Chapter 7):
XPath 2.0 PredicatesAs in XPath 1.0, the next part of a location step, which follows the node text, is the predicate. A location step doesn't need a predicate, but using predicates, you can filter the nodes you want to locate even more. Predicates are where you use the many XPath 2.0 functions when you want to use them in path expressions. This works as it does in XPath 1.0; for example, in the location step child::planet[position() = 2] , the predicate 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. As in XPath 1.0, this location step can also be abbreviated as planet[2] . XPath 2.0 Abbreviated SyntaxThe rules for abbreviated syntax in XPath 2.0 are the same as in XPath 1.0 (technically speaking, . is not actually an abbreviationit's the context node item, as discussed earlier.):
As you know from XPath 1.0, you can also abbreviate position expressions like [position() = 6] as [6] . That's all there is to abbreviated syntax. The next type of XPath 2.0 expression we'll take a look at is sequence expressions. |