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.
Current context
Document root
Root element
Recursive descent
Specific element
Examples
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.
Current context
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:./author
Note that this is equivalent to the following:
author
Document root
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:/bookstore
Root element
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:/*
Recursive descent
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://author
The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.
Specific elements
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:images/background.jpg
The following expression refers to the collection of
<book>
elements within the<bookstore>
elements in the current context node:bookstore/book
The following expression refers to all
<first.name>
elements in the current context node:first.name
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.
Context in XSLT
When using XPath directly from the XSLT, you define the context by the current node.
Other Resources
For more information, see the XML Path Language (XPath) Version 1.0 (W3C Recommendation 16 November 1999) at www.w3.org/TR/xpath.