May 28, 2021 XPath
6. Pick the title of the first book
9.. Select the price node with a price price higher than 35
In this section, let's learn some basic XPath syntax through examples.
We'll use this XML document in the following example:
"books.xml":
View this "books" file in .xml browser.
All modern browsers support the use of XMLHttpRequest to load XML documents.
Code for most modern browsers:
Code for older Microsoft browsers (IE 5 and 6):
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:
Firefox, Chrome, Opera, and Safari use the evaluate() method to select nodes from XML documents:
Here's an example of picking all the title nodes:
The following example picks the title of the first book node below the bookstore element:
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.
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:
The following example picks all the text in the price node:
The following example picks all price nodes with prices above 35:
The following example picks all title nodes with prices above 35: