You can create ranges with two points, a start point and an end point, as long as they are in the same document and the start point is not after the end point. (If the start point and the end point are the same, the range is said to be collapsed . ) A range is all of the XML structure between those two points. A range doesn't have to be a neat subsection of a document; it can extend from one subtree to another in the document, for example. All you need are a valid start point and a valid end point in the same document. Range FunctionsThe XPointer specification adds a number of functions to those available in XPath to handle ranges:
String RangesThe XPointer specification also includes a function for basic string matching, string-range() . This function returns a location set with one range for every nonoverlapping match to the search string. The match operation is case sensitive. You can also specify optional index and length arguments to specify how many characters after the match the range should start and how many characters should be in the range. Here's how you use string-range() in general: string-range( location_set, string, [ index, [ length ]])
For example, this expression returns a location set containing ranges covering all matches to the word Saturn : string-range(/, "Saturn") To extract a specific match from the location set returned, you use the [] operator. For example, this expression returns a range covering the second occurrence of Saturn in the document: string-range(/, "Saturn")[2] This expression returns a range covering the third occurrence of the word Jupiter in the < NAME > element of the sixth <PLANET> element in a document: string-range(//PLANET[6]/NAME, "Jupiter")[3] You can also specify the range you want to return using the index (which starts with a value of 1 ) and length arguments. For example, this expression returns a range covering the letters er in the third occurrence of the word Jupiter in the <NAME> element of the sixth <PLANET> element: string-range(//PLANET[6]/NAME, "Jupiter", 6, 2)[3] If you want to locate a specific point, you can create a collapsed ( zero-length ) range like this: string-range(//PLANET[6]/NAME, "Jupiter", 6, 0)[3] Another way to get a specific point is to use the start-point() function, which returns the start point of a range: start-point(string-range(//PLANET[6]/NAME, "Jupiter", 6, 2)[3]) Here's an expression that locates the second @ character in any text node in the document and the five characters following it: string-range(/, "@", 1, 6)[2] |