> DNU home > online course > DirXML Driver Development
April 2001
DeveloperNet University Course

Interpreting XPATH


An XPATH expression identifies input document nodes with relation to the context node, and returns references to them in what is called a nodeset.

 
 

The "location path" in an XPATH expression is composed of steps delimited by slashes, describing a path from the context node to a single node or many nodes in the document tree.

 

 

Many times an XPATH expression will consist of just one step. Each step is composed of two main parts delimited by a token. The first part of a step is called the axis and the second part is called a nodetest.

 

 

The axis defines a line of selectable nodes extending from the context node. Possible axes include child, parent, attribute, and others.

 
 

The nodetest further qualifies the selectable nodes by specifying a type or name.

 
 

In addition, we can compound expressions with logical operators, like the '|' union operator which combines the nodesets resulting from two expressions.

 
 

Consider the XPATH expression,

"child::node()|attribute::*"
applied to the document fragment below with <nds> selected as the context node.

 

<nds dtdversion="1.0" ndsversion="8.5">
<source>
<product asn1id="2 16 840 1 113719 1 x" version="1.0b3">DirXML</product>
<contact>Novell, Inc.</contact>
</source>
<input>
</input>
</nds>

 

Consider the fragment's node tree diagram below:

 

 

Let's apply the first part of the XPATH expression,

"child::node()"
to the node tree. You can see that <source> and <input> are node children of <nds>. This puts them on the child axis and makes them potentially selectable. To further constrain the selection, the node() nodetest is applied. The node() nodetest allows any node. So the returned nodeset for the subexpression would include both of them.

 
 

Now, let's apply the second part of the XPATH expression,

"attribute::*" 
to the node tree. You can also see that "dtdversion" and "ndsversion" are attributes of <nds>. This puts them on the attribute axis of the <nds> node and makes them potentially selectable. To further constrain the selection, the "*" nodetest is applied. With the "*" nodetest applied to an attribute axis, it allows the selection of any or all attributes on the axis. So, a nodeset from this subexpression would include both attributes.

 
 

The combined nodesets resulting from the XPATH expression,

"child::node()|attribute::*" 
would be the ordered list shown as numbered nodes in the diagram.

 
 

An XPATH expression of

"parent::*"
would obtain the node set shown below.

 

 

However, an XPATH expression of

"parent::text()"
would obtain an empty nodeset because the parent of <nds> is not a text node (text nodes can't be parents anyway) but an element node.

 
 

An XPATH expression of

"child::source"
would obtain the node set shown below because only the <source> node is on the child axis and has the name of "source" .

 

 

A step can be added to select the children of <source> in the XPATH expression. For example, the expression

"child::source/child::node()"
produces the nodeset shown below:

 
 

Each XPATH step can be further qualified with predicates. Predicates are sub-expressions enclosed by the square bracket characters '[ ]'.

 
 

For example, the XPATH expression in the second step of

"child::source/child::node()"
can be further defined to identify <product> by adding a predicate like this:

 

"child::source/child::node()[attribute::version]"

 

This is because only the <product> child of <source> has an attribute with the name of version. So, the expression with the predicate produces the node set shown below:

 

 

Abbreviation symbols are often used in place of XPATH terms. Shown below are a couple of items showing common substitutions for axes:

 
  1. '@' = attribute::
  2. = child::
 

Item 1: This example shows that the '@' character can be used in place of the

"attribute::" 
>axis specifier, (e.g.,
"child::source/child::node()[@version]").

 
 

Item 2: That's right your eyes aren't fooling you. It isn't a typo, there is really nothing to the left of the '='. Item 2 shows that whenever an axis is not specified, the axis is always,

"child::"
.

 
 

For example, the XPATH expression, "source/node()[@version]") would produce the same nodeset as

"child::source/child::node()[@version]"
shown below.

 

 

For the purposes of this course, those axes, nodetests, functions, instructions, and others defined in this section should be sufficient. However, there are many other axes, nodetests, functions, instructions, and others defined in XPATH. Refer to a good reference book (like XSLT Programmer's Reference by Michael Kay, ISBN 1-861003-12-9) for more information.

 
Previous Contents Next
download sample file