XML - XPointer
XPointer allows hyperlinks to point to specific parts (fragments) of XML documents.
What is XPointer?
In HTML, we can create a hyperlink that either points to an HTML page or to a bookmark inside an HTML page (using #).
Sometimes it is more useful to point to more specific content. For example, let's say that we want to link to the third item in a particular list, or to the second sentence of the fifth paragraph. This is easy with XPointer.
If the hyperlink points to an XML document, we can add an XPointer part after the URL in the xlink:href attribute, to navigate (with an XPath expression) to a specific place in the document.
For example, in the example below we use XPointer to point to the fifth item in a list with a unique id of "rock":
XPointer is a W3C Recommendation
The XML Pointer Language (XPointer) became a W3C Recommendation 25. March 2003.
XPointer Browser Support
There is no browser support for XPointer. But XPointer is used in other XML languages.
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 start by looking at the target XML document (the document we are going to link to).
A Target XML Document
This target XML document is called "dogbreeds.xml" and it lists a few different dog breeds:
<dog breed="Rottweiler" id="Rottweiler">
<picture url="http://dog.com/rottweiler.gif" />
<history>The Rottweiler's ancestors were probably Roman
<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 we may want to link to!
The Linking XML Document
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 attributes.
The expression: #xpointer(id("Rottweiler")) refers to the element in the target document, with the id value of "Rottweiler".
So the xlink:href attribute would look like this: xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"
However, XPointer allows a shorthand form when 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 refers to information of the dog breed for each of my dogs :-), all through XLink and XPointer references:
Anton is my favorite dog. He has won a lot of.....
Pluto is the sweetest dog on earth......
Thank You For Helping Us!
Your message has been sent to W3Schools.