XML technologies / XML / XML in a Nutshell / XPointer / Shorthand pointers

(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)

XPointer provides a number of convenient extensions to XPath. Among the simplest are shorthand pointers. Such an XPointer resembles a named anchor in HTML: it identifies its target element by that element’s ID attribute. The ID comes from an ID attribute on the referenced element, rather than a special a element with a name attribute. To reference an element with a shorthand pointer, append the usual separator # to the URL, followed by the target element’s ID. For example, "http://www.w3.org/TR/1999/REC-xpath-19991116.xml#NT-AbsoluteLocationPath" creates a link to the element in the XPath 1.0 specification with an ID attribute whose value is NT-AbsoluteLocationPath.

The ID attribute is an attribute declared with type ID in the DTD of the document. It need not be named ID or id. Shorthand pointers cannot reference elements in documents without DTDs, since such a document cannot have an attribute of type ID.

Note: The inability to use IDs in documents without DTDs is a serious limitation of XML. Work continues to address it. A general ID attribute such as xml:id might provide a solution, or a namespace, identifying ID attributes.

For example, suppose you want to reference the “Motivation and Summary” section of the Recommendation Namespaces in XML at http://www.w3.org/TR/1999/REC-xml-names-19990114/xml-names.xml. A quick look at the source shows that it has an attribute id with value sec-intro and that this attribute is indeed declared with type ID in the associated DTD. Its start-tag looks like this:

<div1 id='sec-intro'>

Thus, "http://www.w3.org/TR/1999/REC-xml-names-19990114/xml-names.xml#sec-intro" is a URL referencing that section. The name need not—and should not—be enclosed in xpointer( ) for this to work. The ID value alone is sufficient. This link is also a useful shorthand for an XPointer based on an XPath expression that uses the function id( ). The same URL could also be written "http://www.w3.org/TR/1999/REC-xml-names-19990114/xml-names.xml#xpointer(id(sec-intro))".

  

 

 

 

back to top