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

Operators and Special Characters

XPath expressions are constructed using the operators and special characters shown in the following table.

/ Child operator; selects immediate children of the left-side collection. When this path operator appears at the start of the pattern, it indicates that children should be selected from the root node.
// Recursive descent; searches for the specified element at any depth. When this path operator appears at the start of the pattern, it indicates recursive descent from the root node.
. Indicates the current context.
* Wildcard; selects all elements regardless of the element name.
@ Attribute; prefix for an attribute name.
@* Attribute wildcard; selects all attributes regardless of name.
: Namespace separator; separates the namespace prefix from the element or attribute name.
( ) Groups operations to explicitly establish precedence.
[ ] Applies a filter pattern.
[ ] Subscript operator; used for indexing within a collection.
+ Performs addition.
- Performs subtraction.
div Performs floating-point division according to IEEE 754.
* Performs multiplication.
mod Returns the remainder from a truncating division.

This table does not include Boolean and set operators, which are listed in Boolean, Comparison, and Set Expressions or Set Operations.

Precedence order (from highest precedence to lowest) is defined as indicated in the following table.

Precedence Character Purpose
1 ( ) Grouping
2 [ ] Filters
3 / // Path operations

The group operator, (), is applicable only at the top-level path expression. For example, (//author/degree | //author/name) is a valid grouping operation, but //author/(degree | name) is not.

The filter pattern operators ([]) have a higher precedence than the path operators (/ and //). For example, the expression //comment()[3] selects all comments with an index equal to 3 relative to the comment's parent anywhere in the document. This differs from the expression (//comment())[3], which selects the third comment from the set of all comments relative to the parent. The first expression can return more than one comment, while the latter can return only one comment.

These operators and special characters are described in detail throughout this reference.

Path Operators

The collection of elements of a certain type can be determined using the path operators (/ and //). These operators take as their arguments a "left side" collection on which to perform the selection and a "right side" collection indicating which elements to select. The child operator (/) selects from immediate children of the left-side collection, while the descendant operator (//) selects from arbitrary descendants of the left-side collection. In effect, // can be considered a substitute for one or more levels of hierarchy.

Note that the path operators change the context as the query is performed. By stringing path operators together, users can traverse the document tree.

Examples

Expression Refers to
author/first-name All <first-name> elements within an <author> element of the current context node.
bookstore//title All <title> elements one or more levels deep in the <bookstore> element (arbitrary descendants). Note that this is different from the following pattern, bookstore/*/title.
bookstore/*/title All <title> elements that are grandchildren of <bookstore> elements.
bookstore//book/excerpt//emph All <emph> elements anywhere inside <excerpt> children of <book> elements, anywhere inside the <bookstore> element:
.//title All <title> elements one or more levels deep in the current context. Note that this situation is essentially the only one in which the period notation is required.

Wildcard Character

An element can be referenced without using its name by substituting the wildcard (*) collection. The * collection refers to all elements that are children of the current context, regardless of the tag name.

Examples

Expression Refers to
author/* All element children of <author> elements.
book/*/last-name All <last–name> elements that are grandchildren of <book> elements.
*/* All grandchildren elements of the current context.
my:book The <book> element from the my namespace.
my:* All elements from the my namespace.

Note that the pattern *:book is not supported.

Attributes

XPath denotes attribute names with the @ symbol. Attributes and child elements are treated impartially, and capabilities are equivalent between the two types wherever possible.

Note   Attributes cannot contain child elements, so syntax errors occur when path operators are applied to attributes. In addition, you cannot apply an index to attributes because, by definition, no order is defined for attributes.

Examples

Expression Refers to
@style The style attribute of the current element context.
price/@exchange The exchange attribute of <price> elements within the current context.
book/@style
The style attribute of all <book> elements.

Note that the following example is not valid, because an attribute cannot have any children.

price/@exchange/total

Finding Multiple Attributes

All attributes of an element can be returned using @*. This is potentially useful for applications that treat attributes as fields in a record.

Examples

Expression Refers to
@* All attributes of the current context node.
@my:* All attributes from the my namespace. This does not include unqualified attributes on elements from the my namespace.

Note that the pattern @*:title is not supported.

Try It!

The following is an interactive demonstration to illustrate the features of XPath expressions discussed above. When you select a context node and an XPath expression from the drop-down menu, you will see them displayed in colors in the XML source document that follows.
To proceed, click here.

See Also

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