Context for XPath Expressions
The evaluation of an XPath expression depends on the context against which the expression operates. The context consists of the node against which the expression is evaluated and its associated environment, which includes the following:
The position of the context node in the document order, relative to its siblings.
The size of the context—that is, the number of siblings of the context node plus one.
Variable bindings with which references to a variable can be resolved.
A function library.
The namespace declarations in scope for the expression.
To better appreciate the concept of context, consider a tree containing nodes. Asking for all nodes named X from the root of the tree returns one set of results, while asking for those nodes from a branch in the tree returns a different set of results. Thus, the result of an expression depends upon the context against which it executes.
XPath expressions can match specific patterns at one particular context, return the results, and perform additional operations relative to the context of the returned nodes. This gives XPath expressions extraordinary flexibility in searching throughout the document tree.
Basic XPath Expressions
The following are basic types of XPath expressions. Each type is described below.
The following examples show some basic XPath expressions. More complex expressions are possible by combining these simple expressions together and by using the various XPath operators and special characters.
An expression prefixed with a period and forward slash (./) explicitly uses the current context as the context. For example, the following expression refers to all <author> elements within the current context:
Note that this is equivalent to the following:
An expression prefixed with a forward slash (/) uses the root of the document tree as the context. For example, the following expression refers to the <bookstore> element at the root of this document:
An expression that uses a forward slash followed by an asterisk (/*) uses the root element as the context. For example, the following expression finds the root element of the document:
An expression that uses the double forward slash (//) indicates a search that can include zero or more levels of hierarchy. When this operator appears at the beginning of the pattern, the context is relative to the root of the document. For example, the following expression refers to all <author> elements anywhere within the current document:
The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.
An expression that starts with an element name refers to a query of the specific element, starting from the current context node. For example, the following expression refers to the <background.jpg> element within the <images> element in the current context node:
The following expression refers to the collection of <book> elements within the <bookstore> elements in the current context node:
The following expression refers to all <first.name> elements in the current context node:
Note Element names can include the period character (.). These names can be used just like any other name.
Context in the DOM
When using XPath expressions with the Microsoft XML DOM, the context is the Node object whose selectNodes method or selectSingleNode method is called.
When using XPath directly from the DOM, you define the context from a particular node. For more information, see How the DOM Defines the Context for XPath Expressions.
Context in XSLT
When using XPath directly from the XSLT, you define the context by the current node. For more information about how a context is defined, see How XSLT Defines the Context for XPath Expressions.
XML Path Language (XPath) Version 1.0
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.
.. The parent of the current context node.
* 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() 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()), 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.