O'Reilly logo

XPath and XPointer by John Simpson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required