Chapter 4. XPath Expressions

  • Expressions

  • Location Path

  • Patterns

  • Axes, node test, and predicate

  • Position()

  • Abbreviations

XPath enables you to access parts of an XML document instance using “family affair” navigation relationships like ancestor, descendant, parent, child, and sibling. It provides the processor-readable (and human-readable) syntax for doing this, in the form of expressions.

In Chapter 1, we identified the tree structure by which XML addresses nodes in a document. In this chapter, we will explain the use of expressions, in the form of patterns and location paths, as well as location steps within those paths. We begin with the basic concepts of XPath, the XPath syntax and concepts like document order, context, and proximity. Chapter 5 will build upon these concepts with XPath functions, rounding out the intricate mechanisms that can be part of an XPath expression.

It is no accident that both the XPath and XSLT specifications became recommendations from the W3C on the same day in November 1999. XSLT elements rely entirely on the expressions provided by XPath to select and extract nodes from the input XML document. While many common functions of XSLT could be achieved with macros and scripts, XPath adds an easy, user-friendly interface.

Note

From observation of developers' lists and user groups for extensible markup technology, it quickly becomes clear that proper knowledge of XPath is frequently more challenging than mastering the variety of XSLT functions. Typically, an XSLT template will be properly written but will fail to operate correctly—or as intended—due to improper selection of a node-set in the match expression.


XPath expressions enable you to access any node or combination of nodes of an XML document with unlimited granularity.

There is a difference between knowing the path and walking the path.

—Morpheus, The Matrix

XPath operates with the implicit assumption that, while you may know part of the logical structure or path for the nodes in a document, you don't need to know all of it. In other words, what you know of the document's detail and how much the XSLT processor can actually do with it can fortunately be two very different things.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.246.159