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.
Examples
Expression | Refers to |
---|---|
|
All |
|
All |
Indexing into a Collection
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
Examples
Expression | Refers to |
---|---|
|
The first |
|
The third |
Note that indexes are relative to the set being filtered. Consider, for example, the following data.
<x>
<y/>
<y/>
</x>
<x>
<y/>
<y/>
</x>
The following table shows how to select specific <x>
and <y>
elements.
Expression | Refers to |
---|---|
|
The first |
|
The first |
|
The first |
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:
x/y[1]
This expression is equivalent to this one:
x/child::y[1]
Both expressions mean "for each <x>
element, select the first child element named <y>
."
The following example uses the same syntax.
x/ancestor::y[1]
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.
Finding the Last Element in a Collection
The last() function returns True for the last element in a collection. Note that last
is relative to the parent node.
Examples
Expression | Refers to |
---|---|
|
The last |
|
The last |
|
The last |
Grouping
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.
Examples
Expression | Refers to |
---|---|
|
All |
|
All |