Hack 28 Explore XLink and XML

figs/moderate.gif figs/hack28.gif

XLink and XML Base are implemented or partially implemented by Mozilla. This hack explores these technologies, using Mozilla as a platform.

The XML Linking Language or XLink (http://www.w3.org/TR/xlink/) defines a vocabulary for creating hyperlinks to resources using XML syntax. XLink goes beyond the simple linking in HTML and XHTML by adding concrete semantics and extended links that can link to more than one resource. XLink hasn't really taken off yet, but Mozilla supports simple links in XLink, though not extended links (http://www.mozilla.org/newlayout/xml/#linking). Use of XLink is growing, if slowly?see, for example, the use of XLink in the OpenOffice specification (http://www.oasis-open.org/committees/download.php/6037/office-spec-1.0-cd-1.pdf).

XML Base (http://www.w3.org/TR/xmlbase/) consists of a single XML attribute, xml:base, that acts like the base element from HTML and XHTML; i.e., it explicitly sets the base URI for a document. A base URI is often understood implicitly by a program such as a Web browser by the location of a resource, such as a location on the web or the location of a file in a directory or file structure. In HTML or XHTML, this base URI could be set directly with the base element, as shown in this fragment of XHTML markup (note bold):

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>

 <title>Links</title>

 <base href="http://www.xml.com"/>

</head>

<body>

 ...

You set a base URI explicitly using xml:base, as shown in the example document base.xml (Example 2-21). It is also displayed in Mozilla Firefox in Figure 2-27.

Example 2-21. base.xml
<?xml version="1.0" encoding="UTF-8"?>



<?xml-stylesheet href="base.css" type="text/css"?>



   



<links xml:base="http://www.xml.com/"

  xmlns:xlink="http://www.w3.org/1999/xlink">

<heading>Resources on XML.com.</heading>

<block>Start here:

 <link xlink:type="simple" xlink:href="index.csp">Home</link></block>

<block>Topics:

 <link xlink:type="simple" xlink:href="/programming/">Programming  

  articles</link> :

 <link xlink:type="simple" xlink:href="/schemas/">Schema articles </link> :

 <link xlink:type="simple" xlink:href="/style/">Style articles</link>

</block>

<block xml:base="/images/">Logo for XML.com:

 <link xlink:type="simple" xlink:href="logo_tagline.jpg">logo</link>

</block>

</links>

Figure 2-27. base.xml in Firefox
figs/xmlh_0227.gif


2.19.1 XML Base

The base URI is set to http://www.xml.com with xml:base on line 4. This setting is inherited by the children of links. The XLink to index.csp on line 8, therefore, is able to resolve to http://www.xml.com/index.csp because index.csp is relative to http://www.xml.com.

The xml:base attribute on line 14 adds the images directory to the base URI so that it becomes http://www.xml.com/images/, changing the earlier setting on line 4. This is in effect only for the children of the block where it is set. Hence, the XLink on line 15 can find the JPEG image of the XML.com logo with only the filename logo_tagline.jpg.

2.19.2 XLink

The namespace for XLink is declared on line 5 and is associated with the xlink prefix (xmlns:xlink="http://www.w3.org/1999/xlink"). The xlink prefix is customary, and Mozilla won't work without it.

The namespace declaration allows the link elements on lines 8, 10, 11, 12, and 15 to use the XLink attributes xlink:type and xlink:href. The value of xlink:type states the type of XLink (simple in Example 2-21). Other possible values are extended, locator, arc, resource, title, or none, but because Mozilla supports only simple links, I only used simple. The xlink:href attribute contains a URI that identifies the resource that the link can traverse, similar to the href attribute on the a element in HTML or XHTML.

When the mouse pointer hovers over the XLinks in base.xml in the browser, the background changes according to the CSS styling defined in base.css, which is referenced by the XML stylesheet PI on line 2.

2.19.3 Other XLink Functionality

Three other XLink attributes merit some discussion: xlink:actuate, xlink:show, and xlink:label. Unlike xlink:type and xlink:href, these attributes have not been implemented in Mozilla. In fact, they apparently have not been implemented in software that can be easily demonstrated on the Web, so unfortunately I can't show you any practical examples of them. Nevertheless, it would be good for you to at least get familiar with the intended functionality of these XLink attributes, should they ever reach the masses.

The xlink:actuate attribute indicates when or how a link is to be traversed. The value of xlink:actuate can be onRequest, onLoad, none, or other. onRequest means the link is actuated when it is clicked or triggered in some way. onLoad means that the link is actuated when the page or resource is loaded by an application such as a browser. A value of none essentially turns off the behavior, and a value of other allows for application-specific behavior.

The xlink:show attribute can have these values and behaviors:


new

Load the ending resource in a new window.


replace

Load the resource in the same window, frame, pane, or what have you.


embed

Load the resource at the place where the link is actuated.


other

Application-specific behavior.


none

Essentially "don't do anything," though behavior is not specified by the spec.

The xlink:label attribute contains a label that identifies an element holding a link to a resource. Then the xlink:from and xlink:to attributes can contain values that match values in xlink:label attributes (see Example 2-23).

2.19.3.1 Extended links

Extended links (http://www.w3.org/TR/xlink/#extended-link) are hard to explain. There is no simple way to demonstrate them because browsers don't support them. The fact that they are complicated is perhaps one reason why they are not widely implemented. But they are worthy of a few passing remarks, as they hold potential.

We are used to simple links that go in one direction from one resource to another. Extended links can point to more than one resource at a time. This is done by using the xlink:type="locator" attribute/value pair on an element, in combination with xlink:href. You can provide some advisory text with xlink:title. xlink:role can contain a URI value that annotates the link, but doesn't actually link to a resource. An extended link using all these attributes would look something like Example 2-22.

Example 2-22. Extended link example
<link xml:base="http://www.ri.gov/living/" xlink:type="extended">

 <country>United States</country>

 <state>Rhode Island</state>

 <city xlink:type="locator" xlink:title="Bristol"

   xlink:href="/towns/town.php?town=Bristol">Bristol</city>

 <city xlink:type="locator" xlink:title="Newport"

   xlink:href="/towns/town.php?town=Newport">Newport</city>

</link>

2.19.3.2 XLink linkbases

An XLink link database or linkbase (http://www.w3.org/TR/xlink/#xlg) is an XML document that contains one or more inbound and third-party links. Linkbases are a little hard to grasp, especially because, once again, you can't really demonstrate them.

An arc provides information about how to traverse a pair of resources, including direction and perhaps some information about how an application may act. An arc is said to be outbound if it starts at a local resource and ends at a remote resource. An arc is inbound if it starts at a remote resource and lands in a local one. An arc is a third-party arc if it neither starts nor ends at a local resource. In other words, a third-party arc is one for which you do not or cannot create the link at either the starting or ending resource.

The following XML fragment (Example 2-23) indicates that when exterior.xml is loaded, the linkbase.xml document should be loaded. On the load element, the xlink:actuate attribute specifies load behavior, and using the labels in the link and linkbase elements (defined by xlink:label), load also establishes a traversal from ext to lb using the xlink:to and xlink:from attributes.

Example 2-23. Linkbase example
<block>

  <link xlink:label="ext" xlink:href="exterior.xml" />

  <linkbase xlink:label="linkbase" xlink:href="linkbase.xml" />

  <load xlink:from="ext" xlink:to="lb" xlink:actuate="onLoad" />

</block>

2.19.4 See Also

  • For more insight into XLink: http://www.xml.com/pub/a/2002/09/25/linkoffering.html