XML - XPointer

XPointer (the XML Pointer language) allows hyperlinks to point to specific parts (fragments) of XML documents.

What is XPointer?

  • 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

XPointer Browser Support

There is no browser support for XPointer. But XPointer is used in other XML languages.

XPointer Example

In this example, we will 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 linking to):

<?xml version="1.0" encoding="UTF-8"?>


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


Note that the XML document above uses id attributes on each element!

So, 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 attribute, like this: xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))". The expression refers to the element in the target document, with the id value of "Rottweiler".

XPointer also allows a shorthand method for 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 contains links to more information of the dog breed for each of my dogs:

<?xml version="1.0" encoding="UTF-8"?>

<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">

  Anton is my favorite dog. He has won a lot of.....
  <fact xlink:type="simple" xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
  Fact about Rottweiler

  Pluto is the sweetest dog on earth......
  <fact xlink:type="simple" xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">
  Fact about flat-coated Retriever





Search w3schools.com:

Translate w3schools.com: