Escolar Documentos
Profissional Documentos
Cultura Documentos
http://www.w3schools.com/xpath/xpath_examples.asp
TRANSLATE
HOME
HTML
CSS
XML
JAVASCRIPT
ASP
PHP
SQL
MORE...
XPath Tutorial
XPath HOME XPath Intro XPath Nodes XPath Syntax XPath Axes XPath Operators XPath Examples XPath Summary
XPath Examples
Previous Next Chapter
Let's try to learn some basic XPath syntax by looking at some examples.
WEB HOSTING
WEB BUILDING
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 Share with
XPath Reference
XPath Functions
<?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 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:
xmlDoc.selectNodes(xpath);
1 sur 3
10/07/2012 08:24
XPath Examples
http://www.w3schools.com/xpath/xpath_examples.asp
Firefox, Chrome, Opera and Safari use the evaluate() method to select nodes from the XML document:
Example
/bookstore/book/title
Try it yourself
Example
/bookstore/book[1]/title
Try it yourself
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");
Try it yourself
Example
/bookstore/book/price/text()
Try it yourself
Example
/bookstore/book[price>35]/price
Try it yourself
Example
/bookstore/book[price>35]/title
Try it yourself
Previous
Next Chapter
REPORT ERROR | HOME | TOP | PRINT | FORUM | ABOUT
2 sur 3
10/07/2012 08:24
XPath Examples
http://www.w3schools.com/xpath/xpath_examples.asp
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2012 by Refsnes Data. All Rights Reserved.
3 sur 3
10/07/2012 08:24