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 Location Paths

Previous Next

A location path expression results in a node-set.


Location Path Expression

A location path can be absolute or relative.

An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more location steps, each separated by a slash:

An absolute location path:
/step/step/...
A relative location path:
step/step/...

The location steps are evaluated in order one at a time, from left to right. Each step is evaluated against the nodes in the current node-set. If the location path is absolute, the current node-set consists of the root node. If the location path is relative, the current node-set consists of the node where the expression is being used. Location steps consist of:

  • an axis (specifies the tree relationship between the nodes selected by the location step and the current node)
  • a node test (specifies the node type and expanded-name of the nodes selected by the location step)
  • zero or more predicates (use expressions to further refine the set of nodes selected by the location step)

The syntax for a location step is:

axisname::nodetest[predicate]

Example:

child::price[price=9.90]


Axes and Node Tests

An axis defines a node-set relative to the current node. A node test is used to identify a node within an axis. We can perform a node test by name or by type.

AxisName Description
ancestor Contains all ancestors (parent, grandparent, etc.) of the current node

Note: This axis will always include the root node, unless the current node is the root node

ancestor-or-self Contains the current node plus all its ancestors (parent, grandparent, etc.)
attribute Contains all attributes of the current node
child Contains all children of the current node
descendant Contains all descendants (children, grandchildren, etc.) of the current node

Note: This axis never contains attribute or namespace nodes

descendant-or-self Contains the current node plus all its descendants (children, grandchildren, etc.)
following Contains everything in the document after the closing tag of the current node
following-sibling Contains all siblings after the current node

Note: If the current node is an attribute node or namespace node, this axis will be empty

namespace Contains all namespace nodes of the current node
parent Contains the parent of the current node
preceding Contains everything in the document that is before the starting tag of the current node
preceding-sibling Contains all siblings before the current node

Note: If the current node is an attribute node or namespace node, this axis will be empty

self Contains the current node

Examples

Example Result
child::cd Selects all cd elements that are children of the current node (if the current node has no cd children, it will select an empty node-set)
attribute::src Selects the src attribute of the current node (if the current node has no src attribute, it will select an empty node-set)
child::* Selects all child elements of the current node
attribute::* Selects all attributes of the current node
child::text() Selects the text node children of the current node
child::node() Selects all the children of the current node
descendant::cd Selects all the cd element descendants of the current node
ancestor::cd Selects all cd ancestors of the current node
ancestor-or-self::cd Selects all cd ancestors of the current node and, if the current node is a cd element, the current node as well
child::*/child::price

Selects all price grandchildren of the current node

/ Selects the document root


Predicates

A predicate filters a node-set into a new node-set. A predicate is placed inside square brackets ( [ ] ).

Examples

Example Result
child::price[price=9.90] Selects all price elements that are children of the current node with a price element that equals 9.90
child::cd[position()=1] Selects the first cd child of the current node
child::cd[position()=last()] Selects the last cd child of the current node
child::cd[position()=last()-1] Selects the last but one cd child of the current node
child::cd[position()<6] Selects the first five cd children of the current node
/descendant::cd[position()=7] Selects the seventh cd element in the document
child::cd[attribute::type="classic"] Selects all cd children of the current node that have a type attribute with value classic


Location Path Abbreviated Syntax

Abbreviations can be used when describing a location path.

The most important abbreviation is that child:: can be omitted from a location step.

Abbr Meaning Example
none child:: cd is short for child::cd
@ attribute:: cd[@type="classic"] is short for
child::cd[attribute::type="classic"]
. self::node() .//cd is short for
self::node()/descendant-or-self::node()/child::cd
.. parent::node()

../cd is short for
parent::node()/child::cd

// /descendant-or-self::node()/ //cd is short for
/descendant-or-self::node()/child::cd

Examples

Example Result
cd Selects all the cd elements that are children of the current node
* Selects all child elements of the current node
text() Selects all text node children of the current node
@src Selects the src attribute of the current node
@* Selects all the attributes of the current node
cd[1] Selects the first cd child of the current node
cd[last()] Selects the last cd child of the current node
*/cd Selects all cd grandchildren of the current node
/book/chapter[3]/para[1] Selects the first para of the third chapter of the book
//cd Selects all the cd descendants of the document root and thus selects all cd elements in the same document as the current node
. Selects the current node
.//cd Selects the cd element descendants of the current node
.. Selects the parent of the current node
../@src Selects the src attribute of the parent of the current node
cd[@type="classic"] Selects all cd children of the current node that have a type attribute with value classic
cd[@type="classic"][5] Selects the fifth cd child of the current node that has a type attribute with value classic
cd[5][@type="classic"] Selects the fifth cd child of the current node if that child has a type attribute with value classic
cd[@type and @country] Selects all the cd children of the current node that have both a type attribute and an country attribute


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

IISCart
ASP Ecommerce & Shopping Cart


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