Home arrow XML arrow Page 2 - XLink Basics

...In With The New - XML

If XML is to get anywhere near as popular as HTML is today, it needs to address a pretty basic requirement: the ability to connect different pieces of data together. Luckily, the W3C has a solution...and it's called XLink. Read on for the scoop.

TABLE OF CONTENTS:
  1. XLink Basics
  2. ...In With The New
  3. Playing By The Rules
  4. A Little Experiment
  5. Back To Base
  6. A Little More...
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 3
October 05, 2001

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Given these requirements, XLink's authors have considered the linking process and broken it up into three distinct and atomic parts, which I will define below:

The "link definition", which clearly defines the relationship between the items to be linked;

The "participating resources", or the items connected together by an XLink - these resources may be local (stored within the same physical document) or remote (stored within a different document);

"Traversal rules" or "arcs", which specify the direction of traversal between a pair of participating resources. Arcs may be "outbound" (local resource to remote resource), "inbound" (remote resource to local resource) or "third-party" (remote resource to remote resource.)

Using these three basic constructs, it becomes possible to create links which satisfy all the requirements stated above. Take a look at the example below, which illustrates these concepts:

<?xml version="1.0"?> <performers xmlns:xlink="http://www.w3.org/1999/xlink"> <item xlink:type="extended"> <!-- link definition (local) --> <link xlink:type="resource" xlink:label="overview" xlink:title="Information on Sinatra">Frank Sinatra</link> <!-- link definitions (remote) - Sinatra's biography, songs and articles --> <link xlink:type="locator" xlink:href="bio.xml" xlink:label="bio" xlink:title="Biography" /> <link xlink:type="locator" xlink:href="songs.xml" xlink:label="songs" xlink:title="Songs"/> <link xlink:type="locator" xlink:href="press.xml" xlink:label="press" xlink:title="Press articles" /> <!-- local to remote arc - from name to biography --> <arc xlink:type="arc" xlink:from="overview" xlink:to="bio" xlink:show="replace" xlink:actuate="onRequest" /> <!-- remote to remote arc - from biography to song list --> <arc xlink:type="arc" xlink:from="bio" xlink:to="songs" xlink:show="replace" xlink:actuate="onRequest" /> <!-- remote to remote arc - from biography to press archive --> <arc xlink:type="arc" xlink:from="bio" xlink:to="press" xlink:show="replace" xlink:actuate="onRequest" /> </item> </performers>
This example sets up conceptual links between a performer (the evergreen Frank Sinatra), his biography, his songs, and an archive of press articles about him. Arcs specify the direction of link traversal - they allow for navigation between the performer and his biography, between the biography and a song list, and between the biography and an archive of press clippings.

It's important to note at this point that XLinks are not expressed as elements, but as element attributes (from the XLink namespace) which can be attached to any XML element; the most important of these is the XLink "type" attribute, which specifies the type of link being defined. The example above uses this attribute to define four types of links: extended links, resources, locators and arcs (more on these later).

By allowing any XML element to become an XLink, the XLink specification substantially improves on HTML's current linking mechanism, which only allows the anchor tag to define links. In the example above, the "item", "link" and "arc" XML elements have been converted to XLinks by the addition of specific attributes from the XLink namespace.

Depending on the value of the XLink "type" attribute, one or more XLink attributes are required to provide additional information about the link - the XLink specification lays down the basic rules and constraints for each of these. In the example above, you can see that XLinks of type "locator" have an additional "href" attribute, while XLinks of type "arc" display "from" and "to" attributes.{mospagebreak title=Keeping It Simple} XLink allows for the construction of two basic types of links: simple and extended.

Simple links are the ones you've been used to for so long - anchor-tag-style hyperlinks which link two resources together, with a clearly-defined direction of traversal. Since these types of links are the most common, they've been included in the XLink specification as "shortcuts" for link authors who don't need all the power and flexibility of extended links.

Here's an example:

<?xml version="1.0"?> <performers xmlns:xlink="http://www.w3.org/1999/xlink"> <item xlink:type="simple" xlink:href="bio.xml">Sinatra's biography</item> <item xlink:type="simple" xlink:href="songs.xml">Song list</item> <item xlink:type="simple" xlink:href="press.xml">Press clippings</item> </performers>
This is very similar to the standard linking construct used in today's HTML pages - if you recreated the links above in HTML, you would see something like this:

<html> <head> </head> <body> <a href="bio.xml">Sinatra's biography</a> <a href="songs.xml">Song list</a> <a href="press.xml">Press clippings</a> </body> </html>
Extended links, on the other hand, are a completely different beast. They allow link authors to create new types of links, typically by using arcs to create relationships (via multiple traversal rules) between many different local and remote resources. Since extended links connect many resources together, they are typically stored separately from the resources they link together - and, if you've been paying attention, you'll immediately realize this implies that it now becomes possible to update link definitions without requiring write access to either source or destination(s).

Consider the following diagram, which sets up links between an employee's profile, salary and performance appraisal,



and then look at how you could represent these relationships with XLink.

<?xml version="1.0"?> <ext xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended"> <!-- starting point - employee's name (local) --> <local xlink:type="resource" xlink:label="name" xlink:title="John Doe" /> <!-- employee's salary (remote) --> <remote xlink:type="locator" xlink:href="salary.xml" xlink:label="salary" xlink:title="John Doe's salary information" /> <!-- employee's last appraisal (remote) --> <remote xlink:type="locator" xlink:href="appraisal.xml" xlink:label="appraisal" xlink:title="John Doe's last performance appraisal" /> <!-- employee's resume (remote) --> <remote xlink:type="locator" xlink:href="resume.xml" xlink:label="resume" xlink:title="John Doe's resume" /> <!-- link name to resume --> <arc xlink:type="arc" xlink:from="name" xlink:to="resume" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from name to resume" /> <!-- link name to current salary --> <arc xlink:type="arc" xlink:from="name" xlink:to="salary" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from name to salary" /> <!-- link last appraisal to current salary--> <arc xlink:type="arc" xlink:from="appraisal" xlink:to="salary" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from appraisal to salary" /> <!-- these next two arcs set up a bidirectional link --> <!-- link qualifications to salary --> <arc xlink:type="arc" xlink:from="resume" xlink:to="salary" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from resume to salary" /> <!-- link salary to qualifications --> <arc xlink:type="arc" xlink:from="salary" xlink:to="resume" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from salary to resume" /> </ext>
An extended link (or, to be more precise, an XML element containing an extended link) is actually nothing more than a wrapper around other XLink definitions for local and remote participating resources and arcs.

<ext xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended"> <!-- participating resources --> </ext>
These nested elements can represent any of the following four link types:

Resource: This link type represents a local resource which participates in the extended link. Typically, it contains some content which serves as the starting point for link traversal.

<local xlink:type="resource" xlink:label="name" xlink:title="John Doe" />
Locator: This link type represents a remote resource (via the additional "href" attribute) which participates in the extended link.

<remote xlink:type="locator" xlink:href="salary.xml" xlink:label="salary" xlink:title="John Doe's salary information" />
Arc: An arc sets up navigation rules between locators and resources (via the additional "from" and "to" attributes), and is the primary construct for specifying the direction and behaviour of link traversal.

<arc xlink:type="arc" xlink:from="name" xlink:to="salary" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from name to salary" />
Arcs come in very handy when setting up so-called bi-directional links - for example, A -> B and B -> A. Here's an example:

<arc xlink:type="arc" xlink:from="resume" xlink:to="salary" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from resume to salary" /> <arc xlink:type="arc" xlink:from="salary" xlink:to="resume" xlink:show="replace" xlink:actuate="onRequest" xlink:title="Link from salary to resume" />
Now, however, that the XLink specification very clearly states that arcs cannot be duplicated - in other words, a particular from-to relationship can be represented by one and only one arc.

Title: This link type is used to provide additional, human-readable information for an extended link's participating resources. The XLink specification suggests that this link type is primarily used in the context of internationalization, where different languages may require different titles - for example,

<ext xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended"> <greeting xlink:type="title" xml:lang="en">Hello!</greeting> <greeting xlink:type="title" xml:lang="fr">Bon jour!</greeting> <greeting xlink:type="title" xml:lang="it">Ciao!</greeting> </ext>


 
 
>>> More XML Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

XML ARTICLES

- Google Docs and Xpath Data Functions
- Flex Array Collection Sort and Filtering
- The Flex Tree Control
- Flex List Controls
- Working with Flex and Datagrids
- How to Set Up Podcasting and Vodcasting
- Creating an RSS Reader Application
- Building an RSS File
- An Introduction to XUL Part 6
- An Introduction to XUL Part 5
- An Introduction to XUL Part 4
- An Introduction to XUL Part 3
- An Introduction to XUL Part 2
- An Introduction to XUL Part 1
- XML Matters: Practical XML Data Design and M...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: