Creating Path Expressions

Creating Path Expressions

As 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 Axes

Each 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 Tests

When 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:

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

  • The * wildcard character matches any node of the principal node kindelements, attributes, or namespaces. For example, child::* will select all element children of the context node, and attribute::* will select all attributes of the context node. You can also use * with the namespace axis.

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

  • The element() node test selects elements.

  • The attribute() node test selects attributes.

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):

  • element(planet) matches any element node whose name is planet and whose type annotation conforms to the schema declaration for a <planet> element.

  • element(planet, *) matches any element node whose name is planet without any restriction on type annotation.

  • element(planet, giant) matches any element node whose name is planet, and whose type annotation is giant or is derived from giant .

  • element(*, giant) matches any element node whose type annotation is giant , regardless of its name.

  • element(solarsystem/planet) matches any element node whose name and type annotation conform to the schema declaration of a <planet> element in a <solarsystem> element.

  • attribute() matches any attribute node.

  • attribute(language, *) matches any attribute whose name is language , regardless of its type annotation.

  • attribute(*, xs:decimal) matches any attribute whose type annotation is xs:decimal or derived from xs:decimal .

  • document-node() matches any document node.

  • document-node(element(planet)) matches any document node whose content consists of a single element node that satisfies the element(planet) node test.

XPath 2.0 Predicates

As 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 Syntax

The 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.):

  • 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 @node name

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

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.

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: