Let's try to learn some basic XPath syntax by looking at some examples.
We will use the following XML document in the examples below.
"books.xml":
View the "books.xml" file in your browser.
Using XMLHttpRequest to load XML documents is supported in all modern browsers.
Code for most modern browsers:
Code for old Microsoft browsers (IE 5 and 6):
Unfortunately, there are different ways of dealing with XPath in Internet Explorer and other browsers.
In our examples we have included code that should work with most major browsers.
Internet Explorer uses the selectNodes() method to select nodes from the XML document:
Firefox, Chrome, Opera and Safari use the evaluate() method to select nodes from the XML document:
The following example selects all the title nodes:
The following example selects the title of the first book node under the bookstore element:
There is a problem with this. The example above shows different results in IE and other browsers.
IE5 and later has implemented that [0] should be the first node, but according to the W3C standard it should have been [1]!!
To solve the [0] and [1] problem in IE5+, you can set the SelectionLanguage to XPath.
The following example selects the title of the first book node under the bookstore element:
The following example selects the text from all the price nodes:
The following example selects all the price nodes with a price higher than 35:
The following example selects all the title nodes with a price higher than 35:
Your message has been sent to W3Schools.