Thursday, June 13, 2013

xLink and XPointer

What is XLink?

XQuery
  • XLink is short for XML Linking Language
  • XLink is used to create hyperlinks in XML documents
  • Any element in an XML document can behave as a link
  • XLink supports simple links (like HTML) and extended links (for linking multiple resources together)
  • With XLink, the links can be defined outside the linked files
  • XLink is a W3C Recommendation



What is XPointer?

XQuery
  • XPointer is short for XML Pointer Language
  • XPointer allows the links to point to specific parts of an XML document
  • XPointer uses XPath expressions to navigate in the XML document
  • XPointer is a W3C Recommendation



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 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.
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)"

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>
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.


No comments:

Post a Comment