XPath Syntax

To select nodes or node-sets in an XML document, the path expressions are used by XPath, i.e., by following a path or steps, a node is selected.

Example:






  ABC
  100



  XML Basics
  300



Selecting Nodes:

To select nodes or node-sets in an XML document, the path expressions are used by XPath, i.e., by following a path or steps, a node is selected. The most useful path expressions are:

Expression Uses
nodename To select all the nodes with the name “nodename”.
/ To select from the root node.
// To select nodes in the document from the current node that match the selection no matter where they are.
. To select the current node.
.. To select the parent of the current node.
@ To select the attributes.

Some path expressions and the result of these expressions:

Path Expression Uses
bookstore To select all the nodes with the name “bookstore”.
/bookstore To select the root element bookstore.
bookstore/book To select all the book elements that are children of the bookstore.
//book To select all the book elements no matter where they are in the document.
bookstore//book To select all the book elements that are descendants of the bookstore element, no matter where they are under the bookstore element.
//@lang To select all the attributes that are named lang.

The path starting with a slash ( / ) always represents an absolute path to an element.

Predicates:

To find a specific node or a node with a specific value, the predicates are used. They are always embedded in square brackets.

Some path expressions with predicates and the result of the expressions:

Path Expression Uses
/bookstore/book[1] To select the first book element which is the child of the bookstore element.
/bookstore/book[last()] To select the last book element which is the child of the bookstore element.
/bookstore/book[last()-1] To select the last but one book element which is the child of the bookstore element.
/bookstore/book[position()<3] To select the first two book elements that are children of the bookstore element.
//title[@lang] To select all the title elements with an attribute named lang.
//title[@lang=’en’] To select all the title elements with the “lang” attribute with a value of “en”.
/bookstore/book[price>100.00] To select all the book elements of the bookstore element that have a price element with a value greater than 100.00.
/bookstore/book[price>100.00]/title To select all the title elements of the book elements of the bookstore element that have a price element with a value greater than 100.00.

The first node In IE 5,6,7,8,9 is [0], but it is [1] according to W3C. Thus we need to set the SelectionLanguage to XPath. For example, in JavaScript: xml.setProperty(“SelectionLanguage”,”XPath”);

Selecting Unknown Nodes:

To select unknown XML nodes, the XPath wildcards are used.

Wildcard Uses
* To match an element node.
@* To match an attribute node.
node() To match a node of any kind.

Some path expressions and the result of the expressions:

Path Expression Uses
/bookstore/* To select all the child element nodes of the bookstore element.
//* To select all the elements in a document.
//title[@*] To select all the title elements which have at least one attribute of any kind.

Selecting Several Paths:

To select several paths, we can use the | operator in an XPath expression.

Some path expressions and the result of the expressions:

Path Expression Uses
//book/title | //book/price To select all the title AND price elements of all the book elements.
//title | //price To select all the title AND price elements in the document.
/bookstore/book/title | //price To select all the title elements of the book element of the bookstore element AND all the price elements in the document.