Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.5 Links

A hyperlink is a very rich concept, even though its implementation in HTML is rather primitive. Basically, an HTML link consists of two parts: the address that tells the browser where to go and the link element itself that (with its attributes and children) defines the link's presentation and behavior. However, in HTML, all possible address types are limited to a single syntax (URI), and all possible link types are served by one element type (a) with a limited set of attributes. Let's see how we can improve this scheme.

Note that this section only covers inline links that are part of the body of a page and thus need to be specified in the page's XML source. Navigational links, created by the stylesheet based on the master document data, are discussed in 3.9.1.

3.5.1 Elements or attributes?

When deciding how to cast your linking semantics into XML constructs, it is natural to reuse the HTML approach with a link consisting of an element (signaling the link) and its attributes (providing the address and other link properties). For example, you might write

This was <link address="address">reviewed</link> elsewhere.

However, this only looks good when you're linking text fragments within a text flow. As soon as you have a separate element representing some object that may have a link property (among others), it is much more convenient to designate the link by an extra attribute of that element rather than a wrapper element. For example, it is easier to create a linked image like this:

<image src="button" link="address"/>

compared to the HTML-inspired approach:

<link address="address"><image src="button"/></link>

Not only the address but other properties of a link as well (such as its title, behavior, or classification) might similarly attach as attributes to an element that represents a nontextual link.

So, we see that it is natural to express linking semantics via a set of attributes that may apply to many different elements (or even to any element at all) instead of an element type with its own fixed attributes. This is because a link is most often an attribute of some object rather than an object in itself. This approach was implemented in W3C's XLink standard,9 and you may consider incorporating XLink into your source definition for link markup (however, please read the rest of 3.5 for other possible link properties, not all of which are supported by XLink).

For in-flow textual links, you still need a generic linking element type (such as link in the example above) that only serves as a markup container for the same set of linking attributes. Most schema languages have no problem defining a separate set of attributes that can be used in different element types.

3.5.2 Link types

Along with the href attribute with the link's URI, an a element in HTML may provide a target attribute for specifying the target window or frame for the linked resource. However, just as you can add attributes with JavaScript code to program various aspects of the link's behavior (e.g., actions performed when the link is activated), your source XML may also need to provide link properties other than the address.

This does not mean, of course, that you'll have to embed JavaScript into your XML source. As with any other data, what you need to do first is develop a classification of all possible types of link attributes or behaviors, without detailing their implementation. As soon as you have such a classification, it's easy to coin an appropriate attribute and define the vocabulary of allowed values for it.

Categorizing links. For example, analysis may reveal that your links fall into one of the following categories:

  • internal links (links to other pages within the site);

  • external links (links to other sites);

  • dictionary links (links to a script on an online dictionary site providing definitions for linked words); and

  • thumbnail links (thumbnails linked to pop-up windows with larger versions of images or pages).

Both thumbnail links and dictionary links may be either internal or external. However, they need to be classified separately because of their special role on the pages, resulting in different formatting and behavior. On the other hand, you may not be planning any formatting or behavior differences between internal and external links, but separating them into different types is still a good idea because it is a natural classification and because this lets you make your address abbreviations (discussed in 3.5.3) more logical.

Classifier attributes. To differentiate these link types, we could add a classifier attribute, e.g. linktype, specifying the type of the link:

...available on the <link linktype="external" 
link="www.kirsanov.com/te/">original site</link> and 
<link linktype="internal" link="mirror/te">mirrored here</link>.

This approach works both with standalone link elements and with any other elements that may need to use these linking attributes (e.g., image). Note that we used linktype rather than type and link rather than address for the attribute names so that the common prefix, link, will help you keep track of these attributes as a group without the risk of confusing them with their parent elements' native attributes. You can also separate all linking attributes into a namespace of their own, but this is not really necessary unless you plan to use them with different document vocabularies.

It's also advisable to make all linking attributes but the address (i.e., link) optional and provide sensible default values. For example, you can mandate that the missing linktype attribute in a linked element implies that the link is internal.

Classifier element types. For in-flow links, instead of (or, better, in addition to) the bulky classifier attribute, a separate element type for each link type is more convenient. As these element types will be used quite often, each should have a short but clear name:

...available on the <ext link="www.kirsanov.com/te/">original 
site</ext> and <int link="mirror/te">mirrored here</int>.

Separate element types have the additional advantage of being easier to validate with grammar-based schema languages like DTD or XML Schema. schema languagesgrammar-based DTDsvalidation with validationwith DTDs XSDLvalidation with validationwith XSDL

Advanced link types. Other link types may have their own sets of required and optional attributes and may perform other functions, besides creating a link. For instance, dictionary links from the above classification are likely to be used only within text flow, so we can introduce a special element type for them and declare that whenever the address attribute is missing, the element's content is taken as the (abbreviated, 3.5.3) address:

...was going to <def>disembogue</def> profusely.

...at which point it <def word="disembogue">disembogued</def> itself...

Here, two occurrences of the obscure word disembogue are linked to a dictionary site, so that a pop-up window or floating tooltip with the word's definition could be displayed when the link is activated in some way (e.g., clicked or hovered over). You don't need to specify the dictionary site to use, or the complete URL for accessing the dictionary script, or the JavaScript code to create the pop-up; all this is taken care of by the stylesheet. The only thing you may need in the source is the word attribute that optionally provides the base form of the linked word or phrase; if it is absent, the contents of the def element are used.

For generality, this special kind of link can also be given by a link element with linktype="dictionary" and the link attribute playing the role of word.

Similarly, a thumbnail link could be created by a thumb element with a single attribute (e.g., image). This attribute would provide the identifier of the corresponding image, with the stylesheet doing all the rest: inserting and formatting the thumbnail, creating a display page with the full-size version of the image, and linking it to the thumbnail. The stylesheet can even automatically create the thumbnail from a full-size image (5.5.2.6).

3.5.3 Abbreviating addresses

When creating a link, we usually want to specify a certain piece of content that the link will point to. What a URL allows us to specify, however, most often is a file that can be moved, renamed, or deleted even if the content we are interested in is still out there somewhere. Moreover, a URL includes a lot of technical information (protocol, file extension) that is not relevant for our purpose of establishing a content-level link.

All this invites the idea of using abbreviated addresses that would hide the underlying technical complexity of URLs and provide an abstraction layer protecting our semantic XML from URL changes. For each address, we will create an identifier to be used in the XML source; at transformation time, the stylesheet will resolve this identifier into the actual URL to be put into the corresponding HTML link element.

Example: RFC links. Suppose you often need to link to enumerated documents such as RFCs.10 Such links could use a special value of the link classifier attribute and/or an element type of their own. However, to make them even more convenient, it is natural to use only the RFC number as an abbreviation for the complete URI:

...as per <rfc num="1489"/>.

Or, the same could be spelled out in a generic fashion:

...as per <link linktype="rfc" link="1489"/>.

This latter variant uses generic linking attributes that can be applied to different elements to make links out of the corresponding objects, whereas the num attribute is only recognized in an rfc element.

The XSLT stylesheet will have to recognize this type of link, possibly apply some special formatting to it, and most importantly resolve (unabbreviate) the abbreviated address. In this example, unabbreviation would supply the complete URL of the referenced document for the HTML link:

...as per 
<a href="ftp://ftp.rfc-editor.org/in-notes/rfc1489.txt">RFC 1489</a>.

You could also allow an rfc element to enclose character content:

...which was <rfc num="1489">defined</rfc> in 1993.

which would give the following in HTML:

...which was 
<a href="ftp://ftp.rfc-editor.org/in-notes/rfc1489.txt">defined</a> 
in 1993.

Mnemonic addressing. Abbreviated addresses in your source XML must be unique only within your site, as opposed to URLs that are globally unique. This means you can make them easier to remember and more meaningful (to you) than are URLs. The abbreviated addresses are also completely devoid of irrelevant technical details and can be arbitrarily long (i.e., detailed and readable) or arbitrarily short (i.e., quick to type and quick to read).

3.5.3.1 Multiple abbreviation schemes

You can use as many independent abbreviation schemes as necessary. Each more or less complete and logical group of addresses can be served by its own abbreviation algorithm (and the corresponding resolver in the stylesheet). For example, links to an online dictionary or search engine might be abbreviated to just the word you want to look up; links to W3C standards can be represented by their unique identifiers as used by the W3C site (e.g., xslt20 for XSLT 2.0, which unabbreviates into http://www.w3.org/TR/xslt20/). Any address domain whose URLs can be "losslessly compressed" into a shorter or easier-to-remember form is ripe for abbreviation.

With multiple abbreviation schemes, the stylesheet must be able to know which one to use for each link. This is where link types () are useful, distinguished by a classifier attribute value (<link linktype="rfc" ...>) or the element type (<rfc ...>) used for each link. It is natural to define abbreviation schemes on a per-link-type basis, or even to define link types based on the abbreviation schemes they are using.

Along with resolving the address, your stylesheet can perform other processing tasks, such as retrieving the title of the referenced RFC to be displayed in the link's floating tooltip. A Schematron schema for your source definition, in addition to performing link syntax validation, can also check for broken links (5.1.3.3). Another important advantage is that you can easily change all your RFC links from one RFC repository to another simply by editing the stylesheet.

3.5.3.2 Unabbreviation algorithms

To expand the abbreviated addresses, your stylesheet may use any sources of information, such as local or remote database queries or even web search. It's easiest, however, to create simple algorithmic abbreviations that map to the corresponding URLs through some calculations or string manipulations.

Thus, for external links, the most obvious and perhaps the only sensible abbreviation is dropping the protocol specification (usually http://) from the URLs. Even this simple provision can make address input somewhat easier by allowing you to type www.kirsanov.com instead of http://www.kirsanov.com.

Note, however, that in this case the stylesheet must be able to recognize the protocol part of an address and only add http:// if it is missing. Addresses that already contain a protocol specification (be it http://, https://, or ftp://) must not be modified in any way.

3.5.3.3 Multicomponent abbreviations

An address abbreviation may contain more than one component. This is often necessary to link to scripts (as opposed to static pages) that require a number of parameters in the request URI. Some of these parameters (e.g., the partner's ID or formatting options) are static and can therefore be filled in by the stylesheet, but the key information pointers (e.g., the date and the number of the article within that date) must be present in the source of the linking page. Here's an example of a link with a multicomponent abbreviated address:

As <foonews 
       date="02-12-2003"
	   num="6490">reported</foonews> by FooBarNews...

which could be expanded into the HTML link:

As <a 
href="http://foonews.com/news?date=02-12-2003&num=6490">reported</a>
by FooBarNews...

3.5.3.4 Internal links

One highly recommended abbreviation scheme that makes sense for almost any site is using page identifiers, defined in the master document, instead of pathnames11 for internal links. This will make your site's structure much more flexible because you will be able to rename a page or move it around without changing all the other pages that link to it.11

Linking a foobar. For example, suppose you have a page on your site describing a product called Foobar Plus. You don't want to spell out the complete pathname each time you link to that page, as it may be quite long (e.g., /products/personal/foobar_plus). Much more convenient would be using that page's unique (within your site) and easy-to-remember identifier. Since you don't, in all probability, have another Foobar Plus on your web site, it is natural to use an abbreviated name of the product as the identifier:

Check out our new <int link="fb+">Foobar Plus</int>!

The correspondence between web pages and their identifiers is to be set in the master document (3.9.1.2, page 129). Now it doesn't matter if your Foobar Plus page is moved, say, from /products/personal/foobar_plus to /products/corporate/foobarplus. All you need to do is change the reference in the master document and retransform all site pages.

Aliases. To make life even easier for site maintainers, you can allow them to use any of a number of aliases referring to the same page. For example, the Foobar Plus page might just as well be linked to as fb+, foobar+, or foobar-plus. All you need to do is register all such aliases in the master document (see Example 3.2 on page 143).

Linking translations. In multilingual sites, a special kind of link that must be present on every page is the link(s) to the other language version(s) of the same page. The absolute minimum of information needed to construct such a link is, obviously, the identifier of the language we are linking to. Thus, if we write on the Foobar Plus page

<lang link="de">This page in German</lang>

then the stylesheet will use the current page's pathname to construct the proper HTML link - for example,

<a href="/products/personal/foobar_plus.de.html">This 
page in German</a>

or

<a href="/products/personal/foobar_plus.html?lang=de">This
page in German</a>

or any other variant, depending on your web site setup. Once again, the correspondence between languages and link URIs is deduced from the master document's data.

  • + Share This
  • 🔖 Save To Your Account