Tuesday, September 22, 2009

This topic reviews the syntax examples that appear throughout the XPath Reference. All are based on the Sample XML File for XPath Syntax (inventory.xml). For an example of using an XPath expression in a test file, see "Example of Unions ( | )", at the bottom of this topic.

Expression

Refers to

Xml

/IMG>Copy Code

./author

All <author> elements within the current context. Note that this is equivalent to the expression in the next row.

Xml

/IMG>Copy Code

author

All <author> elements within the current context.

Xml

/IMG>Copy Code

first.name

All <first.name> elements within the current context.

Xml

/IMG>Copy Code

/bookstore

The document element (<bookstore>) of this document.

Xml

/IMG>Copy Code

//author

All <author> elements in the document.

Xml

/IMG>Copy Code

book[/bookstore/@specialty = @style]

All <book> elements whose style attribute value is equal to the specialty attribute value of the <bookstore> element at the root of the document.

Xml

/IMG>Copy Code

author/first-name

All <first-name> elements that are children of an <author> element.

Xml

/IMG>Copy Code

bookstore//title

All <title> elements one or more levels deep in the <bookstore> element (arbitrary descendants). Note that this is different from the expression in the next row.