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:

  • xlink:arcrole holds the link's role in an arc (arcs can contain multiple resources and traversal paths), which may be different for different arcs. More on this later in this chapter.

  • xlink:actuate determines when traversal operations occur. You can set this attribute to the official values of onLoad , onRequest , other , none , or other values as required by the software you're using.

  • xlink:from defines starting resources.

  • xlink:href supplies the data that allows an XLink application to find a remote resource.

  • xlink:label holds a human-readable label for the link.

  • xlink:role describes the role, or function, of a link's remote resource in a machine-readable fashion. Search engines should be able to read this attribute.

  • xlink:show indicates how you want to display the linked-to resource. XLink applications must recognize the following values: new ( open a new display space, such as a window), replace (replace the currently displayed data), embed (embed the new resource in the current one), other (leaving the show function up to the displaying software), or none (don't show the resource).

  • xlink:title describes the function of a link's remote resource for people to understand.

  • xlink:to defines a target of the link.

  • xlink:type sets the type of the XLink; can be one of simple , extended , locator , arc , resource , title , or none . More on these later in this chapter.

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

 

SIMPLE

EXTENDED

LOCATOR

ARC

RESOURCE

TITLE

actuate

Optional

Omitted

Omitted

Optional

Omitted

Omitted

arcrole

Optional

Omitted

Omitted

Optional

Omitted

Omitted

from

Omitted

Omitted

Omitted

Optional

Omitted

Omitted

href

Optional

Omitted

Required

Omitted

Omitted

Omitted

label

Omitted

Omitted

Optional

Omitted

Optional

Omitted

role

Optional

Optional

Optional

Optional

Optional

Omitted

show

Optional

Omitted

Omitted

Optional

Omitted

Omitted

title

Optional

Optional

Optional

Optional

Optional

Omitted

to

Omitted

Omitted

Omitted

Optional

Omitted

Omitted

type

Required

Required

Required

Required

Required

Required

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:

  • arc makes an arc with multiple resources and traversal paths.

  • extended makes an extended link, which can involve multiple simple links.

  • locator makes a locator link that points to a resource. This kind of link holds a link to another link.

  • resource makes a resource link, which indicates a specific resource.

  • simple makes a simple link, which is like a standard hyperlink.

  • title makes a title link; such elements are useful, for example, for cases where human-readable label information needs further element markup, or where multiple titles are necessary for internationalization purposes.

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:

  • embed causes the software to embed the linked-to resource in the current resource, for example, by showing an image in a Web page.

  • new causes the software to open a new display area, such as a new window, to display the new resource.

  • none causes the software to not show the resource. Although the resource is not specifically displayed, its data is still accessible to implementation-specific software with this kind of link.

  • other indicates some other setting that those that are predefined.

  • replace causes the software to replace the current resource, in the same window if there is one.

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:

  • onRequest should be traversed only on the user's request.

  • onLoad should be traversed when the document or resource is loaded.

  • other means a custom preference as defined by the application.

  • none means no actuation (or that the behavior of the application is not constrained by the markup).

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>

Категории