Chapter 2. Introducing XPath

XPath 2.0 is an expression language that is absolutely fundamental to XSLT 2.0 in several important ways.

A common use of XPath in XSLT is selecting nodes in an XML document. For example, you can make a very wide selection and refer to all the <li> elements in a document using the expression select="//li", or be very specific by pointing to the class attribute in the first <p> in the third <div> of a document using div[3]/p[1]/@class.

XPath expressions are also used by an XSLT processor to match source tree nodes to template rules. As the XSLT processor traverses the element nodes in an XML source document, it looks for a corresponding XPath expression that you set in the match attribute of an <xsl:template> declaration.

XPath functions provide you with a wide range of features, enabling you to manipulate strings, dates and times, numbers, and nodes.

In this chapter you take a break from creating full-scale stylesheets and look into some XPath 2.0 features in some detail. You'll do all of the following:

  • Review node types and properties.

  • Look at the XPath data model.

  • Tackle the important topic of path expressions used to navigate the node trees in your source documents.

  • Get a brief overview of using the XPath Analyzer—a tool that can help you avoid problems along the XPath trail.

  • Study a rich set of XPath functions that you will encounter repeatedly as you develop your XSLT skills.

Nodes

In the introduction you saw that XPath models the XML document as a tree of ...

Get Beginning XSLT and XPath: Transforming XML Documents and Data 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.