XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
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. Here's what W3C says in the W3C working draft: "This specification defines the XML Linking Language (XLink), which allows elements to be inserted into XML documents in order to create and describe links between resources. It uses XML syntax to create structures that can describe links similar to the simple unidirectional hyperlinks of today's HTML, as well as more sophisticated links." XLinks are not restricted to any one element like the <A> elementyou can make any XML element into an XLink. XLinks can be quite complexfor example, you might want a link to point to ten mirror sites of a main site and let the browser select the one that's closest , or you can link to an entire set of documents that the browser should search, or you can set up a series of paths that lets the user navigate between a set of documents in various directions but not in others, and so on. As mentioned, you create an XLink with attributes, not with specific elements. In particular, you use the xlink:type attribute to create an XLink, setting it to one of the allowable types of XLinks: simple , extended , locator , arc , resource , title , or none . Here are the current XLink attributes:
So what does an XLink at work look like? There isn't much software designed to display XLinks, not even simple XLinks, but we can create a mockup that will work in Internet Explorer. In fact, Internet Explorer even supports the onClick attribute if you use it with an XML element, so we can use a little JavaScript that will make the browser navigate to a new URI when the XLink is clicked. You can see how this looks in ch06_01.xml (Listing 6.1). Listing 6.1 A Simple XLink ( ch06_01.xml )
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch06_02.css"?> <document> Want to read my review of <review xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href="http://www.w3c.org" onClick="location.href='../www.w3c.org/default.htm'"> Goldfinger </review>? </document> You can even style this XLink to make it look something like a traditional hyperlink using cascading style sheets (CSS). The style sheet ch06_02.css , shown in Listing 6.2, makes this XLink appear in a blue, underlined font, and makes Internet Explorer's cursor change to a hand as the mouse passes over the XLink. Listing 6.2 Styling a Simple XLink ( ch06_02.css )
review {color: #0000FF; text-decoration: underline; cursor: hand} The result appears in Figure 6.1, where the simple XLink functions much like an HTML hyperlink. You can even click this link to make Internet Explorer navigate to a new document. Figure 6.1. A mockup of a simple XLink.
There is some software around that will let you work with XLink directly, such as W3C's test browser, Amaya, which you can get for free at http://www.w3.org/Amaya/. You can see our working simple XLink in Amaya in Figure 6.2. Figure 6.2. A simple XLink in Amaya.
Using XLink Attributes
To create an XLink, you use XLink attributes. So which attributes do you need to create what type of XLink? It all depends on the type of link you're creating, as given by the xlink:type attribute. Depending on link type, some of these attributes are required, and some are optional. You can find the complete rules in Table 6.1, where the rows correspond to the various XLink attributes, and the columns to the various XLink types. Table 6.1. XLink Attributes by xlink:type
Each of the attributes in Table 6.1 uses the xlink namespace; this namespace always uses the value "http://www.w3.org/1999/xlink", as we saw in our earlier simple link example:
<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> Because you use these attributes to create XLinks, we'll go over a few of them now. Using the xlink:type Attribute
The xlink:type attribute defines the type of XLink you want to create. You can set this attribute to one of these possible values:
Using the xlink:show Attribute
The xlink:show attribute specifies how you want the linked-to resource displayed when the link is traversed. The xlink:show attribute has these predefined values:
For example, the default behavior of HTML links is to navigate to a linked-to document, replacing the current document with the new one. You can change that behavior by assigning xlink:show a value of new , making the software open a new window for the linked-to document:
<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> Using the xlink:actuate Attribute
The xlink:actuate attribute indicates when a link should be traversed. The xlink:actuate attribute has these predefined values:
In addition, you can set your own values for xlink:actuate . If you set the value of xlink:actuate to onLoad , the link is traversed when the resource containing it is loaded. For example, here's how you'd activate a link only when the user requests it to be activated:
<review xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://www.XPathCorp.com/reviews/goldfinger.xml" xlink:actuate="onRequest"> </review> Using the xlink:href Attribute
This attribute contains the data which allows an XLink application to find a remote resource, usually a URI, as in this example:
<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> |