Introduction to XLink

XLink, which stands for XML Linking Language, is a W3C standard designed to enable more advanced linking mechanisms in XML documents than simple hyperlinks available in HTML. It offers a flexible way to create both simple and complex links between XML documents and other web resources. XLink allows you to associate multiple resources, define the behavior of links, and even establish relationships between remote documents.

What is XLink?

XLink is an XML-based language that allows you to create rich, multidirectional, and more expressive hyperlinks. It extends the hyperlink concept beyond HTML’s simple linking structure by supporting:

  • Simple links similar to HTML hyperlinks.
  • Extended links, which allow relationships between multiple resources.
  • Behavior control, defining how links are shown or triggered.

Key Features of XLink

  • Supports simple and extended links.
  • Enables links between multiple resources (multidirectional links).
  • Allows customization of link behaviors, such as when and how links are activated.
  • Can link to remote documents or specific parts of documents using XPointer.

Basic Structure of XLink

Before we dive into specific examples, let’s understand the basic structure and elements used in XLink.

Namespace Declaration

To use XLink in an XML document, you need to declare the XLink namespace:

				
					xmlns:xlink="http://www.w3.org/1999/xlink"
				
			

This tells the XML processor that the elements and attributes prefixed with xlink: belong to the XLink specification.

XLink Attributes

The primary attributes used in XLink include:

  • xlink : Defines the type of link (either simple or extended).
  • xlink : Specifies the target URI of the link.
  • xlink : Controls how the link is displayed (e.g., new, replace, embed).
  • xlink : Defines when the link should be activated (onRequest or auto).

Simple XLink

A simple XLink is similar to the traditional HTML <a> tag. It creates a one-way link from one XML document to another resource, such as a web page or another XML document.

Example of Simple XLink

				
					<book xmlns:xlink="http://www.w3.org/1999/xlink">
    <title>Learning XML</title>
    <author xlink:type="simple" xlink:href="author.xml#john_doe">John Doe</author>
</book>

				
			

Explanation:

  • The xlink:type="simple" attribute indicates that this is a simple link.
  • The xlink:href attribute points to a fragment (john_doe) in another XML document called author.xml.

When processed, this creates a link to the john_doe section within author.xml.

Extended XLink

An extended XLink is more advanced and allows you to link multiple resources together. This is useful when you want to create relationships between several documents or elements within documents.

Structure of Extended XLink

Extended links are defined using xlink:type="extended". You can then include multiple xlink:type="locator" elements within the extended link to define multiple targets.

Example of Extended XLink

				
					<book xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
    <title>XML Guide</title>
    <locator xlink:type="locator" xlink:href="authors.xml#john_doe" />
    <locator xlink:type="locator" xlink:href="reviews.xml#review1" />
</book>

				
			

Explanation:

  • The root book element is set as an extended link using xlink:type="extended".
  • Two locator elements are used to define multiple resources: one links to john_doe in authors.xml and another links to review1 in reviews.xml.

This allows the document to create relationships with both an author and a review.

Controlling Link Behavior in XLink

XLink provides attributes to control how and when a link is activated or displayed. These are the xlink and xlink attributes.

xlink

The xlink:show attribute specifies how the link should be displayed when activated. Common values include:

  • new: Opens the link in a new window or context.
  • replace: Replaces the current content with the linked content.
  • embed: Embeds the linked content within the current document.

Example of xlink

				
					<book xmlns:xlink="http://www.w3.org/1999/xlink">
    <author xlink:type="simple" xlink:href="author.xml#john_doe" xlink:show="new">John Doe</author>
</book>

				
			

In this example, when the link is clicked, the target (author.xml#john_doe) will open in a new window.

xlink

The xlink:actuate attribute defines when the link should be triggered. Common values are:

  • onRequest: The link is activated when the user explicitly requests it (e.g., clicks on it).
  • auto: The link is activated automatically when the document is loaded.

Example of xlink

				
					<book xmlns:xlink="http://www.w3.org/1999/xlink">
    <author xlink:type="simple" xlink:href="author.xml#john_doe" xlink:actuate="auto">John Doe</author>
</book>

				
			

In this case, the link to author.xml#john_doe will be automatically activated when the document is loaded, without any user interaction.

Linking to Specific Parts of XML Documents

Sometimes, you need to link not just to an entire XML document but to a specific part of it. XLink can be used in combination with XPointer to achieve this.

Example of Linking to a Specific Element

				
					<book xmlns:xlink="http://www.w3.org/1999/xlink">
    <author xlink:type="simple" xlink:href="authors.xml#xpointer(id('john_doe'))">John Doe</author>
</book>

				
			

Here, XPointer is used to point to an element in authors.xml with the ID john_doe.

Extended XLink with Multiple Resources

You can create links between multiple resources using extended XLinks. This is useful when you need to link different elements, documents, or resources together.

Example of Extended XLink with Multiple Resources

				
					<books xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
    <locator xlink:type="locator" xlink:href="authors.xml#john_doe">John Doe</locator>
    <locator xlink:type="locator" xlink:href="reviews.xml#book1_review">Review of XML Guide</locator>
</books>

				
			

Explanation:

  • The books element creates an extended link between two resources: an author (John Doe) and a review of the book.

This creates a complex, multidirectional link where multiple resources are interconnected.

Advanced XLink Usage: Arc Elements

Arc elements define the relationships between locators in extended XLinks. These elements describe how different resources are connected.

Example of Using Arc Elements

				
					<books xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
    <locator xlink:type="locator" xlink:href="authors.xml#john_doe" />
    <locator xlink:type="locator" xlink:href="reviews.xml#book1_review" />
    <arc xlink:type="arc" xlink:from="john_doe" xlink:to="book1_review" />
</books>


				
			

In this example:

  • arc defines a relationship between the author (john_doe) and the review (book1_review).

This helps you define the logical relationships between different resources.

XLink significantly extends the linking capabilities of XML by offering a robust mechanism to create both simple and complex links between documents and resources. By mastering XLink, you can build interconnected XML documents with rich relationships, improving the organization, navigation, and functionality of XML-based systems. Happy Coding!❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India