XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
Another use for XPath is with the XPointer and XLink specifications. XLinks specify how one document links to another document, and XPointers specify locations inside a document. We'll see both of these in Chapter 6, but it's worth getting an overview now. The XLink specification is a W3C recommendation, released on June 27, 2001. You can find the most current version of this recommendation at www.w3.org/TR/xlink. You use XLinks to link one document to another. Here's an example to give you an idea what an XLink looks like; unlike HTML hyperlinks , any element can be a link in XML. You specify that an element is a link with the attribute xlink:type like this, where we're creating a simple XLink:
<MOVIE_REVIEW xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href="http://www.starpowdermovies.com/reviews.xml"> Mr. Blandings Builds His Dream House </MOVIE_REVIEW> In this case, we're creating a simple XLink, which is much like an HTML hyperlink, by setting the xlink:type attribute to "simple" . We're also setting the xlink:show attribute to "new" , which means that XLink-aware software should open the linked-to document in a new window or other display context, and setting the xlink:href attribute to the URI of the new document (which can be quite general and need not be in the URL form we've used here). 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 the XPath specification. XPointers are in the W3C Working Draft stage, and you can learn more about them at www.w3.org/TR/xptr-framework/. How do you add an XPointer to a document's URI to specify a specific location in a document? 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:
<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::*[126])"> Mr. Blandings Builds His Dream House </MOVIE_REVIEW> As you can see, XPath is central to a number of XML specifications. We'll see how XPath is used in these specifications in more detail, and with examples, later in the book. |