Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XPointer example

XPointer example


Let us learn some basic XPointer syntax by studying an example.


XPointer example

In this example we will show you how to use XPointer together with XLink to point to a specific part of another document.

We will start by examining the target XML document (the document we want to link to).


Target XML document

The target XML document is called "dogbreeds.xml" and it lists several different dog breeds:

 

  The Rottweiler's ancestors were probably Roman

  drover dogs.....

  Confident, bold, alert and imposing, the Rottweiler

  is a popular choice for its ability to protect....

 

  One of the earliest uses of retrieving dogs was to

  help fishermen retrieve fish from the water....

  The flat-coated retriever is a sweet, exuberant,

  lively dog that loves to play and retrieve....

View in your browser "dogbreeds.xml" file.

Notice that the XML document above uses the id attribute on every element we want to link to!


XML linking document

Beyond linking to an entire document (when using XLink), XPointer lets you link to a specific part of a document. To link to a specific part of a page, add a hash (#) and an XPointer expression after the URL in the xlink:href attribute.

The expression:#xpointer(id("Rottweiler")) can reference the element whose id is "Rottweiler" in the target document.

So the xlink:href attribute would look like this:xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"

However, when linking to an element by id, XPointer allows a shorthand form. You can use the id value directly, like this:xlink:href="http://dog.com/dogbreeds.xml#Rottweiler".

The following XML document can reference the breed information of each dog, all through XLink and XPointer:

  xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">

 

  xlink:href="http://myweb.com/mydogs/anton.gif">

  Anton is my favorite dog. He has won a lot of.....

 

  xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">

 

  xlink:href="http://myweb.com/mydogs/pluto.gif">

  Pluto is the sweetest dog on earth......