XML

Linking with XLink

The whole point of XPointer (no pun intended) is to provide a means of referencing portions of XML documents for the purpose of creating powerful XML links. XLink ultimately makes links possible through linking elements, which are elements that describe the characteristics of links. The anchor element in HTML is a good example of a linking element. Although linking elements form the basis of XLink, there are no predefined linking elements in the XLink language. Although it may seem strange at first, you won't find any standard element in the XLink language. The reason is because XML is all about the creation of custom tags (elements), which precludes the use of a fixed linking element in XLink. In other words, you are encouraged to define your own linking elements specific to a particular XML-based language, as opposed to being locked into a fixed element, such as HTML's anchor element (a).

Even though HTML's anchor element is somewhat limiting in the context of XML, there still must be some kind of mechanism in XLink that identifies links. This mechanism comes in the form of standard linking attributes that can be associated with any element. There are several of these attributes, which you learn about in the next section. For now, just understand that the presence of XLink attributes is sufficient to identify an element as a linking element.

A linking element uses a construct called a locator to connect resources involved in a link. In both HTML and XML, the HRef attribute serves as the locator for links. Although HTML and XML share this attribute, links in XML are described in much more detail than their HTML counterparts. Perhaps the most important difference is the fact that XML links completely describe the resources involved, even if a target resource is just a document fragment. In HTML, it is necessary to place an anchor element in a target fragment resource and identify it using the id attribute. This is not the case in XML because XLink provides the necessary ingredients to fully describe the resources involved in a link.

There are two types of linking elements supported in XLink:

  • Inline links

  • Out-of-line links

An inline link is a link whose content serves as one of the link's participating resources. Typically, an inline link has a linking element that contains content that serves as the source for the link. HTML anchor links are good examples of inline links because an anchor link contains text or an image that acts as the source for the link. Due to HTML's use of inline links, you may be curious as to how a link could work any other way. Out-of-line links extend the concept of linking in XML by allowing you to create links that are independent of the linked resources.

An out-of-line link is a link whose content doesn't serve as one of the link's participating resources. This means that out-of-line links are independent of their participating resources and therefore serve a very different purpose than inline links. Out-of-line links are useful for linking information in documents that you can't modify for one reason or another. For example, if you wanted to create a link between two resources that reside on other web sites, you'd use an out-of-line link. Such a link is possible because out-of-line links are geared toward opening up interesting new opportunities for how links are used to connect documents. More specifically, it would be possible to create link databases that describe relationships between information spread across the Web.

Out-of-line links partially form the concept of extended links in XML. Extended links are basically any links that extend the linking functionality of HTML. Out-of-line links obviously are considered extended links because HTML doesn't support any type of out-of-line linking mechanism. Extended links also support the association of more than one target resource with a given link. With extended links, you could build a table of contents for a web site that consists solely of extended links that point to the various pages in the site. If the links were gathered in a single document separate from the table of contents page itself, they would also be considered out-of-line links.

Understanding XLink Attributes

Hopefully I've sold you on the fact that XLink offers some interesting opportunities for creating XML links that are impossible in HTML. Now it's time to look at exactly how such interesting linking is made possible by XLink. Earlier I mentioned that XLink defines standard attributes that are used to establish linked elements in XML documents. Following are the XLink attributes that can be used to create linked elements:

  • type A string that specifies the type of link

  • href A locator that addresses a target resource using a URI

  • from A string that identifies the resource being linked from when describing an arc

  • to A string that identifies the resource being linked to when describing an arc

  • show A string that determines how a target resource is to be revealed to the user

  • actuate A string that determines how a link is initiated

  • role An application-specific string used to describe the function of a link's content

  • title A string that serves as a name for a link

The type attribute determines the type of a link and can have one of the following values: simple, extended, locator, resource, arc, or group. The href attribute is one with which you are already familiar, based on its use in HTML. The from and to attributes are used by arcs, which describe the traversal behavior of links. More specifically, an arc defines where a two-way link comes from and where it goes. Arcs could be used to establish web rings, where web pages are linked from one to the next using the from and to attributes to traverse the ring.

The show attribute determines how a target resource for a link is revealed to the user. There are three main values for the show attribute:

  • replace The target resource replaces the current document (default value).

  • new The target resource is shown in a new window.

  • embed The target resource is inserted into the current document in place of the link.

The functionality of the show attribute follows that of HTML anchor links until you get to the last possible value, parsed. If you set the show attribute to parsed, the link will be replaced by the target resource. This type of link allows you to divide a document into subdocuments and then link them together to form a compound document, which can help improve the organization of data.

The actuate attribute determines how a link is initiated and is typically set to one of the following values:

  • onRequest The link must be manually traversed by the user (default value).

  • onLoad The link is automatically traversed upon loading the source document.

Setting the actuate attribute to onRequest makes a link act like an HTML link, which means that you have to click the link in order to activate it. The onLoad value offers functionality not directly available in HTML by allowing a link to be traversed when a document is first loaded. The onLoad value is particularly useful when used in conjunction with the embed value for the show attribute; this results in a resource being automatically loaded and placed directly in a document.

The last two XLink attributes are role and title, which are used primarily for descriptive purposes. The role attribute describes the role of the content in a link, whereas title provides a human-readable title for the link that may be displayed in a browser.

Creating Links with XLink

You're now finally ready to put all of your XPointer and XLink knowledge to work and create some links that would never be possible in HTML. As an example, consider an element named employees that is used to identify a group of employees for a company. Following is an example of how you might create a simple link for the employees element:

<employees xmlns:xlink="http://www.w3.org/1999/xlink"
  xlink:href="employees.xml">
  Current Employees
</employees>

This example is the simplest possible link you can create using XLink, and it actually carries out the same functionality as an HTML anchor link, which is known as a simple link in XML. Notice in the code that the XLink namespace is declared and assigned to the xlink prefix, which is then used to reference the href attribute; this is the standard approach used to access all of the XLink attributes. What you may not realize is that this link takes advantage of some default attribute values. The following is another way to express the exact same link by spelling all of the pertinent XLink attribute values:

<employees xmlns:xlink="http://www.w3.org/1999/xlink"
  xlink:type="simple"
  xlink:href="employees.xml"
  xlink:show="replace"
  xlink:actuate="user"
  xlink:role="employees"
  xlink:title="Employee List">
  Current Employees
</employees>

In this code, you can more clearly see how the XLink attributes are specified in order to fully describe the link. The type attribute is set to simple, which indicates that this is a simple link. The show attribute has the value replace, which indicates that the target resource is to replace the current document when the link is traversed. The actuate attribute has the value user, which indicates that the link must be activated by the user for traversal to take place. And finally, the role and title attributes are set to indicate the meaning of the link and its name.

The previous example demonstrated how to create a link that imitates the familiar HTML anchor link. You can dramatically change a simple link just by altering the manner in which it is shown and activated. For example, take a look at the following link:

<resume xmlns:xlink="http://www.w3.org/1999/xlink"
  xlink:type="simple"
  xlink:href="resume_e1.xml"
  xlink:show="parsed"
  xlink:actuate="auto"
  xlink:role="employee1 resume"
  xlink:title="Employee 1 Resume"/>

This code shows how to effectively embed another XML document into the current document at the position where the link is located. This is accomplished by simply setting the show attribute to parsed and the actuate attribute to auto. When a web browser or XML application encounters this link, it will automatically load the resume_e1.xml document and insert it into the current document in place of the link. When you think about it, the img element in HTML works very much like this link except that it is geared solely toward images; the link in this example can be used with any kind of XML content.

By the Way

In case you haven't fully caught on, XPointer impacts links through the href attribute, which is where you specify the location of a source or target resource for a link. All of the flexibility afforded by XPointer in specifying document parts can be realized in the href attribute of any link.


Although simple links such as the previous example are certainly important, they barely scratch the surface in terms of what XLink is really capable of doing. Links get much more interesting when you venture into extended links. A powerful use of extended links is the linkset, which allows you to link to a set of target resources via a single source resource. For example, you could use an extended link to establish a link to each individual employee in a company. To create an extended link, you must create child elements of the linking element that are set to type locator; these elements are where you set each individual target resource via the href attribute. Following is an example of an extended link, which should help clarify how they work:

<employees xmlns:xlink="http://www.w3.org/1999/xlink"
  xlink:type="extended"
  xlink:role="employees"
  xlink:title="Employee List"
  xlink:show="replace"
  xlink:actuate="user">
  <employee xlink:type="locator" xlink:href="employee1.xml">
    Frank Rizzo
  </employee>
  <employee xlink:type="locator" xlink:href="employee2.xml">
    Sol Rosenberg
  </employee>
  <employee xlink:type="locator" xlink:href="employee3.xml">
    Jack Tors
  </employee>
</employees>

This example creates an extended link out of the employees element, but the most interesting thing about the link is that it has multiple target resources that are identified in the child employee elements. This is evident by the fact that each of the employee elements has an href attribute that is set to their respective target resources.

By the Way

Keep in mind that the examples shown in this section reveal only a couple of ways to use extended links. In truth, it has yet to be revealed exactly how XLink will affect XML, and what kinds of innovative linking applications people will dream up. If web browsers eventually get on board with support for XLink, we will likely see a revolution of sorts when it comes to how links are used and perceived.


Of course, you might be wondering exactly how a link like the extended link shown here is used. In web pages, links are usually identified by colored, underlined text, and are activated simply by clicking them with the mouse. When there are multiple targets associated with a link, as in the example, it somehow becomes necessary to specify which target you want when you traverse the link. Because extended links currently aren't supported in any web browsers, it's hard to say exactly how this target resource selection will be carried out. My hunch is that you will be able to select from multiple targets that are displayed in a pop-up menu after you click a link. So, when you first click on a source resource for an extended link with multiple targets, a pop-up menu could appear with the available target links. To visit one of the links, you simply select the target from the menu. This is a reasonably intuitive way to implement the user interface portion of extended links with multiple targets, but it still isn't clear yet if browser vendors will employ this approach.

Another type of extended link is the arc, which is essentially a two-way link that connects two resources in such a way that the link can be traversed in either direction (forward or reverse). When you create an arc, you must first create locators for each resource involved in the link, and then you create the arc connections that connect the resources together. A web ring is a good example of how arcs workeach web page in a web ring has a Forward and Back button that allows you to view more pages in the ring. The URI of a web page in a web ring would be identified in a locator, whereas the connections between each page in the ring would be established with arcs.

by BrainBellupdated
Advertisement: