Syntax of XLink and XPointer
- Previous Page Introduction to XLink
- Next Page XLink Example
XLink Syntax
In HTML, we know that the <a> element can define hyperlinks. However, XML does not work this way. In an XML document, you can use any name you need - therefore, browsers cannot predict what hyperlink elements can be called in an XML document.
The method to define hyperlinks in an XML document is to place a mark that can be used as a hyperlink on the element.
This is a simple example of using XLink to create links in an XML document:
<?xml version="1.0"?> <homepages xmlns:xlink="http://www.w3.org/1999/xlink"> <homepage xlink:type="simple" xlink:href="http://www.codew3c.com">Visit CodeW3C.com</homepage> <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</homepage> </homepages>
To access the attributes and features of XLink, we must declare the XLink namespace at the top of the document.
The namespace of XLink is: "http://www.w3.org/1999/xlink".
The xlink:type and xlink:href attributes in the <homepage> element define the type and href attributes from the XLink namespace.
xlink:type="simple" can create a simple two-way link (meaning 'from here to there'). We will study multi-way links (multi-directional) later.
XPointer Syntax
In HTML, we can create a hyperlink that points to both an HTML page and a bookmark within an HTML page (using #).
Sometimes, it is more beneficial to point to more specific content. For example, we need to point to the third item in a specific list, or point to the second line of the fifth paragraph. This can be easily done with XPointer.
If a hyperlink points to an XML document, we can add the XPointer part to the URL in the xlink:href attribute, so we can navigate (via XPath expressions) to a specific location in the document.
For example, in the following example, we use the unique id 'rock' with XPointer to point to the fifth item in a list.
href="http://www.example.com/cdlist.xml#id('rock').child(5,item)"
- Previous Page Introduction to XLink
- Next Page XLink Example