XML Pointer Language XPointer
- General Model
- Xpointer Forms
- Using Xpointers
- Future Developments
XPath, described in detail in the previous chapter, provides a common foundation for other standards that need to address into XML documents. One such standard, and the most interesting with regard to implementing hypermedia based on XML technologies, is the XML Pointer Language (XPointer) [DeRose+ 01a], which is used for fragment identifiers for XML resources. According to RFC 3023 [Murata+ 01], XML documents are associated with a number of MIME types.1 For all these di8erent types of XML resources, it is possible to specify a fragment identifier, which is separated from the URI of the resource itself by a crosshatch (#) character. As defined in RFC 2396 [Berners-Lee+ 98] (the standard for URI syntax), a fragment identifier is not an actual part of a URI but is often used in conjunction with one in the so-called URI reference.
Thus, XPointer can be used for specifying references that point to parts of an XML document, and not to the whole document. As a simple example, while the URI http://www.w3.org/TR/ references the technical reports page of the W3C (as shown in Figure 6.1), the URI reference http://www.w3.org/TR/#xpointer(id('xptr')) specifically points to the entry for the XPointer standard on that page.2 This mechanism makes it possible to create links that are much more specific than through the use of URIs only. There are, however, several things to keep in mind, as follows:
Figure 6.1 Snapshot of W3C's technical reports page
The resource must be XML. XPointer is a mechanism for addressing into XML documents, so the resource identified by the URI must be XML.3 In the example just given, this is true since W3C makes its pages available in XHTML, the XML variant of HTML. However, the vast majority of documents on the Web are not XML, and consequently XPointer cannot be used to address into them. While it is assumed that XML resources will become more popular in the near future (in particular since XHTML is the successor of HTML), as long as non-XML browsers are still widely used,4 HTML is likely to remain the most popular language.
As a side note, HTML also supports fragment identifiers, but they are limited to pointing to IDs only (as opposed to the XPath-based addressing capabilities of XPointer). HTML uses its own extremely simple syntax for fragment identifiers, which works by giving the ID as the fragment identifier, so the XML example just given would be equivalent to the HTML version http://www.w3.org/TR/#xptr.5 (In this case, there is a simple correspondence between the XML and the HTML fragment identifier, because both address the fragment using its ID.)
The resource must remain available. Of course, a fragment identifier makes sense only as long as the resource is still available. This brings up the well-known problem of broken links in the Web, and it is independent from specifying fragment identifiers. However, because fragment identifiers are often used with URIs, this issue must be addressed. Resources on the Web often have an astonishingly short life span [Dasen & Wilde 01]; and while some resources disappear (i.e., no longer exist or at least are no longer available via a known URI), others are moved to a new URI without having automatic redirections set up by the Web server operator.
The ID must remain the same. In cases where the fragment identifier uses an ID within the document, it will work correctly only as long as the ID remains valid within the document (and, in the example just given, continues to identify the element representing the XPointer entry within the document). However, since we do not have control over the W3C's document management and identification policy, we have no guarantee that the ID will always be the same and that it will always identify the element we want to reference. The basic dilemma behind this is that the resource (the W3C's Web page) and the reference to it (our fragment identifier) are handled by di8erent entities, which do not necessarily cooperate (or even know each other; for example, even though we know the W3C's Web pages, the W3C probably does not know that we used their ID as an example in this book).
The client has to support XPointers. Even if all previous requirements are satisfied (i.e., the document is XML, it is still available via the URI, and the XPointer can still be interpreted meaningfully), the application processing the URI with the fragment identifier must implement the XPointer standard. At the time of writing, this is not the case for almost all available software, though we hope this will change in the near future. In comparison to XLink and XPath, XPointer is lagging behind in the standardization process; and as long as there is no stable standard, it cannot be implemented.
The major browsers in their most current versions (at the time of writing, Internet Explorer 5.5, Navigator 6, and Opera 5) all support XML in the sense that they are able to not only download and interpret XML documents, but also to display them using style sheet mechanisms (CSS and/or XSL). There is, however, no support for XPointer currently. Nevertheless, as soon as XPointer reaches recommendation status, we hope to see XPointer support (as well as XLink support) in the next releases of the major browsers.
These are the requirements that must be met when using XPointer. We believe that in the near future XPointer (along with many other XML-based technologies) will become widely supported and a popular technology. For an illustration of how XPointer may not only become useful for hypermedia applications (which are the focus of this book) but also for other relatively simple cases of usage, consider the following scenario:
You find an interesting quote on the Web, possibly in an XHTML resource, that you would like to send to a friend. Instead of copying the quote into an e-mail (which would mean taking the quote out of context) or simply sending the resource's URI (which would make it necessary to somehow indicate exactly which part of the resource you mean), you select the quote with the mouse and then choose the "Generate XPointer" option from your browser's menu, which automatically generates a URI reference that exactly identifies the selected quote. You paste this URI reference into the e-mail and send it to your friend. This way, you have exactly identified the quote that was important to you without taking it out of context. Upon receiving the URI reference, your friend's browser not only requests and displays the resource containing the quote but also automatically highlights the quote identified by the XPointer part of the URI reference.
This example depends on the browser's ability to generate XPointers. Ideally, it would do so in a clever way, because for each subresource there is a multitude of possibilities for creating an XPointer identifying it. We will discuss this important issue in detail later in this chapter (in sections 6.4.3 and 6.4.4), but by now it should be clear that XPointer can provide a lot of value in an XML-based Web.
We now look at the details of XPointer. Section 6.1 discusses the general data model of XPointer, which is a generalization of XPath's data model. After this introductory section, we go into the details of how XPointers may be used as fragment identifiers, described in section 6.2. The next issue is XPointer's extensions to XPath and, in particular, the additional functions that XPointer defines. These functions are discussed in section 6.3.
After this rather formal discussion of XPointer, we then spend some time considering possible usage scenarios and how XPointer may be applied in the best possible way (section 6.4). Finally, even though XPointer is a very new standard, in section 6.5 we briefly describe our view of what XPointer's future may look like.
6.1 General Model
One of the most important aspects of XPointer is that it defines a generalization of the XPath concepts of nodes, node types, and node sets (as described in section 5.1) to the XPointer concepts of locations, location types, and location sets.6 As a reminder, nodes, node types, and node sets in XPath are used to describe concepts that can be identified as nodes in a document's tree representation, as described by the XML Infoset. XPath functionality, such as filtering an axis output by predicate, is generally de-fined in terms of operations on nodes and node sets (an exception are the string functions, but these are rather limited and always operate on strings within one text node).
XPointer's goal is to define a mechanism for XML fragment identifiers. A very common usage scenario is a user selecting arbitrary document content with a mouse and then wishing to have an XPointer generated that identifies exactly that content (e.g., to use the XPointer for creating a link pointing to that content). Since this selection can span multiple elements and furthermore may start in the middle of the text of one element and end in the middle of another, it is impossible to identify this content with XPath's constructs of nodes or strings. XPointer's solution to this problem is an extension of XPath's data model, described in section 6.1.1. To make the concepts of XPointer's data model easier to understand, we give some examples in section 6.1.2 of how this model maps to real-world scenarios.
6.1.1 XPointer Data Model
XPointer generalizes the concept of XPath nodes to locations, and, in essence, this generalization defines each location to be an XPath node, a point, or a range.7 The following definitions are taken from the XPointer specification8 and show how XPath's definition of a NodeType is extended by the concepts point and range:
Based on these definitions, XPointer also defines the location set as a generalization of XPath's node set. This definition allows XPath node tests to select locations of type point and range from a location set that might include locations of all three types. All locations generated by XPath constructs are nodes, but XPointer constructs can also generate points and ranges. The concepts of points and ranges are defined in the next two sections.
A location of type point is defined by a node, called the container node, and a non-negative integer, called the index. It can represent the location preceding any individual character, or preceding or following any node in the information set constructed from an XML document. Two points are identical if they have the same container node and index. Each point can be either a node point or a character point, which are defined as follows:
Node point. If the container node of a point is of a node type that can have child nodes (that is, when the container node is an element node or a root node), then the index is an index into the child nodes, and such a point is called a node point. The index of a node point must be greater than or equal to zero and less than or equal to the number of child nodes of the container. An index of zero indicates the point before any child nodes, and a non-zero index n indicates the point immediately after the nth child node.
Character point. When the container node of a point is of a node type that cannot have child nodes (i.e., text nodes, attribute nodes, namespace nodes, comment nodes, and processing instruction nodes), then the index is an index into the characters of the string value of the node. Such a point is called a character point. The index of a character point must be greater than or equal to zero and less than or equal to the length of the string value of the node. An index of zero indicates a point immediately before the first character of the string value, and a non-zero index n indicates the point immediately after the nth character of the string value.
Figure 6.2 shows the relationship of container nodes, node points, and character points for an example of an element containing text, then another element (which also contains text), and then some more text.
Figure 6.2 Container nodes, node points, and character points. Reproduced from the XPointer specification [DeRose+ 01a] by kind permission of Steven DeRose, spring 2002.
XPointer's goal is to make XPath's concepts applicable to locations and not only to nodes, and thus the following properties for applying XPath's concepts to points are defined: The self and descendant-or-self axes of a point contain the point itself. The parent axis of a point is a location set containing a single location, the container node. The ancestor axis contains the point's container node and its ancestors. The ancestor-or-self axis contains the point itself, the point's container node, and its ancestors.
The child, descendant, preceding-sibling, following-sibling, preceding, following, attribute, and namespace axes of points are always empty.
A range is defined by two points, a startpoint and an endpoint.9 A range represents all of the XML structure and content between the startpoint and the endpoint. A range whose start- and endpoints are equal is called a collapsed range. If the container node of one point of a range is a node of a type other than element, text, or root, then the container node of the other point of the range must be the same node.10 The axes of a range are identical to the axes of its startpoint.
As a side note, remember that node sets are only one of the object types defined in XPath (the others being boolean, number, and string), and the other XPath object types also exist in XPointer. However, there is one important exception, and this is the result type of a whole XPointer. While an XPath can evaluate to any object type (consider the rather simple XPath 2+3, which evaluates to a number), XPointer requires an XPointer to always evaluate to a location set (which is logical, given that there is no way that objects other than location sets could be interpreted as fragment identifiers pointing into documents).
In order to understand some of the functions XPointer provides, it is also essential to introduce the concept of the covering range. By definition, a covering range is a range that wholly encompasses a location. This means that the concept of a covering range can be applied to any location, whether it be a node, a point, or a range. Basically, a covering range is the smallest possible range covering a given location. In detail, this is defined as follows:
For a range location, the covering range is identical to the range.
For an attribute or namespace location (both node locations), the container node of the start point and end point of the covering range is the attribute or namespace location; the index of the startpoint of the covering range is 0; and the index of the endpoint of the covering range is the length of the string value of the attribute or namespace location.
For the root location (a node location), the container node of the startpoint and endpoint of the covering range is the root node; the index of the start point of the covering range is 0; and the index of the endpoint of the covering range is the number of children of the root location.
For a point location, the start- and endpoints of the covering range are the point itself.
For any other kind of location, the container node of the startpoint and endpoint of the covering range is the parent of the location; the index of the startpoint of the covering range is the number of preceding sibling nodes of the location; and the index of the endpoint is one greater than the index of the startpoint.
In summary, XPointer's extensions to XPath's data model include the extension of the concept of nodes and node sets to that of locations and location sets (with the location being a node, a point, or a range). XPointer also introduces the concept of a covering range to support mapping of locations to ranges. For each location, there is a well-defined covering range.
In addition to these extensions of XPath's data model, XPointer also extends the concept of document order, as introduced in section 5.1. In general, the concept is extended not only to arrange nodes in a well-defined order but also to include point and range locations. Figure 6.2 shows how the locations of a document fragment are arranged in XPointer's document order. For defining rules regarding how to determine document order, XPointer introduces the concept of an immediately preceding node and then uses it to define how every possible combination of the relevant location types (i.e., nodes, points, and ranges) have to be compared to establish the document order of these locations.
6.1.2 XPointer Data Model Examples
To make these abstract definitions more understandable, we give some examples of how the concepts of locations, points, ranges, and nodes relate to each other. Our scenario is a browser that allows a user to create XPointers by selecting content with a mouse and then using a menu option for generating an XPointer identifying that content.
Marking a Point Within a Document
The simplest use is to mark a point within the document and generate an XPointer for this point (e.g., for creating an XPointer that is attached to an e-mail saying "please insert your text here"). To do this, the browser generates an XPointer that is a point. Depending on where the point has been selected, it is either a node point (if it has been marked within the root node or an element node) or a character point (in all other cases). Depending on how the browser was implemented, di8erent XPointers that refer to the same point could be created. Consider the case where a user selects the point before the first character of a paragraph. Depending on the browser's implementation, this could result in the following:
a node point into the paragraph's parent element node, identifying the point before the paragraph child,
a node point with index zero within the paragraph's element node, or
a character point with index zero within the text node of the paragraph's text.
All these cases make sense. In the first, the point could be used to insert elements before the paragraph element. In the second, the point could be used to insert elements into the paragraph before the first character; while in the third, it could be used to insert characters before the first character within the paragraph's text. It is entirely dependent on the implementation and application how these cases should be treated. Indeed, one possibility would be for the browser to present the user with a choice as to which is the appropriate XPointer.11
Selecting text within one node is the equivalent of selecting two points,12 the start and the end point of the selection, that lie inside the same node (e.g., the same element, attribute, or comment). From the user's point of view, it may not be apparent whether or not the two points are located within the same node (e.g., if two elements are not visibly separated by any formatting); but for the browser, this is easy to determine. Based on the selection of the user, the browser generates an XPointer defining a range between the selected start and end points. Since in this case the selected text lies within one node, the two points can easily be used to construct an XPointer range, which spans between the two points.
Selecting Text That Spans a Number of Nodes
If the start and the end point lie in di8erent nodes, then the range defined by the two points also spans multiple nodes. According to the range constraints defined by XPointer, this is allowed only if the containing nodes of the startpoint and the endpoint are element, text, or root nodes (in all other cases, the start and endpoint must lie within the same node). A possible selection that spans a number of nodes in the case of an XHTML-like paragraph may select text that is all inside the paragraph but still spans multiple nodes. This may be because in between there are other element nodes (e.g., nodes representing emphasized text or hyperlinks). This situation e8ectively places the start and the endpoints in di8erent text nodes (making them both children of the same paragraph node).
To further generalize this scenario, the start point and the end point may occur in entirely di8erent subtrees of the document tree, for example, within a paragraph of the first chapter and in a table cell of the third chapter. This still would represent a valid range, spanning from the character point in the text node representing the first chapter's paragraph to the node point in the table row node directly after the selected table cell.
Making Multiple Selections
Even though today's user interfaces in most cases do not support this type of interaction, it would be perfectly reasonable to implement an interface that allows multiple non-contiguous selections. This could be used to create an XPointer that references multiple ranges within the same resource (e.g., attached to an e-mail saying "what do you think about these three statements?"). In a case such as this, it is mainly a question of the design of the user interface as to how multiple selections could be implemented in a user-friendly way. From XPointer's point of view, the multiple selections could be easily combined to yield a single location set.
The preceding scenarios illustrate some typical cases in which XPointer concepts are relevant.13 So far we have not discussed how exactly the hypothetical browser maps the concepts to actual XPointers, and as a first step toward resolving this issue, we first have to discuss the possible forms of XPointers.