XML technologies / XML / XML in a Nutshell / XPointer / Points

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

XPaths, shorthand pointers, and child sequences can refer only to complete nodes or node-sets. Sometimes, however, you want to point to something that is not a node, such as the third word of the third paragraph or the year in a datum attribute that looks like this: datum="01/03/1950". To make this possible, XPointer extends the XPath data model with points and ranges. A point is the position before or after a tag, a comment, a processing instruction or a character in a #PCDATA section. Points can also be positions inside comments, processing instructions, or attribute values. Points cannot be located inside an entity reference, but can occur within an entity’s replacement text. A range is a sequence of parsed character data between two points. Nodes, points, and ranges are collectively called locations; a set that can contain nodes, points, and ranges is called a location-set. In other words, a location generalizes an XPath node to include points and ranges as well as elements, Namespaces, text nodes, comments, processing instructions, and the root node.

A point is identified by its container node and a nonnegative index into that node. If the node has children, meaning it is a root or element node, points exist before and after every child. The point after one child coincides with the point before the next. If the node has no children, as with a comment, processing instruction, attribute, namespace or text node, there is a point before and after every character in the node’s string value. Again, the point after one character is identical to the point before the next.

Consider the document in the following example. It contains a roman element with seven children: three element nodes and four text nodes containing only whitespace.

 

<?xml version="1.0"?>
<?xml-stylesheet type="text/css" value="roman.css"?>
<!-- Sie können dies aus dem letzten Kapitel übernehmen -->
<roman copyright="public domain">
   <titel>The Wonderful Wizard of Oz</titel>
   <autor>L. Frank Baum</autor>
   <jahr>1900</jahr>
</roman>

Code example: A novel document

Eight points lie directly inside the roman element, numbered 0 through 7, immediately before and after each tag. The following figure shows these points.

Points inside the roman element

Figure: Points inside the roman element

Inside the text-node child of the element jahr there are five points:

Notice that points lie between text characters, not on the characters themselves. Points have no dimension. They identify a position but have no extent, not even a single character. To identify one or more characters, you must specify a range between two points.

XPointer adds two XPath functions that make it easy to select the first and last points inside a node: start-point( ) and end-point( ). For example, this XPointer identifies the first point inside the titel element, that is, between the title node and its first text-node child:

xpointer(start-point(//titel))

This XPointer identifies the point immediately before the </autor> tag:

xpointer(end-point(//autor))

If this document contained several titel and autor elements, this function would select several points.

This XPointer points immediately before the letter T in “The Wonderful Wizard of Oz”:

xpointer(start-point(//titel/text()))

This point lies immediately after the point identified by xpointer(start-point(//titel)). These are two different points even though they lie between the same characters (> and T).

To address points other than the start and end of a node, first use string-range() to construct a range starting or ending at the desired point. You can then access it using start-point() or end-point(). We will tackle this on the next page.

  

 

 

 

back to top