Unlike XPath, XPointer enables you to select information that is contained within more than one set of nodes. For example, if you are presenting a document that contains several sections of text, where each section is contained within a paragraph element, it's possible that a user may highlight text that begins in the middle of one paragraph element and ends in the middle of another paragraph element. XPath allows you to define only complete sets of nodes, and because this example includes parts of nodes, XPath can't be used to represent the section. XPointer has extensions to XPath that allow you to get a reference to the text that was selected. Any XPath expression that returns a node-set may be used with XPointer. Thus, the following XPointer expression will select all the order elements that are grandchildren of the root element (in this case, the two order elements in our example XML document):
In addition to defining nodes, XPointer also defines points and ranges. The combination of nodes, points, and ranges creates locations. A point is a position in an XML document. A point location is the combination of a node, called the container node, and an index. When the container node of a point is a root node or an element node, the point is called a node point. The index must be equal to or greater than 0 and less than or equal to the number of children nodes of the container node. The index zero represents the point before any child nodes, and a non-zero index n represents the point directly after the nth child node.
If the container node is not a root node or an element node, the point is called a character point. The index must be either zero or less than or equal to the number of characters associated with the node. The index zero represents the point immediately before the first character. A non-zero index represents a point immediately after the nth character.
A range represents all the XML structure and content between a start point and an end point. The start and end points must be in the same document, and the start point must occur before the end point in document order. The start and end points can be located in the middle of a node. If the start and end points are at the same point, it is called a collapsed range. If the container node of the start point is a node other than an element node, a text node, or a root node, the end point must be in the same container node. The string value of a range is all of the text nodes within the range.
We can now use the XPointer to define a range as follows:
Xpointer (/child::customer [attribute::customerID= c1 to /child::customer [attribute::customerID= c2]
This XPointer selects a range starting at the beginning of the child customer element that has an attribute with a value of c1 and ending at the end of the child customer element that has an attribute with a value of c2. For our example XML fragment, the two customer child nodes are selected.updated