XML technologies / XML / XML in a Nutshell / XPointer / Ranges
(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)
A range is a span of parsed character data between two points. It may represent well-formed XML, but need not: it can include an element’s start-tag while omitting its end-tag. Ranges therefore suit uses such as representing text selected with a mouse. Four functions create ranges, added by XPointer to XPath:
range( )range-to( )range-inside( )string-range( )
The range( ) function
The function range( ) takes an XPath expression as an argument returning a location-set. For each location in the set, range( ) returns a range covering exactly that location: it starts immediately before and ends immediately after it. For an element, the range starts directly before its start-tag and ends directly after its end-tag. Consider this XPointer:
xpointer(range(//titel))
Applied to the example A novel document on the page Points, it selects the range covering exactly the single titel element. If the document contained several titel elements, it would return a range for each titel element. If there were no titel elements, it would return no range.
Now consider this XPointer:
xpointer(range(/roman/*))
Applied to the same example, it returns three ranges, each covering one of the three children of the root element roman.
The range-inside( ) function
The function range-inside( ) takes an XPath expression returning a location-set. For each location, it returns a range covering exactly that location’s content. Except for element nodes, the result equals that of range( ). For an element node, the range covers everything inside it, excluding its start-tag and end-tag. Applied to the same example, xpointer(range-inside(//titel)) returns a range covering The Wonderful Wizard of Oz but not <titel>The Wonderful Wizard of Oz</titel>. For comment, processing-instruction, attribute, text and namespace nodes, the range covers the node’s string-value.
The range-to( ) function
The function range-to( ) is evaluated relative to a context node. Its argument is a location-set expected to contain exactly one location. The context nodes’ starting points become the starting points of ranges returned by range-to( ). The argument’s endpoint determines the ranges’ endpoint. If the context node list contains several nodes, range-to( ) returns several ranges.
Note: This function is underspecified in XPointer. In particular, behavior is unclear when the argument contains fewer or more than one location.
Suppose you want one range covering everything between <titel> and </jahr> in the example above. This XPointer does so by starting at the start-point of the titel element and continuing to the end-point of the jahr element:
xpointer(/titel/range-to(jahr))
Ranges need not be well-formed XML fragments. They may include an element’s start-tag but omit its end-tag. This XPointer selects <titel>The Wonderful Wizard of Oz:
xpointer(/titel/range-to(text( )))
It starts at the start-point of the titel element but ends at the end-point of the text-node child of the titel element, omitting the end-tag.
The string-range( ) function
The function string-range( ) is unusual. Instead of working with a location-set including tags, comments and processing instructions, it works with document text after markup removal. Tags are more or less ignored.
The function string-range( ) takes an XPath expression identifying locations and a substring compared with each location’s XPath string-value. For every non-overlapping match, it returns a range covering exactly the matched string. This XPointer, for example, creates ranges for every occurrence of “Wizard” in the document’s titel elements:
xpointer(string-range(//titel, "Wizard"))
Multiple matches return multiple ranges. Applied to the example, this XPointer returns two: one covering the W in “Wonderful” and one the W in “Wizard”:
xpointer(string-range(//titel, "W"))
You may also supply an offset and length, so the string starts a given number of characters after the match’s beginning and spans a specified number of characters. The point before the searched string’s first character is 1. This XPointer selects the first four characters after “Wizard” in titel elements:
xpointer(string-range(//titel, "Wizard", 7, 4))
Nonpositive offsets work backward from the match’s beginning. This XPointer selects the first four characters before “Wizard” in titel elements:
xpointer(string-range(//titel,"Wizard", −3, 4))
If offset or length would place the range outside the document, no range is returned.
Since string ranges can begin and end at almost any text character, they suit points not coinciding with node boundaries. Create a string range beginning or ending at the desired position, then apply start-point() or end-point() to it. This XPointer returns the point immediately before “Wizard” in the titel element:
xpointer(start-point(string-range(//title, "Wizard")))
Relative XPointers
An XPointer normally is a fragment identifier attached to a URL. The referenced document’s root is its context node. XPointers may also appear independently in XML documents, without an explicit URL; their default context is the containing document’s root. The functions here() or origin() can change the context node for the XPointer’s XPath expression.
here()
The function here() is used only within XML documents. It identifies the node containing the XPointer, or, if that is a text node, its containing element. here() is useful in relative links. These navigation elements, for example, link to the seite elements preceding or following their containing pages:
<seite>
Seiteninhalt...
<navigation xlink:type="simple" xlink:href="#xpointer(here()/../../preceding-sibling::seite[1])">Vorherige</navigation>
<navigation xlink:type="simple xlink:href="#xpointer(here()/../../following-sibling::seite[1])">Nächste</navigation>
</seite>In these elements, the function here() identifies the xlink:href attribute nodes containing the XPointer. The first two dots .. move to the parent element navigation. The second .. select its parent element seite, and the final location step selects the previous or next seite element.
origin()
The function origin() is useful when a document was loaded through an external link. It identifies the node from which the user initiates traversal, even if that node does not define the link. Consider this extended link with many roman elements, all locators with the same label:
<reihe xlink:type="extended" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Locator-Elemente -->
<roman xlink:type="locator" xlink:label="oz" xlink:href="ftp://archive.org/pub/etext/etext93/wizoz10.txt">
<titel>The Wonderful Wizard of Oz</titel>
<jahr>1900</jahr>
</roman>
<roman xlink:type="locator" xlink:label="oz" xlink:href="ftp://archive.org/pub/etext/etext93/ozland10.txt">
<titel>The Marvelous Land of Oz</titel>
<jahr>1904</jahr>
</roman>
<roman xlink:type="locator" xlink:label="oz" xlink:href="ftp://archive.org/pub/etext/etext93/wizoz10.txt">
<titel>Ozma of Oz</titel>
<jahr>1907</jahr>
</roman>
<!-- und viele Elemente mehr ... -->
<fortsetzung xlink:type="locator" xlink:label="next" xlink:href="#xpointer(origin( )/following-sibling::roman[1])" />
<naechster xlink:type="arc" xlink:from="oz" xlink:to="next" />
</reihe>The element fortsetzung uses an XPointer and the function origin() to define a locator pointing to the next roman in the series. When the user reads The Wonderful Wizard of Oz, the fortsetzung element points to The Marvelous Land of Oz. When reading The Marvelous Land of Oz, the same fortsetzung element leads to Ozma of Oz, and so on. The element naechster defines links from each roman, all bearing the label oz, to its sequel. The target resource changes from one novel to the next.
Copyright © 2005 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XML in a Nutshell” is subject to the same conditions as the bound edition: the work, including all its parts, is 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