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:
The perfect solution for professionals who need to balance work, family, and career building.
More than 10 000 certificates already issued!
The HTML Certificate documents your knowledge of HTML.
The CSS Certificate documents your knowledge of advanced CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The jQuery Certificate documents your knowledge of jQuery.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
| WEB HOSTING |
|---|
| Best Web Hosting |
| PHP MySQL Hosting |
| Best Hosting Coupons |
| UK Reseller Hosting |
| Cloud Hosting |
| Top Web Hosting |
| $7.95/mo SEO Hosting |
| Premium Website Design |
| WEB BUILDING |
|---|
| Download XML Editor |
| FREE Website BUILDER |
| Free Website Templates Free CSS Templates |
| Make Your Own Website |
| W3SCHOOLS EXAMS |
|---|
|
Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP |
| W3SCHOOLS BOOKS |
|---|
|
New Books: HTML, CSS JavaScript, and Ajax |
| STATISTICS |
|---|
|
Browser Statistics Browser OS Browser Display |
| SHARE THIS PAGE |
|---|