XPath Collections

Collections returned by XPath queries preserve document order, hierarchy, and identity, to the extent that these are defined. That is, a collection of elements is returned in document order without repeated elements. Because by definition attributes are unordered, there is no implicit order to attributes returned for a specific element.

The collection of all elements with a certain tag name is expressed using the tag name itself. This can be qualified by showing that the elements are selected from the current context by using a period and forward slash (./), but the current context is used by default and does not have to be noted explicitly.


Expression Refers to


All <first-name> elements. Note that this expression is equivalent to the expression that follows.


All <first-name> elements.

XPath expressions make it easy to query a specific node within a set of nodes. Simply enclose the index ordinal within square brackets. The ordinal is 1-based (the first element is number 1).

The square bracket characters ([]) have higher precedence than the slash characters (/ and //). For more information see Operators and Special Characters



Expression Refers to


The first <author> element.


The third <author> element that has a <first-name> child element.

Note that indexes are relative to the set being filtered. Consider, for example, the following data.


The following table shows how to select specific <x> and <y> elements.


Expression Refers to


The first <y> inside each <x>.


The first <y> from the entire set of <y> elements within <x> elements.


The first <y> inside the first <x>.

The examples above are simple references to XPath collections that use implied defaults, such as the child:: axis. For this axis, the collection of child nodes is indexed in forward document order.

For other axes, such as ancestor::, use the axis name explicitly in your XPath expression. For this axis, the collection of ancestors is indexed in reverse document order. Consider this example from the previous table:


This expression is equivalent to this one:


Both expressions mean "for each <x> element, select the first child element named <y>."

The following example uses the same syntax.


This example translates to "for each <x> element, select the first ancestor element (in reverse-document order) named <y>". The syntax is the same, but the order is reversed.

The last() function returns True for the last element in a collection. Note that last is relative to the parent node.



Expression Refers to


The last <book> element.


The last <author> element inside each <book> element.


The last <author> element from the entire set of <author> elements inside <book> elements.

Parentheses can be used to group collection operators for clarity or where the normal precedence is inadequate to express an operation. Grouping operators can be used in any filter expressions (predicates), such as author[(degree or award)and publication]. They can also be used in the top-level step expression, such as (book|magazine) or (author/degree | book/award). They cannot be applied to lower-level step expressions. For example, author/(degree | award)is not valid.



Expression Refers to


All <author> elements that are child elements of any <book> element from the current context node.

author[(degree or award) and publication]

All <author> elements that contain at least one <degree> or <award> element and at least one <publication> element.

Build Date: