XPath Examples

In this section, let's learn some basic XPath syntax through examples.

XML example document

We will use this XML document in the following example:

"books.xml" :

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>

View this "books.xml" file in your browser.

Load XML document

All modern browsers support the method of loading XML documents using XMLHttpRequest.

Code for most modern browsers:

var xmlhttp=new XMLHttpRequest()

Code for old Microsoft browsers (IE 5 and 6):

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")

Select nodes

Unfortunately, Internet Explorer and other processors handle XPath differently.

The code in our example is applicable to most mainstream browsers.

Internet Explorer uses the selectNodes() method to select nodes from an XML document:

xmlDoc.selectNodes(xpath);

Firefox, Chrome, Opera, and Safari use the evaluate() method to select nodes from an XML document:

xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE, null);

Select all title

The following examples select all title nodes:

/bookstore/book/title

Try It Yourself

Select the title of the first book

The following example selects the title of the first book node under the bookstore element:

/bookstore/book[1]/title

Try It Yourself

Here is a problem. The above example produces different results in IE and other browsers.

IE5 and higher versions treat [0] as the first node, while according to W3C standards, it should be [1].

To solve the [0] and [1] issues in IE5+, you can set the language selection (SelectionLanguage) for XPath.

The following example selects the title of the first book node under the bookstore element:

xml.setProperty("SelectionLanguage","XPath");
xml.selectNodes("/bookstore/book[1]/title");

Try It Yourself

Select all prices

The following example selects all text within the price nodes:

/bookstore/book/price/text()

Try It Yourself

Select price nodes with a price greater than 35

The following example selects all price nodes with a price greater than 35:

/bookstore/book[price>35]/price

Try It Yourself

Select title nodes with a price greater than 35

The following example selects all title nodes with a price greater than 35:

/bookstore/book[price>35]/title

Try It Yourself