XPath Syntax

An XML Path Language (Xpath) expression uses a path notation, like those used in URLs, for addressing parts of an XML document. The expression is evaluated to yield an object of the node-set, Boolean, number, or string type. For example, the expression book/author will return a node-set of the <author> elements contained in the <book> elements, if such elements are declared in the source XML document. In addition, an XPath expression can have predicates (filter expressions) or function calls. For example, the expression book[@type="Fiction"] refers to the <book> elements whose type attribute is set to "Fiction".

The following table summarizes some of the analogous features between URLs and XPath expressions.

URLs XPath expressions

Hierarchy comprised of folders and files in a file system.

Hierarchy comprised of elements and other nodes in an XML document.

Files at each level have unique names. URLs always identify a single file.

Element names at each level might not be unique. XPath expressions identify a set of all the matching elements.

Evaluated relative to a particular folder, called the "current folder."

Evaluated relative to a particular node called the "context" for the expression.

This section covers the syntax of XPath expressions, including the following:

Topics in this section use the Sample XML File for XPath Syntax (inventory.xml).

XPath also support namespaces and data types. Namespace prefixes can be included in expressions so that the matching operations can check for specific namespace prefixes.

Community Additions