XML technologies / XML / XML in a Nutshell / XPointer / XPointer in links
(Excerpt from "XML in a Nutshell" by Elliotte Rusty Harold & W. Scott Means)
An XPointer’s target depends on the document to which it applies, identified by the URL associated with it. For example, to reference the first name element in the document at "http://beispiel.org/leute.xml", enter:
"http://beispiel.org/leute.xml#xpointer(//name[position( )=1])"
If an XPointer uses characters forbidden in URIs, such as the less-than sign <, double quotation marks " or non-ASCII characters such as é, URI rules require hexadecimal escaping before combining it with the URI. Each is replaced by a percent sign followed by the hexadecimal value of every byte in its UTF-8 encoding of Unicode. Thus, < becomes %3C, " becomes %22 and é becomes %C3%A9.
In HTML URLs in a elements may contain XPointer fragment identifiers:
<a href="http://beispiel.org/leute.xml#xpointer(//name[position( )=1])">Der Name einer Person</a>A browser following this link would probably load the entire document at "http://beispiel.org/leute.xml" and scroll to the start of its first name element. However, no browsers yet support the XPointer scheme, so the exact behavior remains open to discussion. Sometimes displaying only the referenced nodes rather than the whole document would make sense.
Note: Mozilla 1.4 and later support Simon St.Laurent’s proposed XPointer scheme xpath1(). xpath1( ) is essentially identical to the scheme discussed here, xpointer( ). xpath1( ) does not include the XPath point and range extensions supported by the scheme xpointer( ). It supports only plain XPath 1.0 expressions, simplifying implementation.
Because XPath finds nodes only in well-formed XML, XPointers can target only XML documents, not malformed HTML, plain text, or other formats. They may nevertheless originate in HTML documents, be printed in books, painted on walls, or transmitted in any way that carries text.
XPointers are more commonly used in XLinks. This simple link targets the first buch child of the child element buecher of the root element testament in the document at the relative URL at.xml:
<Am_Anfang xlink:type="simple" xlink:href="at.xml#xpointer(/testament/buecher/buch[position( )=1])">Die Genesis</Am_Anfang>In extended links, an XPointer can identify both starting and ending resources of an arc. This extended XLink for example, establishes an arc between the last v element in the document at at.xml and the first v element of the document at nt.xml. It then creates a link from the first v element in nt.xml to the last v element in at.xml:
<Bibel xlink:type="extended" xmlns:xlink="http://www.w3.org/1999/xlink">
<testament xlink:type="locator" xlink:label="at.2" xlink:href="at.xml#xpointer(//v[position()=last( )])" />
<testament xlink:type="locator" xlink:label="nt" xlink:href="nt.xml#xpointer(//v[position( )=1])" />
<nächstes xlink:from="at" xlink:to="nt"/>
<vorheriges xlink:from="nt" xlink:to="at"/>
</Bibel>Links can be entirely internal, connecting locations in the same document. The element folie in this presentation example contains simple XLinks to the first and last slide elements:
<folie xmlns:xlink="http://www.w3.org/1999/xlink">
<punkt>Acme Wonder Goo ist ein ausgesprochen leckeres Dessert!</punkt>
<punkt>Acme Wonder Goo ist ein kräftiger Fliesenreiniger!</punkt>
<punkt>Sie erhalten zwei Produkte in einem!</punkt>
<erste xlink:type="simple" xlink:href="#xpointer(//folie[position( )=1])">Anfang</erste>
<letzte xlink:type="simple" xlink:href="#xpointer(//folie[position( )=last()])">Ende</letzte>
</folie>If the XPath expressions in an XPointer are relative, the context node is the root element of the entity containing the XPointer.
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