Skip to main content

Tip: How to use XLink with XML

XLink works for basic links or for embedding external resources

Brett McLaughlin (brett@newinstance.com), Enhydra strategist, Lutris Technologies
Author photo: Brett McLaughlin
Brett McLaughlin (brett@newinstance.com) works as Enhydra strategist at Lutris Technologies and specializes in distributed systems architecture. He is author of Java and XML (O'Reilly). He is involved in technologies such as Java servlets, Enterprise JavaBeans technology, XML, and business-to-business applications. Along with Jason Hunter, he founded the JDOM project, which provides a simple API for manipulating XML from Java applications. He is also an active developer on the Apache Cocoon project and the EJBoss EJB server as well as a co-founder of the Apache Turbine project.

Summary:  XLink, an XML-related specification, lets you achieve dramatic linking effects in your XML documents. In this short tip learn how to include parts of other XML documents in your own XML through XLink. The code example demonstrates the technique.

Date:  01 Feb 2002 (Published 01 Jul 2001)
Level:  Introductory
Activity:  3887 views

Since the release of XML in early 1998, interest in XML has continued to grow. There are an enormous number of XML-related specifications in use today: XPointer, XLink, XSD (XML Schema), RDF, RSS, and XHTML, to name a few. In this tip, I'll explore XLink, a particularly useful specification that defines an XML linking mechanism for referring to other XML documents.

For those of you who are HTML authors, XLink may sound a lot like the a element that you have used hundreds of times before, as in <a href="http://www.nickelcreek.com">Check out Nickel Creek!</a>. This sort of link is referred to as a unidirectional link. That simply means that it goes from your page to the target page (Nickel Creek's Web site in this example), but not from that site back to yours.

XLink offers much more than unidirectional linking, though. Using XLink, you can create bidirectional links, which allow travel in two directions.. You can also define how links are processed, and, most importantly, you can allow linking from any XML element. This is quite an improvement over only being able to use the a element. For all of these reasons, XLink is worth taking some time to look at.

XLink in action

Let me start by showing you a small XML document that represents a few acoustic guitars: Listing 1.

In the listing, you'll notice that I reference the XLink namespace so that the document has access to the XLink attributes and features. Second, I only used XLinks of the "simple" type, specified by the xlink:type attribute. You can also specify "extended", "locator", "arc", "resource", "title", and "none" for this value; however, I have avoided these more complex types. That's because browser support for XLinks is currently minimal; you will find some support in Mozilla 0.98 and Netscape 6.02, and even less in Internet Explorer 6.0. Earlier versions of all of these browsers had no XLink support at all. I would recommend that you experiment with only the basics now, and then move to more advanced features when browsers catch up with the XLink specification.

With Listing 1, you've got some basic XML to work with. To take advantage of XLink, all you have to do is use XLink-specific attributes on the elements that contain links. Look at Listing 1's guitar element, which specifies a luthier (that's a guitar maker) for each guitar. I already discussed the use of the xlink:type attribute, which is set to the value "simple". The element then specifies a URL to link to using XLink. To specify this URL, the element uses the xlink:href attribute. At this point, you're probably thinking "No big deal -- this looks a lot like HTML." Well, you're absolutely right about that! By default, this XLink will set up the link to replace the current window when the link is clicked. If you want the target of the link to open in a new window, you can add the xlink:show attribute, and give it a value of "new"; the default is "replace", which is normal HTML behavior. You'll recognize xlink:show as similar to specifying a different target window in HTML. And, as easily as that, you are using XLink.


XLink's potential

Up to this point, I've covered only basic linking. Things get more interesting when you want to access remote locations as resources, instead of standalone pages. Look at the description element in Listing 1. It sets the value of the xlink:show attribute to "embed". This means that the resource (an image file in this case) should be processed inline within the page. This instructs an XLink-aware browser to insert the specified document within the XML, rather than opening the resource in a new window. When you consider that this could be another XML document and not just an image, the possibilities really begin to open up; for example, you could have a hierarchy of XML documents, each building upon and referring to others.

Going even further, you can specify when the resource should appear. The timing is handled by the xlink:actuate attribute, which defines when the resource is "read" and shown. When the value is "onLoad", as it is in Listing 1, the resource should be loaded when the initial document loads, and it should be shown then as well. This is similar to how normal HTML resource loading works. You can also specify the value "onRequest" for the attribute, which means that until the link is clicked, the resource is not shown. This is handy for low-bandwidth settings, allowing users to only access resources when they choose (through a mouse click).

I know this has been a short and simple look at XLink, but it should give you a good start. For more on XLink, check out the specification in Resources. You should also keep an eye on feature lists for new browser support for XLink, and once XLink is supported, have at it!


Resources

  • For more on XLink, check out the XLink specification.

  • While you're at it, check out XPointer, which adds even more advanced linking to XLink.

  • Bone up on your XML with Doug Tidwell's tutorial on XML, "Introduction to XML" (developerWorks, August 2002 ).

About the author

Author photo: Brett McLaughlin

Brett McLaughlin (brett@newinstance.com) works as Enhydra strategist at Lutris Technologies and specializes in distributed systems architecture. He is author of Java and XML (O'Reilly). He is involved in technologies such as Java servlets, Enterprise JavaBeans technology, XML, and business-to-business applications. Along with Jason Hunter, he founded the JDOM project, which provides a simple API for manipulating XML from Java applications. He is also an active developer on the Apache Cocoon project and the EJBoss EJB server as well as a co-founder of the Apache Turbine project.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=12014
ArticleTitle=Tip: How to use XLink with XML
publish-date=02012002
author1-email=brett@newinstance.com
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers