HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
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":
<?xml version="1.0" encoding="UTF-8"?>
<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>
Using an XMLHttpRequest object to load XML documents is supported in all modern browsers.
var xmlhttp = new XMLHttpRequest();
Unfortunately, there are different ways of dealing with XPath in different browsers.
Chrome, Firefox, Edge, Opera, and Safari use the evaluate() method to select nodes:
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null);
Internet Explorer uses the selectNodes() method to select node:
xmlDoc.selectNodes(xpath);
In our examples we have included code that should work with most major browsers.
The following example selects all the title nodes:
/bookstore/book/title
The following example selects the title of the first book node under the bookstore element:
/bookstore/book[1]/title
The following example selects the text from all the price nodes:
/bookstore/book/price[text()]
The following example selects all the price nodes with a price higher than 35:
/bookstore/book[price>35]/price
The following example selects all the title nodes with a price higher than 35:
/bookstore/book[price>35]/title