XLink Example


Let's learn some basic XLink syntax by studying an example.


XML Example Document

Look at the following XML document, "bookstore.xml", which is used to present 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 "bookstore.xml" in your browserbookstore.xmlfile.

In the above example, the XLink document namespace (xmlns:xlink="http://www.w3.org/1999/xlink") is declared at the top of the document. This means the document can access XLink's attributes and features.

xlink:type="simple" can create simple HTML-like links. You can also specify more complex links (multi-directional links), but for now, we 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) will open in a new window.


XLink - In-depth Learning

In the above example, we only demonstrated simple links. XLink becomes more interesting when we want to access resources at remote locations, rather than standalone pages. In the above example, the value of the XLink attribute set on the <description> element is "new". This means the link should be opened in a new window. We can set the value of the XLink show attribute to "embed". This means the resource should be embedded in the page. If you think it could be another XML document rather than just an image, you can build an example of a hierarchical structure in an XML document.

Using XLink, you can also specify when the resource is displayed. This is handled by the XLink actuate attribute. xlink:actuate="onLoad" specifies that the resource file should be loaded and displayed. xlink:actuate="onRequest" means that the resource cannot be read or displayed until the link is clicked. This is very convenient for low-bandwidth settings.

Other Extensions