XPath can be used as a general-purpose query notation for addressing and filtering the elements and text of XML documents. XPath is supported in MSXML within XSLT, and through the Document Object Model (DOM) extensions selectNodes and selectSingleNode.
The Microsoft XML Parser (MSXML) supports the World Wide Web Consortium (W3C) recommendation for XML Path Language (XPath) Version 1.0 (W3C Recommendation 16 November 1999). For details of the parser's conformance, see the XSLT and XPath Conformance Notes.
This and the following topics introduce the syntax of XPath:
The XPath notation is declarative, rather than procedural. Each pattern describes the types of nodes to match using a notation that indicates the hierarchical relationship between the nodes. For example, the pattern "book/author" means find "author" elements contained in "book" elements.
The notation enables you to query the XML document tree, which represents the contents of the XML document as a hierarchy of nodes. The syntax mimics the URI (universal resource identifier) directory navigation syntax, but instead of specifying navigation through a logical hierarchy of directories, the navigation is through a hierarchy of nodes in the XML tree.
For example, the following URI means find the background.jpg file within the images directory:
images/background.jpg
Similarly, in an XPath pattern, the following means find the collection of book elements within bookstore elements:
bookstore/book
All XPath queries occur within a particular context, that is, at the level of a particular node within the tree representation.
A "context" is the single node against which the pattern matching operates. To understand 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. The result of a query dependson the context against which it is executed.
XPath queries can match specific patterns at one particular context, retrieve the results, and perform additional operations relative to the context of the returned nodes. This notation gives XPath queries extraordinary flexibility in searching throughout the document tree.
When using patterns with the Microsoft XML DOM programming interfaces, the context is the Node object whose selectNodes or selectSingleNode method is called.
A pattern prefixed with a period and forward slash (./) explicitly uses the current context as the context for the query.
A pattern prefixed with a forward slash (/) uses the root of the document tree as the context for the query.
A pattern can use the // operator to indicate 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. The .// prefix indicates that the context starts at the level in the hierarchy indicated by the current context.
Note that element names can include the period character (.), and these can be used in patterns just as any other name.
Find all author elements within the current context:
./author
Note that this is equivalent to:
author
Find all first.name elements:
first.name
Find the bookstore element at the root of this document:
/bookstore
Find the root element of this document:
/*
Find all author elements anywhere within the current document:
//author
Find all bookstores where the value of the specialty attribute is equal to "textbooks":
/bookstore[@specialty = "textbooks"]
Find all books where the value of the style attribute on the book is equal to the value of the specialty attribute of the bookstore element at the root of the document:
book[@style = /bookstore/@specialty]
In addition to this notation for representing hierarchical relationships among nodes, the implementation also supports Boolean, Comparison, and Set Expressions, Set Operations, Filters and Filter Patterns, Collections, and XPath Functions.
XPath also support namespaces and data types. Namespace prefixes can be included in patterns so that the matching operations can check for specific namespace prefixes.
Samples throughout this documentation are summarized in XPath Examples and refer to the data shown in Sample Data.