Skip to content

XML XLink

XLink is a W3C standard that defines attributes to create links between resources, primarily within XML documents. It aims to provide more powerful linking capabilities than basic HTML links.

XLink allows creating hyperlinks within XML documents.Any element in an XML document can potentially act as a link using XLink attributes.It supports simple, unidirectional links (similar to HTML’s <a> tag) and more complex ‘extended’ links that can connect multiple resources.Links can potentially be defined in separate documents (linkbases), though this feature is rarely implemented.XLink is a mature W3C Recommendation, but its practical application is limited.

Important Note: Despite being a standard, general browser support for XLink in arbitrary XML documents is virtually non-existent. While browsers support the concept for specific XML vocabularies like SVG (Scalable Vector Graphics), they typically do not process XLink attributes in general XML files loaded directly.

Therefore, while understanding the syntax is useful conceptually, relying on XLink for linking general XML data in a web browser context is not practical.

Unlike HTML’s dedicated <a> element, XML uses a namespace and specific attributes to denote links on any element.

Here’s an example demonstrating a simple XLink, conceptually similar to an HTML link:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Declare the XLink namespace -->
<document xmlns:xlink="http://www.w3.org/1999/xlink">
<heading>Useful Links</heading>
<resource xlink:type="simple"
xlink:href="https://www.w3.org/XML/"
xlink:show="new">
Visit the W3C XML Page
</resource>
<resource xlink:type="simple"
xlink:href="/local/resource.xml"
xlink:show="replace">
View Local Resource
</resource>
</document>

Key components:

  1. Namespace Declaration: xmlns:xlink="http://www.w3.org/1999/xlink" makes the xlink: prefix available, associating it with the official XLink namespace URI.
  2. xlink:type="simple": Identifies the link as a simple, unidirectional hyperlink from the current element to a single remote resource.
  3. xlink:href="URL": Specifies the destination URI (the resource to link to), similar to HTML’s href attribute.
  4. xlink:show (Optional): Suggests how the linked resource should be displayed. Common values:
    • new: Open in a new window/tab.
    • replace: Load the resource in the current window/tab (default behavior if omitted).
    • embed: Embed the resource content directly within the current document (rarely supported).
    • none: The application should decide how to handle it.
  5. xlink:actuate (Optional): Suggests when the link should be traversed.
    • onRequest: Only follow the link when activated by the user (e.g., clicked) (default).
    • onLoad: Follow the link automatically when the document loads (rarely supported for simple links).
    • none: The application should decide.

Here, XLink associates descriptions with external image resources:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Learning XML">
<description
xlink:type="simple"
xlink:href="/images/learning_xml_cover.jpg"
xlink:show="new"
xlink:actuate="onRequest">
A comprehensive guide to XML syntax, DTDs, Schemas, XPath, and XSLT.
Click here for the cover image.
</description>
</book>
<book title="SVG Essentials">
<description
xlink:type="simple"
xlink:href="/images/svg_essentials_cover.gif"
xlink:show="new"
xlink:actuate="onRequest">
Learn how to create scalable vector graphics for the web using SVG.
Click here for the cover image.
</description>
</book>
</bookstore>

Again, while syntactically correct XLink, a typical web browser loading this XML file directly would likely not make the <description> elements clickable links based solely on these attributes.

XLink also defines ‘extended links’ which can link multiple resources together, potentially with different roles (arcs) connecting them. This is more complex and even less supported than simple links.

In modern web development, linking is primarily handled via HTML (<a> tags). When dealing with XML data, it’s more common to:

  1. Parse the XML using JavaScript.
  2. Dynamically generate HTML elements (including <a> tags) based on the XML data (e.g., extracting URLs from specific XML elements or attributes).
  3. Transform the XML to HTML using XSLT on the server or client, generating appropriate <a> tags in the output.

XLink remains relevant primarily in specific XML application domains (like SVG linking) or in non-browser environments where custom processing implements the XLink specification.