You are on page 1of 4

XLINK AND XPOINTER TUTORIAL

XLink and XPointer Introduction


• XLink defines a standard way of creating hyperlinks in XML documents.
• XPointer allows the hyperlinks to point to more specific parts (fragments) in the XML document.

What You Should Already Know


Before you continue you should have a basic understanding of the following:
• HTML / XHTML
• XML / XML Namespaces
• XPath
If you want to study these subjects first, find the tutorials on our Home page.

What is XLink?
• XLink is short for the XML Linking Language
• XLink is a language for creating hyperlinks in XML documents
• XLink is similar to HTML links - but it is a lot more powerful
• ANY element in an XML document can behave as an XLink
• XLink supports simple links (like HTML) and extended links (for linking multiple resources together)
• With XLink, the links can be defined outside of the linked files
• XLink is a W3C Recommendation

What is XPointer?
XPointer is short for the XML Pointer Language
• XPointer allows the hyperlinks to point to specific parts of the XML document
• XPointer uses XPath expressions to navigate in the XML document
• XPointer is a W3C Recommendation
• XLink and XPointer are W3C Recommendations
The XML Linking Language (XLink) became a W3C Recommendation 27. June 2001.
The XML Pointer Language (XPointer) became a W3C Recommendation 25. March 2003.

XLink and XPointer Browser Support


The browser support for XLink and XPointer is minimal.
There is some XLink support in Mozilla 0.98+ and Internet Explorer 6.0+. Earlier versions of these browsers
have no XLinks support at all!

XLink and XPointer Syntax


XLink Syntax
In HTML, we know (and all the browsers know!) that the <a> element defines a hyperlink. However, this is
not how it works with XML. In XML documents, you can use whatever element names you want - therefore it
is impossible for browsers to predict what hyperlink elements will be called in XML documents.
The solution for creating links in XML documents was to put a marker on elements that should act as
hyperlinks.

Below is a simple example of how to use XLink to create links in an XML document:
<?xml version="1.0"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
<homepage xlink:type="simple"
xlink:href="http://www.w3schools.com">Visit W3Schools</homepage>
<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>
To get access to the XLink attributes and features we must declare the XLink namespace at the top of the
document.

1
The XLink namespace is:
"http://www.w3.org/1999/xlink".
The xlink:type and the xlink:href attributes in the <homepage> elements define that the type and href
attributes come from the xlink namespace.
The xlink:type="simple" creates a simple, two-ended link (means "click from here to go there"). We will look
at multi-ended (multidirectional) links later.

XPointer Syntax
In HTML, we can create a hyperlink that either points to an HTML page or to a bookmark inside an HTML
page (using #).
Sometimes it is more useful to point to more specific content. For example, let's say that we want to link to
the third item in a particular list, or to the second sentence of the fifth paragraph. This is easy with XPointer.
If the hyperlink points to an XML document, we can add an XPointer part after the URL in the xlink:href
attribute, to navigate (with an XPath expression) to a specific place in the document.

For example, in the example below we use XPointer to point to the fifth item in a list with a unique id
of "rock":
href=http://www.example.com/cdlist.xml#id('rock').child(5,item)

XLink Example
Let's try to learn some basic XLink syntax by looking at an example.

The XML Example Document

Look at the following XML document, "bookstore.xml", that represents a few books:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Harry Potter">
<description
xlink:type="simple"
xlink:href="http://book.com/images/HPotter.gif"
xlink:show="new">
As his fifth year at Hogwarts School of Witchcraft and
Wizardry approaches, 15-year-old Harry Potter is.......
</description>
</book>
<book title="XQuery Kick Start">
<description
xlink:type="simple"
xlink:href="http://book.com/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</description>
</book>
</bookstore>

View the "bookstore.xml" file in your browser


In the example above the XLink namespace is declared at the top of the document
(xmlns:xlink="http://www.w3.org/1999/xlink"). This means that the document has access to the XLink
attributes and features.
The xlink:type="simple" creates a simple "HTML-like" link. You can also specify more complex links
(multidirectional links), but for now, we will only use simple links.
The xlink:href attribute specifies the URL to link to, and the xlink:show attribute specifies where to open the
link. xlink:show="new" means that the link (in this case, an image) should open in a new window.

XLink - Going Further


In the example above we have only demonstrated simple links. XLink is getting more interesting when we
want to access remote locations as resources, instead of standalone pages. The <description> element in
the example above sets the value of the xlink:show attribute to "new". This means that the link should open
in a new window. We could have set the value of the xlink:show attribute to "embed". This means that the

2
resource should be processed inline within the page. When you consider that this could be another XML
document and not just an image, you could, for example, build a hierarchy of XML documents.
With XLink, you can also specify WHEN the resource should appear. This is handled by the xlink:actuate
attribute. xlink:actuate="onLoad" specifies that the resource should be loaded and shown when the
document loads. However, xlink:actuate="onRequest" means that the resource is not read or shown before
the link is clicked. This is very handy for low-bandwidth settings.

XPointer Example
Let's try to learn some basic XPointer syntax by looking at an example.

XPointer Example
In this example, we will show you how to use XPointer in conjunction with XLink to point to a specific part of
another document.
We will start by looking at the target XML document (the document we are going to link to).

The Target XML Document

The target XML document is called "dogbreeds.xml" and it lists a few different dog breeds:
<?xml version="1.0" encoding="ISO-8859-1"?>
<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
<picture url="http://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="http://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>

View the "dogbreeds.xml" file in your browser


Note that the XML document above uses id attributes on each element we may want to link to!
The Linking XML Document
Instead of linking to the entire document (as with XLink), XPointer allows you to link to specific parts of the
document. To link to a specific part of a page, add a number sign (#) and an XPointer expression after the
URL in the xlink:href attributes.
The expression: #xpointer(id("Rottweiler")) refers to the element in the target document, with the id value of
"Rottweiler".
So the xlink:href attribute would look like this:
xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"
However, XPointer allows a shorthand form when linking to an element with an id. You can use the value of
the id directly, like this: xlink:href="http://dog.com/dogbreeds.xml#Rottweiler"

The following XML document refers to information of the dog breed for each of my dogs :-), all
through XLink and XPointer references:
<?xml version="1.0" encoding="ISO-8859-1"?>
<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog xlink:type="simple"
xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
<description xlink:type="simple"
xlink:href="http://myweb.com/mydogs/anton.gif">
Anton is my favorite dog. He has won a lot of.....
</description>
</mydog>
<mydog xlink:type="simple"
xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">

3
<description xlink:type="simple"
xlink:href="http://myweb.com/mydogs/pluto.gif">
Pluto is the sweetest dog on earth......
</description>
</mydog>
</mydogs>

XLink Summary
This tutorial has taught you a standard way of creating hyperlinks in XML documents.
You have learned that linking in XML is divided into two parts: XLink and XPointer.
XLink defines a standard way of creating hyperlinks in XML documents and XPointer allows the hyperlinks
to point to more specific parts (fragments) in the XML document.
For more information on XLink, please look at our XLink Reference.

Now You Know XLink, What's Next?


The next step is to learn about XQuery.
• XQuery
• XQuery is about querying XML data.
• XQuery is designed to query anything that can appear as XML, including databases.

XLink Reference
XLink Attribute Reference
Attribute Value Description
xlink:actuate onLoad Defines when the linked resource is read and shown
onRequest
other
none
xlink:href URL The URL to link to

xlink:show embed Where to open the link. Replace is default


new
replace
other
none
xlink:type simple The type of link
extended
locator
arc
resource
title
none

By: DataIntegratedEntity22592
Source: http://w3schools.com/xlink/default.asp

You might also like