Latest web development tutorials

XPointer Examples

Let's study an example to learn some basic XPointer syntax.


XPointer Examples

In this example, we'll show you how to use XPointer and XLink to point to some other combination of specific parts of a document.

We will begin by studying the target XML document (that is, we want to link to that document).


Target XML document

Target XML document named "dogbreeds.xml", which lists a number of different types of dogs:

<?xml version="1.0" encoding="ISO-8859-1"?>

<dogbreeds>

<dog breed="Rottweiler" id="Rottweiler">
<picture url="../../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="../../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>

See your browser "dogbreeds.xml" file .

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


XML document links

Can be linked to more than the entire document (when using XLink), XPointer allows you to link to a specific part of the document. To link to a specific part of the page, in xlink: href attribute in the URL after adding a pound sign (#) and an XPointer expression.

Expression: #xpointer (id ( "Rottweiler" )) can refer to the target document id value of "Rottweiler" elements.

Therefore, xlink: href attribute like this: xlink: href = "http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler ' ))"

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

The following XML document can be referenced each dog breed information are referenced by the XLink and XPointer:

<?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">
<description xlink:type="simple"
xlink:href="http://myweb.com/mydogs/pluto.gif">
Pluto is the sweetest dog on earth......
</description>
</mydog>

</mydogs>