XPointer Example
- Previous Page XLink Example
- Next Page XLink Summary
Let's learn some basic XPointer syntax by studying an example.
XPointer Example
In this example, we will show you how to use XPointer in conjunction with XLink to point to a specific part of another document.
We will begin by studying the target XML document (i.e., the document we are linking to).
Target XML document
The target XML document is named "dogbreeds.xml", which lists some different dog breeds:
<?xml version="1.0" encoding="ISO-8859-1"?> <dogbreeds> <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> <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, a lively dog that loves to play and retrieve.... </temperament> </dog> </dogbreeds>
View the "dogbreeds.xml" file in your browser.
Note:The above XML document uses the id attribute on each element we need to link!
XML Linking Document
Not only can it link to the entire document (when using XLink), XPointer allows you to link to a specific part of the document. If you want to link to a specific part of the page, add a hash (#) and an XPointer expression after the URL in the xlink:href attribute.
Expression:#xpointer(id("Rottweiler")) It can refer to the element with the id value "Rottweiler" in the target document.
Therefore, the xlink:href attribute will be similar to this:xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"
However, when linking to an element using an id, XPointer allows a shorthand form. You can directly use the value of the id, like this:xlink:href="http://dog.com/dogbreeds.xml#Rottweiler".
The following XML document can refer to the breed information of each dog, all referenced by 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>
- Previous Page XLink Example
- Next Page XLink Summary