XML, XLink and XPointer

To create hyperlinks in XML documents, XLink is used which is a W3C Recommendation. In an XML document, any element can behave like a link. The links can be defined outside the linked files with XLink.

XLink Browser Support:

In XML documents, no browser support for XLink is available, but all major browsers support XLinks in SVG.

XLink Syntax:

The <a> element in HTML defines a hyperlink, but it is not so in XML. Browsers can’t predict what link elements will be called in XML documents, coz we can use whatever element names we want in XML documents.

Example: To use XLink to create links in an XML document:

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

Explanation:

The XLink namespace needs to be declared to get access to the XLink features. The XLink namespace is: “http://www.w3.org/1999/xlink“. Coming from the XLink namespace is the xlink:type and the xlink:href attributes in the <homepage> elements. A simple “HTML-like” link (means “click here to go there”) is created by the xlink:type=”simple”. The URL to link to is specified by the xlink:href attribute.

Example:

<?xml version="1.0" encoding="UTF-8"?>
 
<flowers xmlns:xlink="http://www.w3.org/1999/xlink">
 
<flower title="Rose">
  <description
 xlink:type="simple"
 xlink:href="/images/rose.gif"
 xlink:show="new">
 A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or.......
  </description>
</flower>
 
<flower title="Lily">
  <description
 xlink:type="simple"
 xlink:href="/images/lily.gif"
 xlink:show="new">
 Lilies are a group of flowering plants which are important in culture and literature in much of the world.......
  </description>
</flower>
 
</flowers>

Explanation:

In the above example, the XML document contains the XLink features. Declared at the top of the document is the XLink namespace (xmlns:xlink=”http://www.w3.org/1999/xlink“). A simple “HTML-like” link is created by the xlink:type=”simple”. The URL to link to is specified by the xlink:href attribute and that the link should open in a new window is specified by the xlink:show=”new”.

XLink – Going Further:

XLink is more interesting to be used while accessing remote locations as resources, instead of standalone pages. The linked resource should be processed inline within the page on setting the value of the xlink:show attribute to “embed”. On considering it as another XML document we can, for example, build a hierarchy of XML documents. Also by using the xlink:actuate attribute, we can specify when the resource should appear.

XLink Attribute Reference:

Attribute Value Uses
xlink:actuate onLoad

onRequest

other

none

To specify when the linked resource is read and shown.

  • onLoad – To load and show the resource when the document loads.
  • onRequest – To not read or show the resource before the link is clicked.
xlink:href URL To specify the URL to link to.
xlink:show embed

new

replace

other

none

To specify where to open the link. Its default value is “replace”.
xlink:type simple

extended

locator

arc

resource

title

none

To specify the type of link.

XPointer:

To allow the links to point to specific parts of an XML document, the XPointer can be used. It is a W3C Recommendation and uses XPath expressions to navigate in the XML document

XPointer Browser Support:

With no browser support, XPointer is used in other XML languages.

Example:

<?xml version="1.0" encoding="UTF-8"?>
 
<flowers>
 
<flower breed="Rose" id="Rose">
  <picture url="images/rose.gif" />
  <description>A rose is a woody perennial flowering plant of the genus Rosa, in the family Rosaceae, or.......</description>
  <colors>Pink-, yellow-, red- or white-colored roses (Rosa spp.), some striped or splotched, naturally occur in wild....</colors>
</flower>
 
<flower breed="Lily" id="Lily">
  <picture url="images/lily.gif" />
  <description> Lilies are a group of flowering plants which are important in culture and literature in much of the world.......
</description>
  <colors>White, pink, red, yellow, orange, and many more, each with different meanings attached....</colors>
</flower>
 
</flowers>

Explanation:

In the above example, we are using XPointer in conjunction with XLink to point to a specific part of another document. For this, we are looking at the target XML document, i.e., the document we are linking to. Here, the XML document uses id attributes on each element. Thus, using XPointer we can link to specific parts of the document, instead of linking to the entire document (as with XLink). We need to add a number sign (#) and an XPointer expression after the URL in the xlink:href attribute to link to a specific part of a page: xlink:href=”images/flowers.xml#xpointer(id(Rose))”. The above expression thus refers to the element in the target document that has the id value of “Rose”.

Example:

<?xml version="1.0" encoding="UTF-8"?>
 
<flowers xmlns:xlink="http://www.w3.org/1999/xlink">
 
<flower>
  <description>
Yellow rose represents friendship, joy, caring, warmth, delight, gladness and affection, as well as good luck, welcome back, and......
  </description>
  <meaning xlink:type="simple" xlink:href="images/flowers.xml#Rose">
 Meaning of Yellow Rose
  </meaning>
</flower>
 
<flower>
  <description>
 Yellow lilies symbolizes thankfulness and desire for.......
  </description>
  <meaning xlink:type="simple" xlink:href="images/flowers.xml#Lily">
 Meaning of Yellow Lily
  </meaning>
</flower>
 
</flowers>

Explanation:

In the above example, the XML document contains links to more information of the dog breed for each of the dogs. A shorthand method is thus facilitated by XPointer to link to an element with an id. For this, the value of the id can be used directly, i.e., xlink:href=”images/flowers.xml#Rose”.

Please Share