HOME

XPath Tutorial
XPath HOME
XPath Intro
XPath Syntax
XPath Locations
XPath Expressions
XPath Functions
XPath Examples
Download XMLSpy here
Please visit our sponsors !

XPath Examples

Previous Next

We will use the CD catalog from our XML tutorial to demonstrate some XPath examples.


The CD catalog

If you have studied our XML tutorial, you will remember this XML document:

(A fraction of the CD catalog)

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
.
.
.
.
</catalog>   

If you have IE 5 or higher you can look at the cdcatalog.xml.


Selecting Nodes

We will demonstrate how to select nodes from the XML document by using the selectNodes function in Internet Explorer. This function takes a location path expression as an argument:

xmlobject.selectNodes(XPath expression)


Selecting cd Nodes

The following example selects all the cd nodes from the CD catalog:

xmlDoc.selectNodes("/catalog/cd") 

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


Selecting the First cd Node

The following example selects only the first cd node from the CD catalog:

xmlDoc.selectNodes("/catalog/cd[0]")

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

Note: IE 5 has implemented that [0] should be the first node, but according to the W3C standard it should have been [1].


Selecting price Nodes

The following example selects all the price nodes from the CD catalog:

xmlDoc.selectNodes("/catalog/cd/price") 

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


Selecting price Text Nodes

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

xmlDoc.selectNodes("/catalog/cd/price/text()") 

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


Selecting cd Nodes with Price>10.80

The following example selects all the cd nodes with a price>10.80:

xmlDoc.selectNodes("/catalog/cd[price>10.80]") 

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


Selecting price Nodes with Price>10.80

The following example selects all the price nodes with a price>10.80:

xmlDoc.selectNodes("/catalog/cd[price>10.80]/price") 

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


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

Domain Name Registration
Domain Name
Registration & More!



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search