Using XML Queries and Transformations

Building a Path

Several of the XPath expressions we have seen up until now can be appended to each other to form a longer expression. This is done in a way similar to building a full directory path from several directory names: by separating them with forward slashes. The first expression in the path is evaluated in the original context; the result set from this expression forms the context for the next. Each of the nodes in the result set is used as context for the expression that follows, and all the results of each query are combined to one result set at the end. This would work as follows: this command selects the parent element of all name elements along the descendant axis of our context node:

descendant::name/parent::*

This selects all text nodes from paragraph elements that are children of chapter elements that are children of book elements that are children of our context node:

child::book/child::chapter/child::paragraph/child::text()

Absolute vs. Relative Paths

Just as with directory paths, we can make the XPath expression absolute by prefixing a slash. This sets the expression context to the document root. This is not the root element (compare with the documentElement attribute of the DOMDocument object in the DOM), but the parent of the root element (compare with the DOMDocument object itself). This example would select all attributes on the root element:

/child::*/attribute::*

However, the next example would select nothing, because the document root cannot carry attributes:

/attribute::*

An XPath that consists of only a slash (/) always refers to the document root.

Abbreviated Form / IE5 Compatible Form

The abbreviated notation of XPath is intended to keep the queries shorter. But the most important reason to learn the shorthand notation is that it used to be the full notation (according to the working draft) at the moment that the first release of IE5 hit the shops. In fact, it wasn't even called XPath back then, but was part of the XSL specification, which was later split up in three parts. (More on that later in this chapter.) That's why in IE5, only the shorthand syntax of XPath is implemented (in January 2000; Microsoft released a preview of the newer version of the library, which will support the full XPath specification). The main rulesĀ for the abbreviated syntax are:

Shorthand Rule

Example

The child axis is the default axis

TABLE equals child::TABLE

The attribute axis can be abbreviated to the prefix @

@name equals attribute::name

Table Continued on Following Page

Shorthand Rule

Example

The self axis can be abbreviated to .

. equals self::*

The parent axis can be abbreviated to ..

.. equals parent::*

The descendant axis can be abbreviated to //

//* equals /descendant::*

.//* equals descendant::*

So these XPath expressions are valid in the IE5 implementation. The first command returns all ID attributes from TABLE elements in the whole document:

//TABLE/@ID

While this returns all text nodes that are children of PARAGRAPH elements that are children of CHAPTER elements that are children of the context element:

CHAPTER/PARAGRAPH/text()

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe