XPointer Location Set Functions

Two XPointer functions return location sets:

  • here() Returns a location set with one location, the current location

  • origin() The same as here(), except that this function is used with out-of-link links

The here() function refers to the current element. This is useful because XPointers are usually stored in text nodes or attribute values, and you might want to refer to the current element (not just the current node). For example, you might want to refer to the second previous < NAME > sibling element of the element that contains an Xpointer; you can use an expression like this to do so: here()/ preceding -sibling::NAME[position() = 2] .

The origin() function is much like the here() function, but you use it with out-of-line links. It refers to the original element, which may be in another document, from which the current link was activated. This can be very helpful if the link itself is in a linkbase and needs to refer not to the element the link is in, but to the original element from which the link is activated.

You can use the abbreviated XPath syntax in XPointers as well. I'll take a look at a few examples using our planetary XML document, ch15_06.xml, as the document we'll be navigating. Note that, as with XPath, you can use the [] operator; here, it extracts a particular location from a location set:

  • PLANET Returns the <PLANET> element children of the context node.

  • * Returns all element children of the context node.

  • text() Returns all text node children of the context node.

  • @UNITS Returns the UNITS attribute of the context node.

  • @* Returns all the attributes of the context node.

  • PLANET[3] Returns the third <PLANET> child of the context node.

  • PLANET[first()] Returns the first <PLANET> child of the context node.

  • */PLANET Returns all <PLANET> grandchildren of the context node.

  • /PLANETS/PLANET[3]/NAME[2] Returns the second <NAME> element of the third <PLANET> element of the <PLANETS> element.

  • //PLANET Returns all the <PLANET> descendants of the document root.

  • PLANETS//PLANET Returns the <PLANET> element descendants of the <PLANETS> element children of the context node.

  • //PLANET/NAME Returns all the <NAME> elements that have a <PLANET> parent.

  • . Returns the context node itself.

  • .//PLANET Returns the <PLANET> element descendants of the context node.

  • .. Returns the parent of the context node.

  • ../@UNITS Returns the UNITS attribute of the parent of the context node.

  • PLANET[NAME] Returns the <PLANET> children of the context node that have <NAME> children.

  • PLANET[NAME="Venus"] Returns the <PLANET> children of the context node that have <NAME> children with text equal to "Venus" .

  • PLANET[@UNITS = "days"] Returns all <PLANET> children of the context node that have a UNITS attribute with the value "days" .

  • PLANET[6][@UNITS = "days"] Returns the sixth <PLANET> child of the context node, only if that child has a UNITS attribute with the value "days" . Can also be written as PLANET[@UNITS = "days"][6] .

  • PLANET[@COLOR and @UNITS] Returns all the <PLANET> children of the context node that have both a COLOR attribute and a UNITS attribute.

In XPath, you can locate data only at the node level. That's fine when you're working with software that handles XML data in terms of nodes, such as XSL transformations, but it's not good enough for all purposes. For example, a user working with a displayed XML document might be able to click the mouse at a particular point or even select a range of XML content. (Note that such ranges might not start and end on node boundaries at allthey might contain parts of various trees and subtrees.) To give you finer control over XML data, you can work with points and ranges in XPointer.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net