All About XPointers

At the beginning of this chapter, I took a look at a link that used an XPointer to locate a specific element in a document. That example looked like this:

 <MOVIE_REVIEW xmlns:xlink = "http://www.w3.org/1999/xlink"      xlink:type = "simple"     xlink:show = "new"  xlink:href="http://www.starpowdermovies.com/reviews.xml#xpointer(/child::*[position( graphics/ccc.gif )=126]/child::*[position()=first()])">  Mr. Blandings Builds His Dream House </MOVIE_REVIEW> 

You can see the XPointer part here: xpointer(/child::* [position()=126]/child::*[position()=first()]) . This XPointer is appended to the URI I'm using here, following a # character.

You might notice that this XPointer expression looks a lot like the XPath expressions we used in Chapter 13, and with good reason: XPointers are built on XPaths, with certain additions that I'll note here.

Because XPointers are built on XPaths, they have all the power of XPaths. Among other things, that means that you can use an XPointer made up of location steps that target an individual location in a document, without having to add any markup to that document. You can also use the id() function to target specific elements if you do want to add ID attributes to those elements.

However, because XPointers extend XPaths, there are some differences. The biggest difference is that because users can select parts of documents using the mouse, if they prefer, XPointers allow you to select points and ranges in addition to the normal XPath nodes. A point is just what it sounds like: a specific location in a document. A range is made up of all the XML between two points, which can include part of elements and text strings.

To support points and ranges, XPointer extends the idea of nodes into locations. Every location is an XPath node, a point, or a range. Node sets therefore become location sets in the XPointer specification. XPointer specifically extends the defined node types to include points and ranges so that node tests can return any of those types.

How do you create an XPointer? Like XPaths, XPointers are made up of location paths that are divided into location steps, separated by the / character. A location step is made up of an axis, a node test, and zero or more predicates, like this: axis :: node_test [ predicate ] . For example, in the expression child::PLANET[position() = 5] , child is the name of the axis, PLANET is the node test, and [position() = 5] is a predicate.

You can create location paths with one or more location steps, as in /descendant::PLANET/child::NAME , which selects all the <NAME> elements that have an <PLANET> parent.

XPointers augment what's available with XPaths, so I'm going to take a look these three partsaxes, node tests, and predicatesfor XPointers now.



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