XPATH Location Steps

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 Steps
Axes
Node Tests
Predicates
Context Node
Summary

Introduction

This tutorial introduces the location step, a constituent part of the location path. It explains how axes, node tests, and predicates are used in XPath.

I have introduced the 13 axes, and the seven node tests used in XPath.

Location Steps

Location steps are constituent parts of location paths. They allow nodes to be selected in an XML document, based on expressions, for example using attributes, position, namespace prefix.

A location step contains an axis, and a node test, and an optional predicate. The axis and node test are separated by a double colon. The predicate consists of content in square brackets which follow the node-test.

axis::node-test[predicate expression]

Axes

XPath uses 13 axes. The default axis is the child one, and it can therefore be omitted in an abbreviated location path. The following two tables outline the use of axes in XPath.

Attribute and namespace are abbreviated A&N in this table. Root and element are abbreviated R&E in this table.

The values in the brackets show the abbreviated value.

			Abbreviated		Inapplicable	Omitted		Applicable 
			Location Path		Nodes		Nodes		Nodes
child			Yes(NULL)		A&N				R&E
descendant		No			A&N				R&E
descendant-or-self	Yes(/)			
parent			Yes(..)			R
ancestor			No			R
ancestor-or-self		No
following-sibling		No					A&N
preceding-sibling		No					A&N  
following		No					A&N  
preceding		No					A&N
attribute			Yes (@)
namespace		No
self			Yes(.)

Note this table informs about the returned nodes relative to the context node.

			Relative to
			Context Node
child			all children
descendant		all children, including children of children
descendant-or-self	descendants or itself
parent			containing element or root
ancestor			root and all elements
ancestor-or-self		all ancestors or self
following-sibling		following nodes with the same parent
preceding-sibling		preceding nodes with the same parent
following		all nodes after
preceding		all nodes before
attributes		all attributes
namespace		all namespaces in scope
self			itself

Node Tests

A node test specifies the node type and expanded-name of the nodes selected by the location step. Seven node tests are specified in XPath.

A node test selects nodes from a axis. In XPath and in connection to node tests, we classify axes as follows:

		principal node
Axis		type
-attribute	attribute
- namespace	namespace
- other 		element

Along an element axis, the following node tests apply

node test			Selected Nodes
name			matching element nodes by XML name
prefix:*			element nodes with matching namespace prefixes

Along an attribute axis, the following node tests apply

node test			Selected Nodes
name			matching attribute nodes by XML name

prefix:*			attribute nodes with matching namespace prefixes

Along all axes

Node Test		Selected Nodes
comment()		All comment
text()			All text
processing instruction	All processing instruction
processing-instruction	processing instruction nodes
('target')			with matching target
node()			All
*			All

Predicates

Predicates normally return a true or false value and then a node is kept or removed from a node set. Node sets are returned by location paths. Predicates can is XPath functions.These are discussed elsewhere.

axis::node-text[predicate function]

For example:

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

/child::employee[attribute::name='Trevor Oakley'])

This selected the employee node with the attribute 'Trevor Oakley'.

Context Node

A phrase used extensively in XPath is "Context Node". A context node is a particular node in an XML. document.

Summary

This tutorial introduced the location step, axes, node tests, and the predicate used in location steps.