Home » XML Basics » 05 - X Marks the Spot
5

Xlinks and Xpointer

In the previous chapters, you discovered how to insert a web link using codes. In this chapter, you will learn how to use XLink and XPointer as a method of inserting weblinks.

XLink

In the HTML chapter, you learned how to insert a web link using the following code:

<a href = "http://www.mcdonalds.com> ... ... </a>

In XML, you will learn how to insert weblinks using XLink.

The first thing to do is declare XLink capabilities at the beginning of your XML document. Enter the code structure similar to what is shown below:

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

</root_tag>

The next thing to do is to use the following code to actually link to another site or location.

<child_tag xlink:href = "http... ">    

</child_tag>

Try inserting an XLink to any of your previous XML documents like the one below.

 <?xml version = "1.0"?>   <classcard dategiven = "9/27/05" xmlns:xlink = "http://www.w3.org/1999/xlink" >  <name> Sara </name> <subject> programming </subject> <result> <grade> 3.5/4.0 </grade> very industrious and recites a lot </result> <complaints xlink:href = "http://www.mcdonalds.com">  Eats McDonald’s in the middle of class  </complaints>  </classcard>  

When viewed on a browser, it should look something like the image below.


XPointer

The XPointer is basically an extension of XLink. If XLink links you to a location on the web or on your personal computer then XPointer points you to a particular location on a web page. Use the code structure below to go directly to a certain point.

 xlink:href = "http... ...file.xml#xpointer(attribute(... ...))"  

If you want to go to a certain tag then simply enter the attribute and its value in the #xpointer brackets.