XML technologies / XML / XML in a Nutshell / XPointer / XPointer in URLs
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
A URL identifying a document might look like this: "http://java.sun.com:80/products/jndi/index.html". The scheme in this example, http, tells the application which protocol to use to retrieve the document. The authority, here java.sun.com:80, identifies the host from which to retrieve the document. The authority can also contain the port, to connect to on that host, along with a username and password. The path, /products/jndi/index.html in this example, specifies which file in which server directory to request. This need not be a real file in an actual filesystem, but should be a complete document that the server knows how to generate and return. You already know all this, and XPointer does not change it.
You probably also know that some URLs contain fragment identifiers pointing to named anchors in the referenced document. The hash sign # separates this anchor from the path. Adding the fragment download to the example URL would give: "http://java.sun.com:80/products/jndi/index.html#download". When a browser follows this URL, it searches the document at "http://java.sun.com:80/products/jndi/index.html" for a named anchor called download, which might look like this :
<a name="download"></a>It would then scroll the browser window to that anchor’s position. This is a simple, clear system, sufficient for the basic requirements of HTML. It has a major drawback, however: to link to a specific position, you must be able to modify the target document and insert a named anchor there. XPointer seeks to remove this restriction by letting authors specify link destinations using full XPath expressions as fragment identifiers. XPointer also extends XPath with operations for selecting points or ranges in an XML document that need not correspond to a node or node-set. For example, an XPointer can describe a text range just selected with the mouse.
The simplest XPointer is a XPath expression — often, but not necessarily, a location path — enclosed in the parentheses of xpointer( ). For example, all of the following are valid XPointers:
xpointer(/) xpointer(//vorname) xpointer(id('sec-intro')) xpointer(/leute/person/name/vorname/text( )) xpointer(//mittelinitial[position( )=1]/../vorname) xpointer(//beruf[.="Physiker"]) xpointer(/child::leute/child::person[@index<4000]) xpointer(/child::leute/child::person/attribute::id)
These XPointers do not necessarily identify a single element. Depending on the document against which it is evaluated, an XPointer may identify zero, one, or several nodes. These are usually elements, but can also be attribute or text nodes, points, or ranges.
If you are unsure whether an XPointer will find anything, you can provide an alternative as a fallback. This XPointer, for example, first looks for vorname elements. If none are found, it looks for nachname elements:
xpointer(//vorname) xpointer(//nachname)
The nachname elements are found only if there are no vorname elements. You can chain as many XPointers as you wish. For example, this one first looks for vorname elements. If none are found, it looks for nachname elements. If none of these are found either, it looks for middle initials. If that also fails, it returns an empty node-set:
xpointer(//vorname) xpointer(//nachname) xpointer(//mittelinitial)
Between individual xpointer( ) parts, no special separator or whitespace character is required, although extra whitespace is allowed. This XPointer means the same as the one above:
xpointer(//vorname) xpointer(//nachname) xpointer(//mittelinitial)
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from the book "XML in a Nutshell" is subject to the same conditions as the printed edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de
back to top