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 Syntax

Previous Next

XPath uses path expressions to locate nodes within XML documents.


XML Example Document

We will use this simple XML document to describe the XPath syntax:

<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
  <cd country="USA">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <price>10.90</price>
  </cd>
  <cd country="UK">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <price>9.90</price>
  </cd>
  <cd country="USA">
    <title>Greatest Hits</title> 
    <artist>Dolly Parton</artist> 
    <price>9.90</price> 
  </cd>
</catalog>


Locating Nodes

XML documents can be represented as a tree view of nodes (very similar to the tree view of folders you can see on your computer).

XPath uses a pattern expression to identify nodes in an XML document. An XPath pattern is a slash-separated list of child element names that describe a path through the XML document. The pattern "selects" elements that match the path.

The following XPath expression selects all the price elements of all the cd elements of the catalog element:

/catalog/cd/price

Note: If the path starts with a slash ( / ) it represents an absolute path to an element!

Note: If the path starts with two slashes ( // ) then all elements in the document that fulfill the criteria will be selected (even if they are at different levels in the XML tree)!

The following XPath expression selects all the cd elements in the document:

//cd



Selecting Unknown Elements

Wildcards ( * ) can be used to select unknown XML elements.

The following XPath expression selects all the child elements of all the cd elements of the catalog element:

/catalog/cd/*

The following XPath expression selects all the price elements that are grandchild elements of the catalog element:

/catalog/*/price

The following XPath expression selects all price elements which have 2 ancestors:

/*/*/price

The following XPath expression selects all elements in the document:

//*



Selecting Branches

By using square brackets in an XPath expression you can specify an element further.

The following XPath expression selects the first cd child element of the catalog element:

/catalog/cd[1]

The following XPath expression selects the last cd child element of the catalog element (Note: There is no function named first()):

/catalog/cd[last()]

The following XPath expression selects all the cd elements of the catalog element that have a price element:

/catalog/cd[price]

The following XPath expression selects all the cd elements of the catalog element that have a price element with a value of 10.90:

/catalog/cd[price=10.90]

The following XPath expression selects all the price elements of all the cd elements of the catalog element that have a price element with a value of 10.90:

/catalog/cd[price=10.90]/price



Selecting Several Paths

By using the | operator in an XPath expression you can select several paths.

The following XPath expression selects all the title and artist elements of the cd element of the catalog element:

/catalog/cd/title | /catalog/cd/artist

The following XPath expression selects all the title and artist elements in the document:

//title | //artist

The following XPath expression selects all the title, artist and price elements in the document:

//title | //artist | //price

The following XPath expression selects all the title elements of the cd element of the catalog element, and all the artist elements in the document:

/catalog/cd/title | //artist



Selecting Attributes

In XPath all attributes are specified by the @ prefix.

This XPath expression selects all attributes named country:

//@country

This XPath expression selects all cd elements which have an attribute named country:

//cd[@country]

This XPath expression selects all cd elements which have any attribute:

//cd[@*]

This XPath expression selects all cd elements which have an attribute named country with a value of 'UK':

//cd[@country='UK']



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

IISProtect
Password Protect
Your Web Pages



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