- General Model
- Xpointer Forms
- Functions
- Using Xpointers
- Future Developments
- Conclusions
6.4 Using Xpointers
So far we have discussed the specifics of XPointer as a way of identifying resource fragments. In this section, we talk about some of the issues that arise when using XPointers. One of the obvious problems when creating and using XPointers in an environment that is essentially character-based is the issue of character escaping (described in section 6.4.1). Another topic of a similar nature is the question of how to use XPointers with XML Namespaces (discussed in section 6.4.2).
XPointers identify resource fragments by describing ways for locating them inside the resources. As pointed out earlier, this can be done in an endless variety of ways, so composing XPointers is not a mechanical process. Rather, it requires some intelligence in order to compose "good" XPointers. Since XPointer is a new technology, there is not much implementation experience to build on; but nevertheless in section 6.4.3 we describe some guidelines for composing good XPointers. Finally, in section 6.4.4 we look into the question of what exactly good means. In many cases, it will have a lot to do with ensuring XPointer persistence.
6.4.1 XPointer Character Escaping
While composing XPointers is based on the way subresources within XML documents should be identified, they must also be coded in a way that makes it possible to exchange and interpret them unambiguously. XPointers use a character-based notation and are thus easy to compose and read. But several characters within XPointers have special meaning and must therefore be escaped, if they have to be embedded into XPointers. Because of the di8erent standards involved when actually using an XPointer, escaping mechanisms occur on di8erent levels, as follows:
XPointer escaping rules. The XPointer specification defines escaping rules for some special characters. Most importantly, parentheses in XPointer must be balanced. This is because XPointer is built on the assumption that the end of syntactic constructs using parentheses can be found by identifying the balanced parenthesis. Consequently, unbalanced parentheses in XPointers must be escaped, and this is done by prefixing them with the circumflex character, "^". This makes it necessary to also escape the circumflex character, which is done by escaping it with itself (i.e., the literal circumflex character within an XPointer is written as "^^").
XML escaping rules. Very often XPointers will be used within XML documents, and in this case XML's rules for escaping XML special characters must be observed. This means that any characters not representable in the character encoding of the XML document (as well as any characters relevant for XML markup) must be written as character references or as predefined entity references.
URI escaping rules. URI references must adhere to the syntactic rules defined by RFC 2396 [Berners-Lee+ 98], which allows only a limited set of characters. All other characters must be represented using the URI escape mechanism, which represents these characters by a percent sign, "%", followed by two hexadecimal digits.
These character-escaping rules in many cases must be combined when XPointers are used in an XML-based environment. Consequently, character escaping can become quite complicated. The XPointer specification gives some examples of how the di8erent escaping mechanisms a8ect a given XPointer (reproduced in Tables 6.2 and 6.3).
Table 6.2 XPointerChar acterEscaping (Example 1)
Level |
Example |
Initial |
xpointer(string-range(//P,"a little hat ^")) |
XPointer |
xpointer(string-range(//P,"a little hat ^^")) |
XML |
xpointer(string-range(//P,"a little hat ^^")) |
URI |
xpointer(string-range(//P,%22a%20little%20hat%20%5E%5E%22)) |
Table 6.3 XPointer Character Escaping (Example 2)
Level |
Example |
Initial and XPointer |
xpointer(id('resum ´ e')) ´ |
XML |
xpointer(id('résumé')) |
URI |
xpointer(id('r%C3%A9sum%C3%A9')) |
In the first example (Table 6.2), it is interesting to see that even trivial things such as space characters must be escaped in the URI encoding because spaces are not allowed to appear literally within URIs. In the XML encoding, the double quotes must be escaped only if the XPointer appears within an XML attribute that is delimited with double quotes.
The second example (shown in Table 6.3) shows how to deal with non-ASCII characters. Because XPointer is based on Unicode, the accented letter appears both in the initial and the XPointer form. Based on the assumption that the XML document supports only ASCII, the accented letter must be represented by a character reference to its Unicode code point [Unicode 00]. In the URI encoding, however, the accented character has first to be encoded in UTF-8 [Yergeau 98] before the resulting byte sequence is escaped, so the result looks quite di8erent from the XML escaping.
6.4.2 XPointers and Namespaces
In section 6.2.3, we discussed how XPointer defines the concept of schemes (in fact, each full XPointer is nothing more than a sequence of scheme-specific parts) and that currently only the xpointer and the xmlns schemes are specified. XPath (and thus, XPointer) makes it possible to use qualified names that have a namespace prefix and a local part. (For a discussion of qualified names and XML Namespaces in general, see section 4.2.) In XML documents, the namespace prefix can easily be interpreted because, in order for the qualified name to be valid, there must be a namespace declaration associating that prefix with a namespace URI somewhere on an ancestor element.28 This is no problem since the qualified names in XML are embedded into the context provided by the XML document (in particular, the namespace declarations within this document). An XPointer, however, does not have such a context because it may be used outside any document, simply as part of a URI reference. Consequently, there must be a way to establish the context of namespace declarations for XPointers.
XPointer defines the xmlns scheme for declaring namespaces. This is, in a way, very similar to namespace declarations in XML documents. Each xmlns scheme part associates one namespace prefix with a namespace URI. However, the syntax is slightly di8erent from the one used in XML. The syntax is defined in rules 9 and 10 of the standard, as shown in section 6.2.3 (and repeated here):
Thus, whenever an XPointer is used that contains qualified names, it has to contain xmlns scheme parts for declaring the prefixes being used in the qualified names, as shown in the following example:
...#xmlns(html=http://www.w3.org/1999/xhtml)xpointer(//html:h3[9])
It is important to note that the prefix used in the XPointer and the prefix used in the resource need not be the same in order for the XPointer to match. The important part in this case is the namespace URI, so it is necessary only that the URI in the XPointer xmlns scheme part and the URI in the XML document (i.e., the namespace declaration using the xmlns attribute) are the same.
If two xmlns scheme parts within one XPointer declare the same prefix, then the second (i.e., right) declaration overrides the first (i.e., left) one. However, because evaluation of XPointer scheme parts is done stepwise from left to right, an xpointer scheme part that appears between the two xmlns scheme parts declaring the same prefix will be interpreted using the first declaration.
6.4.3 How to Compose XPointers
In section 5.5 we described in detail how to use XPath. The same principles apply to XPointer, particularly the key points of "being as specific as possible" and "filtering as early as possible." However, it is important to see the di8erence in possible application scenarios:
XPath and XSLT. Today, the most frequently used application of XPath is in XSLT. In XSLT, XPaths may be evaluated very often during the processing of a style sheet, so it is important to keep an eye on the eSciency of the XPaths being used. Furthermore, the XSLT author often also controls the XML document (as well as the schema behind it), which makes it easier to compose XPaths that are not compromised by modifications to documents or even the schema.
XPath and XPointer. In XPointer, however, XPath is often used for identifying fragments in resources not under the control of the XPointer's author. On the other hand, XPointers are usually evaluated only once (when locating the fragment within the resource), so eSciency is not a significant issue. Robustness, on the other hand, is very important since the XPointer should continue to work even if the resource that it points into changes.
Consequently, there is a di8erence between using XPath in the context of XSLT and using it in the context of XPointer. In general, the most important aspect of composing XPointers is robustness, which therefore is discussed separately in section 6.4.4.
As pointed out already, there are countless ways for each given fragment to be identified by an XPointer. We have already demonstrated this with the example of W3C's technical reports Web page at the start of this chapter. Continuing this discussion, we could add that, if, for example, the heading was not identified by the last-call ID, it would be possible to locate the heading based on its content,29 as shown in the following URI:
http://www.w3.org/TR#xpointer(//h3[contains(string(.), 'Working%20Drafts%20in%20Last%20Call')/following::dl[1]/dt[7]//a)
However, it would not make much sense to list a huge number of possible XPointers identifying the same resource because this list would never be exhaustive. Furthermore, without knowing the schemas behind the resources and the characteristics of how they are modified, it is hard to actually rate the many variants qualitatively.
The main point is that anybody involved in the creation of XPointers (either manually or programmatically) should be aware that this is not a strict science but more of an art form. In particular, any software generating XPointers should be carefully designed in order to generate good XPointers, and doing this is a non-trivial task. The quality criteria depend on the application domain and on how much knowledge there is available about the resources being used. In particular, one criterion that will very often be highly ranked is the persistence of XPointers.
6.4.4 Persistence
As discussed in the introduction to this chapter, the persistence of an XPointer is a serious issue (see also the discussion in section 3.3.2). Even if the resource addressed by a URI is still available, it may have changed, and the XPointer may not work any more, or it may not work as expected. In order to construct robust XPointers (i.e., XPointers that are tolerant against modifications of the resource), it is necessary to follow some guidelines. These guidelines, however, depend on how much is known about the resources being used.
As an example, it is fairly certain that the W3C will keep its overall structure of the technical reports page (see Figure 6.1), and that additions to the individual sections (e.g., "Working Drafts in Last Call") will always be at the start of the section. It would therefore be a reasonable idea to use a section's ID and to then start to count from the end of the list contained within the following:
http://www.w3.org/TR#xpointer(id('last-call')/ following::dl[1]/dt[last()-4]//a)
While this approach could (and probably would) work within the "Recommendations" or "Notes" sections (where documents remain), it is less likely to work in the "Working Drafts in Last Call" section where working drafts may change status and may be deleted. By now it should be clear that constructing robust XPointers requires a good deal of knowledge about the resources, which may be impractical or too expensive to acquire.
Using IDs is always a good idea; and as long as there are IDs being used within the resource, it is a good idea to start with an ID and then navigate from there. But again, if the schema of a resource is unknown, then it is not really possible to find out which attributes are used as IDs. (However, it may be possible to make an educated guess, such as looking for attributes with unique values or attributes having the string id as part of their name.)
Besides all these worries about XPointers becoming invalid or incorrect because of resource modifications, it should always be remembered that even though XPointer is the W3C standard for XML fragment identification, it is not the only means of identifying fragments. If, for example, the validity of fragments is very important, then one alternative might be not only to generate XPointers but also to generate information for checking the XPointer's validity. One approach to this might be to use the modification time of the resource at the time the XPointer was generated, or even a digital fingerprint of the resource or subresource (using checksum algorithms such as MD5 [Rivest 92] or SHA [NIST 93]).
Information such as dates or checksums could easily be incorporated into XPointers themselves by using proprietary schemes, which, by defini-tion, would be ignored by applications not knowing or supporting them. That way it would be possible to generate XPointers that would work on all platforms supporting XPointer and that would have the added benefit of being able to be tested for possible modifications of the fragment by platforms supporting the additional XPointer scheme.