XML XLink and XPointer
HTML has one way to link: <a href="…">. XML has no built-in link element at all — any element in your vocabulary might need to point somewhere. XLink is a W3C standard that lets any element become a link by adding attributes from the XLink namespace. XPointer is its companion for pointing at a specific part of an XML document.
Note: No browser treats XLink in arbitrary XML as clickable links. XLink matters mainly because other formats adopted it — most visibly SVG, where older code used
xlink:href— and in publishing and standards documents. This lesson focuses on reading and understanding it.
Declaring the namespace
XLink attributes live in the namespace http://www.w3.org/1999/xlink, usually with the prefix xlink:
<reading-list xmlns:xlink="http://www.w3.org/1999/xlink">
<book xlink:type="simple"
xlink:href="https://example.com/books/dune"
xlink:title="Dune on Example Books"
xlink:show="new">Dune</book>
<review xlink:type="simple"
xlink:href="reviews.xml#r42"
xlink:show="replace">Read the review</review>
</reading-list>Any element — here <book> and <review> — becomes a link simply by having xlink:type="simple" and an xlink:href.
XLink attributes
| Attribute | Values | Meaning |
|---|---|---|
xlink:type |
simple, extended, locator, arc, resource, title |
Kind of link; simple is like an HTML link |
xlink:href |
URI | Where the link points |
xlink:show |
new, replace, embed, other, none |
Where to show the target (new window, same window, inline) |
xlink:actuate |
onRequest, onLoad, other, none |
When to follow: on click, or automatically on load |
xlink:title |
text | Human-readable description |
xlink:role, xlink:arcrole |
URI | Machine-readable meaning of the link |
show="embed" with actuate="onLoad" behaves like an <img>: fetch the target immediately and place it inline.
Extended links
A simple link joins two things. An extended link can connect many resources and describe the routes ("arcs") between them — even when none of the resources is the document holding the link:
<course xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
<teacher xlink:type="locator" xlink:href="staff.xml#kim" xlink:label="teacher"/>
<student xlink:type="locator" xlink:href="students.xml#s1" xlink:label="student"/>
<student xlink:type="locator" xlink:href="students.xml#s2" xlink:label="student"/>
<teaches xlink:type="arc" xlink:from="teacher" xlink:to="student"/>
</course>Extended links were ambitious and are rarely used in practice, but you may meet them in standards like XBRL (financial reporting).
XPointer
A URI can end with a fragment (#…) that picks out part of the target document. For XML targets, XPointer defines what that fragment means:
| Fragment | Points to |
|---|---|
#chapter3 |
The element whose ID is chapter3 (shorthand pointer) |
#element(chapter3/2) |
The 2nd child element of chapter3 |
#element(/1/4) |
The 4th child of the root element |
#xpointer(//section[@id='intro']) |
An XPath-style selection (rarely supported) |
XLink in SVG
SVG 1.1 used XLink for references between elements. You'll still see it in exported icon files:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="120" height="40">
<defs><circle id="dot" r="10" fill="crimson"/></defs>
<use xlink:href="#dot" x="20" y="20"/>
<use href="#dot" x="60" y="20"/>
</svg>SVG 2 lets you write plain href, which all modern browsers understand; xlink:href still works for backwards compatibility.
Reading XLink attributes from JavaScript uses the namespace-aware DOM methods:
<!DOCTYPE html>
<html>
<body>
<script>
const XLINK = 'http://www.w3.org/1999/xlink';
const xml = `<reading-list xmlns:xlink="${XLINK}">
<book xlink:type="simple" xlink:href="https://example.com/books/dune" xlink:show="new">Dune</book>
<book xlink:type="simple" xlink:href="https://example.com/books/emma" xlink:show="replace">Emma</book>
</reading-list>`;
const doc = new DOMParser().parseFromString(xml, 'application/xml');
for (const b of doc.querySelectorAll('book')) {
console.log(b.textContent, '→', b.getAttributeNS(XLINK, 'href'), `(show=${b.getAttributeNS(XLINK, 'show')})`);
}
</script>
</body>
</html>Test yourself
What turns an arbitrary XML element into an XLink simple link?
- Naming it
<a> - Adding
xlink:type="simple"andxlink:hrefwith the XLink namespace declared - Adding a
linkattribute - Wrapping it in
<xlink>
What does xlink:actuate="onLoad" mean?
- Follow the link when the user clicks
- Follow the link automatically when the document loads
- Never follow the link
- Open the link in a new window