Chapter 2. XPath Basics

Chapter 1 provided sketchy information about using XPath. For the remainder of the book, you’ll get details aplenty. In particular, this chapter covers the most fundamental building blocks of XPath. These are the “things” XPath syntax (covered in succeeding chapters) enables you to manipulate. Chief among these “things” are XPath expressions, the nodes and node-sets returned by those expressions, the context in which an expression is evaluated, and the so-called string-values returned by each type of node.

The Node Tree: An Introduction

You’ll learn much more about nodes in this chapter and the rest of the book. But before proceeding into even the most elementary details about using XPath, it’s essential that you understand what, exactly, an XPath processor deals with.

Consider this fairly simple document:

<?xml-stylesheet type="text/xsl" href="battleinfo.xsl"?>
<battleinfo conflict="WW2">
   <name>Guadalcanal</name>
   <!-- Note: Add dates, units, key personnel -->
   <geog general="Pacific Theater">
      <islands>
         <name>Guadalcanal</name>
         <name>Savo Island</name>
         <name>Florida Islands</name>
      </islands>
   </geog>
</battleinfo>

As the knowledgeable human eye — or an XML parser — scans this document from start to finish, it encounters signals that what follows is an element, an attribute, a comment, a processing instruction (PI), whatever. These signals are of course the markup in the document, such as the start and end tags delimiting the elements.

XPath functions at a higher ...

Get XPath and XPointer 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.