Table of Contents
XPath makes it possible for a processor to navigate around the hierarchy of XPath nodes to a particular part or parts of the document and may return a set of such nodes, called a node set, or may return a value which is a string, a number, or a Boolean value (i.e., true or false). A particularly important use of XPath is for matching. For example, if you wanted to transform an XML source document and present selected elements of it as HTML, then an XPath expression can be written to do that. The XPath expression would allow those selected elements (and only those elements), or, more precisely, the nodes which represent those elements, to be matched by the templates or elements in the XSLT stylesheet and their content displayed suitably within the resulting HTML document.
In this general process of using XSLT to create an output tree, XPath fulfills the crucial role of enabling you to choose which elements, attributes, or other parts of the source tree you show to a user in any particular context. XPath expressions and location paths are used to select nodes for inclusion in the result tree. If an XPath location path matches the node you want to output, then the desired output is created. XPath expressions can be used, in effect, to hide information from some or all users. If, for example, you had selling details of the price you paid suppliers within the information that you held about a company s product, you would not want your customers to be aware of that. Such sensitive or confidential information could be selectively suppressed from being part of the output by omitting those elements from the XPath expressions used to create the output tree.
XPath has, fairly confusingly, four forms of syntax, which may be used to write expressions and location paths.
You will perhaps notice in that XPath expression a similarity to the way a path, as in a directory listing or in a URL, is written. That similarity to other path notations gives rise to XPath s name. It is a path language for XML, not a path language in XML.
XPath can be viewed as a way to navigate round XML documents. Thus XPath has similarities to a set of street directions. When you are receiving street directions, you need to know what your starting point is. In XPath the starting point is called the context node. The logical parts of the in-memory representation of an XML document are termed nodes. An XPath processor deals with nodes, not with the more familiar elements and attributes.
In XPath the equivalent of a direction is called an axis. XPath has a total of 13 different axes, which we will look at in more detail later. A particularly commonly used axis is the child axis.
<?xml version='1.0'?> <Invoice> <CustomerName> John Smith </CustomerName> <Address> 123 Any Street </Address> <City> Anytown </City> </Invoice>
the XPath expression child::* selects all child element nodes of the context node would select the nodes that represent the <CustomerName>, <Address>, and <City> elements.
If we wanted to select only the <CustomerName> element, we could use a more specific XPath expression child::CustomerName which selects only child elements of the context node that have an element type name of CustomerName.
XPath can also select a <CustomerName> element from our example XML document using yet another syntax
/child::Invoice/child::CustomerName
which can be abbreviated to
/Invoice/CustomerName
Already you have seen three out of the four forms of syntax that can be used in XPath.