w3schools
Search W3Schools :  
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About
ADVERTISEMENT

Best Web Hosting
FREE Web Hosting
Web Hosting UK

XPath Tutorial

XPath HOME
XPath Intro
XPath Nodes
XPath Syntax
XPath Axes
XPath Operators
XPath Examples
XPath Summary

XPath Reference

XPath Functions

 

XPath Examples

Previous Next

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

/bookstore/book/title

Try it yourself


Select the title of the first book

The following example selects the title of the first book node under the bookstore element:

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


Select all the prices

The following example selects the text from all the price nodes:

Example

/bookstore/book/price/text()

Try it yourself


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

Try it yourself


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

Try it yourself


Previous Next


Altova® XMLSpy® - The industry-leading XML editor!

Altova XMLSpy

Whether you're new to XML or already an advanced user, the user-friendly views and powerful entry helpers, wizards, and debuggers in XMLSpy are designed to meet your XML and Web development needs from start to finish.

  • XML editor
  • Graphical XML Schema / DTD editors
  • XSLT 1.0/2.0 editor, debugger, profiler
  • XQuery editor, debugger, profiler
  • XBRL validator & taxonomy editor
  • Support for Office Open XML (OOXML)
  • Graphical WSDL editor & SOAP debugger
  • Java, C#, C++ code generation
  • And much more!

Download a free trial today!

  Altova XMLSpy

 
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
WEB BUILDING
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Get a Freelancer
Download XML editor
FREE Flash Website
FREE Web Templates
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display
W3Schools.com HOME | TOP | PRINT | FORUM | ABOUT
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.