XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

"Displaying" XLinks?

January 02, 2003

Q: How do I display XLinks?

How can I get my stylesheet to display XLinks? It displays text without the linking.

Here's an excerpt from the schema:

...
<xsd:element name="who">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="description" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="type" fixed="simple" type="xsd:string"/>
<xsd:attribute name="href" type="xsd:anyURI"/>
</xsd:complexType>
</xsd:element>
...

...and some sample XML:

...
<who href="http://handheld.llnl.com/cgi-bin/palmwho.pl"
type="simple"
xmlns:xlink="http://www.w3.org/1999/xlink">
<description>Who</description>
</who>
...

...and the stylesheet::

<xsl:apply-templates select="page/body/caption/who/description"/>

A: It's possible that displaying XLinks isn't the major obstacle you're facing, which is, rather, understanding them in the first place. Just in case, you might want to review some of this background:

  • The XLink 1.0 Recommendation itself, of course, details all you need to know (in theory) in order to "use" (the scare-quotes are important) XLink in your XML documents.
  • Three XML.com resources provide a good handle on the political ups and downs of XLink and its support (or lack thereof) in applications to date:
    • Bob DuCharme's article, "XLink: Who Cares?"
    • Kendall Clark's XML-Deviant column ("The Absent Yet Present Link"), recounting discussions on the XML-DEV mailing list about XLink's prolonged invisibility.
    • Clark's back-to-back feature and XML-Deviant column, "Introducing HLink" and "TAG Rejects XLink," respectively. HLink was an alternative to XLink proposed by the XHTML Working Group of the W3C, as an alternative to XLink.

Reading those last three bulleted resources may leave you wondering if XLink is worth taking seriously at all. Despite XLink's history to date, I think it has real potential for changing the hypertext landscape. (See Tim Berners-Lee's brief note on the subject, " When should I use XLink?" Another good, brief introduction to XLink's advantages is Thomas Erl's " XLink - Inside and Out.") In this light, experimenting with XLink is no waste of time at all. So let's look at your question through the filter, first, of the XLink Recommendation itself.

One of the first things that should jump out at you is that your schema says absolutely nothing about the "xlinkability" of your document. An XLink-aware application will examine the document (perhaps, yes, with information -- such as attribute defaults -- provided by a schema or DTD) not just for href and type attributes, not even for an XLink namespace declaration. (Your document, after all, includes both of those features.) The key is that the href and type attributes must be placed in the XLink namespace, as identified by the XLink namespace declaration and corresponding namespace prefixes. Thus, your document should be changed, just slightly, to something like the following (changes in boldface):

<who xlink:href="http://handheld.llnl.com/cgi-bin/palmwho.pl"
xlink:type="simple"
xmlns:xlink="http://www.w3.org/1999/xlink">
<description>Who</description>
</who>

(Your schema, naturally, would have to take the namespace-qualified attribute names into account. Again, though, a schema or DTD does not by itself establish an element's suitability as an XLinking element. Also note that the element to which the namespace-qualified attributes apply is not itself in the XLink namespace.)

The next question is What XLink-aware application will be processing (i.e., 'displaying') this document? If it's truly XLink-aware, you won't need a stylesheet at all; the application will see the xmlns:xlink namespace declaration and the two XLinking attributes (xlink:href and xlink:type), say "Aha! the who element is an XLinking element!", and establish the link from your document to the remote resource.

The fact that you're using a stylesheet at all makes me think what you're after is the ability to render the who element as a standard hyperlink in a non-XLink-aware application -- in, say, a Web browser. But the brief excerpt from your stylesheet doesn't do that at all, at least on its own. To put it in a larger context, this excerpt must exist within a template rule. Typically (although not necessarily always) what you'd see in a stylesheet would be two or more template rules linked together in a "trickle-down" fashion, such as:

<xsl:template match="page/body/caption/who">
[processing to occur when such a who element is matched in source tree]
<xsl:apply-templates select="description">
</xsl:template>

<xsl:template match="description">
[processing to occur when description element is matched in source tree]
</xsl:template>

See the way the value of an xsl:apply-templates element's select attribute maps onto the match attribute of a different xsl:template element? In your case, assuming you haven't skipped anything critical in your stylesheet, you have not provided a specific template rule for the description element. This means that that element will be processed by XSLT's built-in rules -- which is to say that "it displays text without the linking."

Your objective, in your stylesheet, should be to transform the reasonably intelligent XLink from your source document into its (possibly) less intelligent sibling in XHTML: a plain old a element.

One approach would be to replace the second template rule above, as follows:

<xsl:template match="description">
<a href="{../@xlink:href}"><xsl:value-of select="."/></a>
</xsl:template>

This template rule:

  • Instantiates in the result tree, for each description element in the source tree, a simple a element.
  • Assigns to each a element's href attribute the value of the description element's parent's xlink:href attribute.
  • Provides each a element with a text value: the string-value of the description element currently being processed.

In the case of your sample document, what the browser (or XSLT transformation engine) will produce with these two template rules is:

<a href="http://handheld.llnl.com/cgi-bin/palmwho.pl">Who</a>

And that, I take it, is what you're looking for.

Note that this kind of transformation (from a simple-type XLink in the source tree to a classic XHTML anchor in the result) is pretty straightforward. If you're looking for a real challenge, though, try transforming an extended-type XLink into something XHTML can handle. You will probably have to consider some rather fancy Dynamic HTML and CSS effects. And I, for one, would be interested in what you come up with.



1 to 1 of 1
  1. Book: XML Programming
    2003-01-03 18:52:10 Nicolas Hoening
1 to 1 of 1