Text Nodes The XPath Data Model

page 43 location paths described the parts of the XML document we wanted to work with. Most of the XPath expressions youll use are location paths, and most of them are pretty simple. Before we dive in to the wonders of XPath, we need to discuss the context.

3.2.1 The Context

One of the most important concepts in XPath is the context. Everything we do in XPath is interpreted with respect to the context. You can think of an XML document as a hierarchy of directories in a filesystem. In our sonnet example, we could imagine that sonnet is a directory at the root level of the filesystem. The sonnet directory would, in turn, contain directories named auth:author , title , and lines . In this example, the context would be the current directory. If I go to a command line and execute a particular command such as dir .js , the results I get vary depending on the current directory. Similarly, the results of evaluating an XPath expression will probably vary based on the context. Most of the time, we can think of the context as the node in the tree from which any expression is evaluated. To be completely accurate, the context consists of five things: • The context node the current directory. The XPath expression is evaluated from this node. • Two integers, the context position and the context size. These integers are important when were processing a group of nodes. For example, we could write an XPath expression that selects all of the li elements in a given document. The context size refers to the number of li items selected by that expression, and the context position refers to the position of the li were currently processing. • A set of variables. This set includes names and values of all variables that are currently in scope. • A set of all the functions available to XPath expressions. Some of these functions are defined by the XPath and XSLT standards themselves; others might be extension functions defined by whomever created the stylesheet. Youll read more about extension functions in Chapter 8 . • A set of all the namespace declarations currently in scope. Having said all that, most of the time you can ignore everything but the context node. To use our command line analogy one more time, if youre at a command line, you have a current directory; you also have depending on your operating system a number of environment variables defined. For most commands, you can focus on the current directory and ignore the environment variables.

3.2.2 Simple Location Paths

Now that weve talked about what a context is and why it matters, well look at some location paths. Well start with a variety of simple location paths; as we go along, well look at more complex location paths that use all the various features of XPath. We already looked at one of the simplest XPath expressions: xsl:template match= This template selects the root node of the document. We saw another simple XPath expression in the xsl:value-of element: xsl:value-of select=.