Collections

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XPath Reference

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
./first-name All <first-name> elements. Note that this expression is equivalent to the expression that follows.
first-name All <first-name> elements.

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
author[1] The first <author> element.
author[first-name][3] 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.

<x>
  <y/>
  <y/>
</x>
<x>
  <y/>
  <y/>
</x>

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

Expression Refers to
x/y[1] The first <y> inside each <x>.
(x/y)[1] The first <y> from the entire set of <y> elements within <x> elements.
x[1]/y[1] 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:

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
book[last()] The last <book> element.
book/author[last()] The last <author> element inside each <book> element.
(book/author)[last()] The last <author> element from the entire set of <author> elements inside <book> elements.

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
(book/author) 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.

See Also

Sample XML File for XPath Syntax (inventory.xml) | XPath Examples