6.2. Selecting Nodes

To do anything sophisticated in XSLT, we have to move around the document as nimbly as a monkey in the forest. At all times, we need to know exactly where we are and where we're going next. We also have to be able to select a group of nodes for processing with utmost precision. These navigation skills are provided by XPath,[1] a sophisticated language for marking locations and selecting sets of nodes within a document.

[1] The XPath language is a W3C recommendation. Version 1.0 was ratified in November 1999.

6.2.1. Location Paths

Location is an important concept in XML navigation. In XSLT we often have to describe the location of a node or group of nodes somewhere in a document. XPath calls this description a location path. A good example of this is the match attribute of <xsl:template>, which specifies a path to a group of nodes for the rule to process. Though the examples we've seen so far are simple, location paths can be quite sophisticated.

Location paths come in two flavors: absolute and relative. Absolute paths begin at a fixed reference point, namely the root node. In contrast, relative paths begin at a variable point that we call a context node.

A location path consists of a series of steps, each of which carries the path further from the starting point. A step itself has three parts: an axis that describes the direction to travel, a node test that specifies what kinds of nodes are applicable, and a set of optional predicates that use ...

Get Learning XML now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.