XPath Examples
Let's try to learn some basic XPath syntax by looking at some examples.
The XML Example Document
We will use the following XML document in the examples below.
"books.xml":
<?xml version="1.0" encoding="ISO-8859-1"?>
<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> |
View the "books.xml" file in your browser.
Selecting Nodes
Unfortunately, there are different ways of dealing with XML and XPath in Internet Explorer and other browsers.
In our examples we have included code that should work with most major browsers.
Select nodes for Internet Explorer
Using the Microsoft XMLDOM object to load the XML document and the selectNodes() method to select nodes from the XML document:
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("books.xml");
xmlDoc.selectNodes(xpath); |
Select nodes for Firefox and Opera
Using the implementation() method of the document object to load the XML document and the
evaluate() method to select nodes from the XML document:
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.async=false;
xmlDoc.load("books.xml");
xmlDoc.evaluate(xpath, xmlDoc, null, XPathResult.ANY_TYPE,null); |
Select all the titles
The following example selects all the title nodes:
Example
|
Select the title of the first book
The following example selects the title of the first book node under the bookstore element:
Example
|
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]!!
A Workaround!
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:
Example
xml.setProperty("SelectionLanguage","XPath");
xml.selectNodes("/bookstore/book[1]/title");
|
|
Select all the prices
The following example selects the text from all the price nodes:
Example
|
/bookstore/book/price/text()
|
|
Select price nodes with price>35
The following example selects all the price nodes with a price higher than 35:
Example
|
/bookstore/book[price>35]/price
|
|
Select title nodes with price>35
The following example selects all the title nodes with a price higher than 35:
Example
|
/bookstore/book[price>35]/title
|
|
Reliable, Affordable, Feature-Rich Web Hosting!
Take the uncertainty out of Web hosting and let
GoDaddy.com put service, performance and value back in. No matter which
hosting type or plan you choose, your site receives 24/7
maintenance and protection in our world-class data center. Plus,
you get the expert, friendly service you deserve, from the
world's largest hostname provider.
With three plans to choose from and
prices starting at just $4.99 per month, GoDaddy.com is sure to have a plan that's
right-sized and right-priced just for you!
All plans feature FREE 24x7 setup, FREE 24x7 monitoring, best-
of-breed routers, firewalls and servers, 24x7 onsite physical security
and access to our exclusive Go Daddy Hosting Connection, THE place
to install over 30 FREE applications. Virtual Dedicated and Dedicated
Server plans also available.
Visit GoDaddy.com today.
Virtual Dedicated, Dedicated Server and unlimited plans also available.
Save 20% on 12 months or more of shared web hosting - Enter code w3s20off at checkout
|