XPATH Location Paths

All tutorials are implemented in text (here), and also with exercises and multimedia in the Tutorial Player. The main home page has a search engines, and more facilities.

Introduction
Location Path
Unabbreviated
Abbreviated
Absolute
Root Path
Node Tests
Summary

Introduction

I have introduced the location path, and given some examples of how location paths select nodes from an XML document. Location paths are important in XML.

Location Path

Location paths are expression which select sets of nodes. These sets are called node-sets. A location path can be written in an unabbreviated format (with ::) or with an abbreviated format.

Location paths operate along axis, which are defined as forward or backward depending on the keyword. For example, if ancestor is used, the expression selects from above the context node in the document order. This is called a reverse axis, otherwise a forward axis is used.

Unabbreviated

Location paths specified with a double colon, are in unabbreviated format.

<employees>
<employee name="Trevor Oakley">
<project name="educiate"/>
</employee>
</employees>

/child::employees/child::employee[@name="Trevor Oakley"]

This selects the element node with the attribute Trevor Oakley.

This location path is said to be in unabbreviated format.

The unabbreviated paths are the only ones able to select using the following axes:

followin-sibling
preceding-sibling
following
preceding
namespace
descendant
ancestor-or-self

Therefore, they are more powerful. However, it is more common to find abbreviated location paths in use.

Abbreviated

Abbreviated location paths only select using child, parent, self, attribute, descendant-or-self axes. They do not use the double colon syntax that is used in unabbreviated location paths.

/employees/employee[@name="Trevor Oakley"]

Absolute

Absolute location paths specify the root path ("/") and then optionally several relative location paths. Location steps are separate from each other by the "/" character.

Root Path

The root path is shown in location paths as the "/" character. The root location path is therefore an absolute location path. The root has no parent.

Node Tests

In location paths uses several node tests filters; I have included a list here.

nodeName		selects all child elements with a specified node name
*			selects all child elements
@attributeName		selects an attribute
@*			selects all attributes
namespace:*		selects elements in a given namespace
node() 			matches an element node
text()			matches a text node
comment()		matches a comment node
processing-instruction() 	matches a processing instruction node
. (dot)			selects the current node
.. (double dot)		selects the parent node
/			selects the document node (root)
//			selects descendants and self

|			concatenation in a predicate
<logicians:manager xmlns:logicians="http://www.logicians.com">
<employees>
<!-- employee -->
  <employee name="Trevor Oakley">
     <old_projects>
         <project name="logicians" completion="05.05.00"/>
         <project name="chatty-people.com" completion="12.10.00"/>
    </old_projects>
    <current_project>
         <project name="educiate"/>
         <status>Good
     </current_project>
  </employee>
<logicians:manager >Trevor Oakley
</employees>
//employee				selects all child elements with a specified name of employee
//*					selects all child elements of root
//*[@name="Trevor Oakley"]		selects an attribute Trevor Oakley
//employee[@*]				selects all attributes on all employee elements
/employees/employee/logicians:*		selects the logicians:manager element
/logicians:manager/node() 			selects the logicians:manager node
/employee//text()[.='Good']		selects the text node with Good
//*comment()				selects the comment node with employee text
//current_project/processing-instruction() 	selects the node with the alert processing  instruction node
//current-project[.='Good'] 		selects the current_project element with Good
//current_project[..='Good']		selects nothing, since the conditions is not met

Summary

This tutorial introduced location paths.