home HOME

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

References
XPath Functions

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

pixels

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

We will use the Microsoft XMLDOM object to load the XML document and the selectNodes() function to select nodes from the XML document:

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("books.xml")
xmlDoc.selectNodes(path expression)


Select all book Nodes

The following example selects all the book nodes under the bookstore element:

xmlDoc.selectNodes("/bookstore/book")

If you have IE 5 or higher you can try it yourself.


Select the First book Node

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

xmlDoc.selectNodes("/bookstore/book[0]")

If you have IE 5 or higher you can try it yourself

Note: 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 only the first book node under the bookstore element:

xmlDoc.setProperty "SelectionLanguage", "XPath"
xmlDoc.selectNodes("/bookstore/book[1]")

Try it yourself


Select the prices

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

xmlDoc.selectNodes("/bookstore/book/price/text()") 

If you have IE 5 or higher you can try it yourself.


Selecting price Nodes with Price>35

The following example selects all the price nodes with a price higher than 35:

xmlDoc.selectNodes("/bookstore/book[price>35]/price") 

If you have IE 5 or higher you can try it yourself.


Selecting title Nodes with Price>35

The following example selects all the title nodes with a price higher than 35:

xmlDoc.selectNodes("/bookstore/book[price>35]/title") 

If you have IE 5 or higher you can try it yourself.


Previous Next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. 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-2007 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999