Please visit our sponsors !
XPath Syntax
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:
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:
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:
The following XPath expression selects all the price elements that are
grandchild elements of the catalog element:
The following XPath expression selects all price elements which have 2
ancestors:
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:
The following XPath expression selects the last cd child element of the
catalog
element (Note: There is no function named first()):
The following XPath expression selects all the cd elements of the catalog
element that have a price
element:
The following XPath expression selects all the cd elements of the catalog
element that have a price
element with a value of 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:
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:
This XPath expression selects all cd elements which have an attribute named
country:
This XPath expression selects all cd elements which have any attribute:
This XPath expression selects all cd elements which have an attribute named
country with a value of 'UK':
Jump to: Top of Page
or HOME or
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
|