In this chapter, we're going to take a look at using XPath with XPointer, XLink, and XQuery. This chapter is also going to help us make the transition between XPath 1.0 and XPath 2.0 because XPointer and XLink both use XPath 1.0, whereas XQuery is integral to XPath 2.0. We're going to see that the demands of data access are growing, and that'll give us a natural introduction to XPath 2.0 in the next chapter.
XLink and XPointer are two XML- related specifications that let you use XPath 1.0 to locate data in XML documents, and as such, we'll discuss them here. Then, as a transition into XPath 2.0which starts in the next chapterwe'll take a look at one of the newer ways of accessing data in XML documents using XPathXQuery 1.0. As we're going to see, XQuery is far more powerful than XLink or XPointer, which reflects the fact that handling data is becoming much more sophisticated. In fact, XLink and XPointer can't even be compared directly to XQuery; they're quite simple compared to XQuery's sophistication. And as we're going to see, XQuery 1.0 is actually an extension of XPath 2.0, not just an allied specification (which is true of XPath 1.0 and XLink/XPointer).
Over time, being able to address the particular data you're interested in in an XML document has become more and more important. XLink and XPointer give you a lot of capability, but the need is so great that even more power is needed, and we're seeing the results in XQuery in this chapter. As we're going to see starting in Chapter 7, "What's New in XPath 2.0," XPath 2.0 and XQuery are integrally connectedin fact, the XQuery working group coleads the XPath 2.0 effort and is a coauthor of the XPath 2.0 specifications. XSLT 2.0 is also integrally connected to XPath 2.0, as we're going to see.
For us in this chapter, the idea of data access begins with simple linking. You already know that HTML supports hyperlinksfor example, if you had a review of the James Bond movie Goldfinger , you could create a hyperlink to that review like this:
So how would this look as an XLink that you could use in an XML document? You can create an XLink with the attribute xlink:type like this, where we're creating a simple XLink:
<review xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href="http://www.XPathCorp.com/reviews.xml"> Goldfinger </review>
This is a simple XLink, much like an HTML hyperlink. Here, we're setting the xlink:type attribute to "simple" and the xlink:show attribute to "new", which means XLink-aware software should open the linked-to document in a new window. The xlink:href attribute holds the URI of the linked-to document. That's what a simple XLink looks like, but XLinks can become pretty involved, as we'll see in this chapter. Besides basic unidirectional links like the simple link in this example, you can also create bidirectional links, links between multiple documents and document sets, and much more, including storing your links in link databases called linkbases .
XLinks let you link to a particular document, but you often need to be more precise than that. XPointers let you point to specific locations inside a documentwithout having to modify that document by embedding special tags or markers.
To point to a specific location in a document, the XPointer specification builds on XPath. For example, you can use XPath expressions like /child::*/child::* in XPointers. In fact, XPointer 1.0 even extends XPath in ways that we'll see in this chapter.
You can add an XPointer to a document's URI to specify a specific location in a document. For example, you can append # (following the HTML usage for URLs that specify link targets) and then xpointer() , placing the XPath expression you want to use in the parentheses. Here's an example:
<review xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.XPathCorp.com/reviews.xml#xpointer(/child::*[last()])"> Goldfinger </review>
That's what XLink and XPointer look like in overviewit's time to dig into the details.