XML, XLink, and XQuery

XLink is used to create hyperlinks in XML documents.

XPath

  • XLink is used to create hyperlinks in XML documents
  • Any element in an XML document can act as a link
  • Through XLink, links can be defined outside the linked file
  • XLink is a W3C recommended standard

XLink Browser Support

Browsers do not support XLink in XML documents.

However, all mainstream browsers support XLink in SVG.

XLink Syntax

In HTML, the <a> element defines a hyperlink. However, this is not how it works in XML.

In an XML document, you can use any element name you want - so the browser cannot predict which link elements will be called in the XML document.

Here is a simple example of how to create links in an XML document using XLink:

<?xml version="1.0" encoding="UTF-8"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
  <homepage xlink:type="simple" xlink:href="https://www.codew3c.com">Visit CodeW3C.coms</homepage>
  <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>

If we need to access the XLink feature, we must declare the XLink namespace. The XLink namespace is: "http://www.w3.org/1999/xlink".

The xlink:type and xlink:href attributes in the <homepage> element come from this XLink namespace.

xlink:type="simple" creates a simple "HTML-like" link (meaning "click here to go elsewhere").

The xlink:href attribute specifies the URL to be linked to.

XLink Example

The following XML document contains XLink functionality:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Yashu Talk About Eating">
  <description
  xlink:type="simple"
  xlink:href="/images/cswd.jpg"
  xlink:show="new">
  The book "Yashu Talk About Eating" is a guidebook for Chaozhou cuisine, providing the necessary guidance and help for food lovers and those who seek flavors;
  This book can also be said to be a humanistic reader of Chaozhou cuisine. It introduces Chaozhou cuisine rather than Chaozhou food, fully挖掘 and display the hidden local culture, dietary customs, and historical details behind the food.
  </description>
</book>
<book title="The Wonderful Fox Dad">
  <description
  xlink:type="simple"
  xlink:href="/images/mrfox.jpg"
  xlink:show="new">
  Bogus, Bons, and Bin are the most despicable and mean-spirited people you can meet. They hate Mr. Fox and decide to eliminate his entire family.
  Thus, these three people with guns waited cunningly outside Mr. Fox's cave... But Mr. Fox had a clever plan to deal with them!
  Thus, a contest of wisdom and strength began...
  </description>
</book>
</bookstore>

Example Explanation:

  1. The XLink namespace is declared at the top of the document (xmlns:xlink="http://www.w3.org/1999/xlink")
  2. xlink:type="simple" creates a simple "similar to HTML" link
  3. The xlink:href attribute specifies the URL to which the link should point (in this case, an image)
  4. xlink:show="new" indicates that the link should open in a new window

XLink - Go Further

In the above example, we demonstrated a simple XLink.

XLink becomes more interesting when we use remote locations as resources instead of accessing them as standalone pages.

If we set the xlink:show attribute value to "embed", the linked resource should be handled inline within the page. For example, if you think it might be another XML document, you can build the hierarchy of the XML document.

You can also use the xlink:actuate attribute to specify when the resource should appear.

XLink Attribute Reference

Attribute Value Description xlink:actuate onLoad onRequest other none Define when to read and display the linked resources: onLoad - The resources should be loaded and displayed when the document is loaded onRequest - The resource will not be read or displayed before clicking the link xlink:href URL specifies the URL to be linked to. xlink:show embed new replace other none specifies where to open the link. The default is "replace". xlink:type simple extended locator arc resource title none specifies the type of link.

XPointer

XPath
  • XPointer allows links to point to specific parts of an XML document
  • XPointer uses XPath expressions to navigate within XML documents
  • XPointer is a W3C recommended standard

XPointer Browser Support

No browsers support XPointer. But XPointer is also used in other XML languages.

XPointer Example

In this example, we will combine the use of XPointer and XLink to point to a specific part of another document.

we first take a look at this target XML document (the document we link to):

<?xml version="1.0" encoding="UTF-8"?>
<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
  <picture url="https://dog.com/rottweiler.gif" />
  <history>The Rottweiler's ancestors were probably Roman
  drover dogs.....</history>
  <temperament>Confident, bold, alert and imposing, the Rottweiler
  is a popular choice for its ability to protect....</temperament>
</dog>
<dog breed="FCRetriever" id="FCRetriever">
  <picture url="https://dog.com/fcretriever.gif" />
  <history>One of the earliest uses of retrieving dogs was to
  help fishermen retrieve fish from the water....</history>
  <temperament>The flat-coated retriever is a sweet, exuberant,
  lively dog that loves to play and retrieve....</temperament>
</dog>
</dogbreeds>

Note that the XML document uses the id attribute on each element!

Therefore, XPointer allows you to link to specific parts of a document, rather than to the entire document (like XLink).

To link to a specific part of a page, add a hash (#) and an XPointer expression to the URL in the xlink:href attribute, as shown below:

xlink:href="https://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"

This expression refers to the element with the id value "Rottweiler" in the target document.

xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('Rottweiler'))".

XPointer also allows for abbreviated methods to link to elements with ids. You can directly use the value of the id, as shown below:

xlink:href="https://dog.com/dogbreeds.xml#Rottweiler

The following XML document contains links to more information about each dog's breed:

<?xml version="1.0" encoding="UTF-8"?>
<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog>
  <description>
  Anton is my favorite dog. He has won a lot of.....
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#Rottweiler">
  Fact about Rottweiler
  </fact>
</mydog>
<mydog>
  <description>
  Pluto is the sweetest dog on earth......
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#FCRetriever">
  Fact about flat-coated Retriever
  </fact>
</mydog>
</mydogs>