All About XLinks

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:

  • ACCESSKEY Assigns a keyboard shortcut to the hyperlink.

  • CHARSET Specifies the character encoding of the target of the hyperlink. You set this to an RFC 2045 language character set string (the default value is ISO-8859-1).

  • CLASS Gives the style class of the element.

  • COORDS Sets the coordinate values (in pixels) appropriate to the accompanying SHAPE attribute to define a region of an image for image maps.

  • DIR Gives the direction of directionally neutral text. You set it to LTR for left-to-right text or RTL for right-to-left text.

  • HREF Holds the target URL of the hyperlink. Either this attribute or the NAME attribute must be used.

  • HREFLANG Specifies the base language of the target indicated in the HREF attribute. Set this to RFC 1766 values.

  • ID A unique identifier for the tag.

  • LANG Base language used for the tag.

  • METHODS Can specify methods to be used in accessing the target.

  • NAME Set to an anchor name, the name you want to use to refer to the enclosed items (such as text, images, and so on). Either this attribute or the HREF attribute must be used.

  • REL Specifies the relationship described by the hyperlink.

  • REV Essentially is the same as the REL attribute, but the syntax works in the reverse direction.

  • SHAPE Defines the type of region to be defined for mapping in the HTML AREA tag.

  • STYLE Inline style indicating how to render the element.

  • TABINDEX Sets the tab sequence of hyperlinks in the page.

  • TARGET Indicates the named frame for the HREF hyperlink to jump to. Set this to the name of a frame.

  • TITLE Holds title information for the element.

  • TYPE Specifies the MIME type of the target given in the HREF attribute.

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:

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

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

  • xlink:from This attribute defines starting resources.

  • xlink:href This is the locator attribute. It supplies the data that allows an XLink application to find a remote resource.

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

  • xlink:role You use the role attribute to describe the function of a link's remote resource in a machine-readable fashion and, in the case of extended-type elements, to serve as a resource category label for traversal rules in arc-type elements. For example, search engines can read this attribute.

  • xlink:show You use this attribute to indicate how you want to display the linked-to resource. XLink applications must recognize the following values: new (opens a new display space), replace ( replaces the currently displayed data), embed (embeds the new resource in the current one), other ( leaves the show function up to the displaying software), or none (doesn't show the resource).

  • xlink:title You use the title attribute to describe the function of a link's remote resource, for people to understand.

  • xlink:to This attribute defines target or ending resources.

  • xlink:type This attribute sets the type of the Xlink. This can be simple , extended , locator , arc , resource , title , or none . We'll see what these types mean in this chapter.

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.css
 LINK {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.

graphics/15fig01.gif

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
  simple extended locator arc resourc 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

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( graphics/ccc.gif )= 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).



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net