Hyperlinks have long been an important part of HTML. You create them with the HTML <A> element, like this: <A HREF = "http://www.starpowdermovies.com/reviews.html#blandings"> Mr. Blandings Builds His Dream House <A> The hyperlink appears in an HTML document either as text, typically underlined and colored, or as a clickable image. When clicked, the hyperlink can perform a variety of actions: navigate to a new document or a specific location in that document, open the new document in an existing frame, even open a new window if you use the TARGET attribute, or execute JavaScript if you use the javascript: URL. Here are the official HTML attributes for the <A> element:
There's a lot of functionality here, but it all relies on the <A> element and the simplest type of hyperlinkone that waits to be clicked and then navigates to a new document or document location. Relationships between documents can be far more complex than that. For example, you might want a link to point to 10 mirror sites of a main site and let the browser select the one that's closest . Or, you might want to link to an entire set of documents, complete with subsets , that the browser should search for the resource you want. Or, you might want to set up a series of paths that lets the user navigate through a set of documents in various directions but not in others, and so on. XLinks let you perform all these kinds of linking. XLinks are not restricted to any one element such as the <A> element, which is to say that XLinks may not always appear in your documents in the traditional blue, underlined text (although, of course, they could if you wanted them that way). Being able to make any element into an XLink is great because you can create elements that are always links to other resources. Users might even come to expect that if they come across anything formatted with, say, the <CITATION> element, that element will be linked to the cited material. You create an XLink with attributes, not with specific elements. Specifically, you use the xlink:type attribute to describe the kind of link and to create an XLink, setting it to one of the allowable types of XLinks: simple , extended , locator , arc , resource , title , or none . Here are current XLink attributes:
Using the XLink attributes, you can make an XLink mockup in browsers such as Internet Explorer. As an example, I'll create a mockup of a simple XLink here. Internet Explorer supports the onClick attribute if you use it with an XML element, and I'll add some JavaScript to that attribute that will make the browser navigate to a new URI, using Internet Explorer's location object: Listing ch15_01.xml<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch15_02.css"?> <DOCUMENT> <P> Want to check out <LINK xml:type = "simple" href="http://www.w3c.org" onClick="location.href='../www.w3c.org/default.htm'">W3C</LINK>? </P> </DOCUMENT> I'm even supplying a stylesheet, xlink_example.css, to make this XLink appear in the standard blue, underlined font. In fact, you can even make Internet Explorer's cursor change to a hand, as it does for HTML hyperlinks, with the cursor CSS property: Listing ch15_02.cssLINK {color: #0000FF; text-decoration: underline; cursor: hand} The result appears in Figure 15-1, where the simple XLink functions much like an HTML hyperlink (which, of course, limits the whole concept of XLinks badly ). You can click this link to make Internet Explorer navigate to a new document. Figure 15-1. A mockup of a simple XLink in Internet Explorer.
So when do you use which XLink attributes? It all depends on the type of link you're creating, as given by the xlink:type attribute. Depending on the link type, some of these attributes are required and some are optional. You can find the complete rules in Table 15-1, where the rows correspond to the various XLink attributes and the columns correspond to the various XLink types. Table 15-1. Required and Optional Attributes by xlink:type
Note that each of these attributes uses the xlink namespace; this namespace always has the value "http://www.w3.org/1999/xlink" , as we saw in the earlier simple link 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::*[position( )= 126]/child::*[position()=first()])"> Mr. Blandings Builds His Dream House </MOVIE_REVIEW> XML Base (XBase)Another W3C specification bears discussion while talking about linking and the relationship between documents: the XBase specification. As of this writing, the XBase specification is in recommendation form, released on June 27, 2001. You can find the current version of this document at www.w3.org/TR/xmlbase. This specification lets you provide a base URI for XML documents, just like the HTML <BASE> element. In fact, the HTML <BASE> element is exactly the reason XBase exists: The W3C is committed to giving XLink all the power that HTML 4.0 linking has and then building on that. One of the aspects of linking in HTML 4.0 is the <BASE> element. Here's how it works. You can use the xml:base element in an XML document to set the document's base URI. The other URIs in the document are then resolved using that value as a base. Note that xml:base uses the xml namespace, not the xlink namespace; the xml namespace is predefined in XML (that is, you don't have to define it to be able to use it in most XML parsersin other words, almost any element can be linked) as "http://www.w3.org/XML/1998/namespace" . Here's an example: <MOVIE_REVIEW xmlns:xlink = "http://www.w3.org/1999/xlink" xml:base="http://www.starpowder.com" xlink:type = "simple" xlink:show = "new" xlink:href="reviews.xml"> Mr. Blandings Builds His Dream House </MOVIE_REVIEW> Using the value assigned to the xml:base attribute, the URI in this example's xlink:href attribute, "reviews.xml" , is resolved to the full URI http://www.starpowder.com/reviews.xml . In this way, you can use xml:base to provide a base URI for a document (or even a specific element). |