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:
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.
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.
<author>
elements within the current context:
./author
Note that this is equivalent to the following:
author
<bookstore>
element at the root of this document:
/bookstore
/*
<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.
<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.
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.
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