Share via


XPath Syntax

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

XML Path Language (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 the Microsoft® XML Parser (MSXML) within XSL Transformations (XSLT), and through the Document Object Model (DOM) extensions selectNodes and selectSingleNode.

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 Uniform Resource Identifier (URI) directory navigation syntax. However, 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

In a similar way, in an XPath pattern, the following means find the collection of book elements within bookstore elements.

bookstore/book

Context

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 depends on 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 Document Object Model (DOM) programming interfaces, the context is the Node object whose selectNodes method 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 (.), and these can be used in patterns just as any other name.

Examples

Find all author elements within the current context.

./author

Note that this is equivalent to the following.

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 name spaces and data types. Name space prefixes can be included in patterns so that the matching operations can check for specific name space prefixes.

Samples throughout this documentation are summarized in XPath Examples and refer to the data shown in Sample Data.

See Also

Reference

XPath Functions
IXMLDOMNode

Concepts

Boolean, Comparison, and Set Expressions
Set Operations
Filters and Filter Patterns
Collections
XPath Examples
Sample Data
selectNodes Method
selectSingleNode Method