Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XPath instance


May 28, 2021 XPath


Table of contents


XPath Examples


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


XML instance documentation

We'll use this XML document in the following example:

"books.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="COOKING">
<title >Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="CHILDREN">
<title >Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>

<book category="WEB">
<title >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 >Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>

</bookstore>

View this "books" file in .xml browser.


Load the XML document

All modern browsers support the use of XMLHttpRequest to load XML documents.

Code for most modern browsers:

var xmlhttp=new XMLHttpRequest()

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

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


Pick the node

Unfortunately, Internet Explorer does not handle XPath the same way as others.

In our case, it contains code for most major browsers.

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

xmlDoc.selectNodes( xpath );

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

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


Pick all title

Here's an example of picking all the title nodes:

/bookstore/book/title

Try it out . . .


Pick the title of the first book

The following example picks the title of the first book node below the bookstore element:

/bookstore/book[1]/title

Try it out . . .

Here's a problem. The example above outputs different results in IE and other browsers.

IE5 and later treat the first node, and according to the W3C standard, it should be the first node.

A solution!

To resolve the issues in IE5 plus, you can set up a language selection (SelectionLanguage) for XPath.

The following example picks the title of the first book node below the bookstore element:

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

Try it out . . .


Select all prices

The following example picks all the text in the price node:

/bookstore/book/price/text()

Try it out . . .


Select the price node with a price price higher than 35

The following example picks all price nodes with prices above 35:

/bookstore/book[price>35]/price

Try it out . . .


Pick a title node that has a price higher than 35

The following example picks all title nodes with prices above 35:

/bookstore/book[price>35]/title

Try it out . . .