XLink 是一套以屬性為基礎的語法,用來在 XML 文件中加入連結能力。它比 HTML 的 <a> 強得多——連結可以是單向的,也可以是雙向的,讓文件在兩個方向上互相參照,既能從 A 到 B,也能從 B 到 A。
每個 XLink 元素都必須具備一個 xlink:type 屬性,用來指出連結類型;而 xlink:href 屬性則指向被連結資源的 URI。
一個最簡單的連結:
<test xmlns:xlink="https://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="https://www.ringkee.com/xml.html">
<author>Jims</author>
<date>2005/02/18</date>
</test>
xlink:type 共有六種取值:simple、extended、locator、arc、title、resource。
這個屬性告訴瀏覽器或應用程式,使用者觸發連結後應該如何呈現內容,共五種動作:
new:在新視窗中顯示連結內容。replace:在目前視窗中顯示連結內容。embed:在該連結元素所在的位置直接嵌入內容。other:動作不確定,交由應用程式自行決定。none:不指定任何動作。這個屬性決定連結內容的載入時機,共四種取值:
onLoad:一發現連結就立即載入顯示。onRequest:使用者提出請求時才載入。other:由文件中的其他標記(而非 xlink)決定時機。none:不指定。<test xmlns:xlink="https://www.w3.org/1999/xlink"
xlink:type="simple"
xlink:href="https://www.ringkee.com/xml.html"
xlink:actuate="onRequest"
xlink:show="replace">
<author>Jims</author>
<date>2005/02/18</date>
</test>
onRequest 加 replace 的組合,行為就和傳統超連結幾乎一致:點擊才載入,並在目前視窗開啟。
<image xlink:type="simple"
xlink:actuate="onLoad"
xlink:show="embed"
xlink:href="https://www.ringkee.com/flower.png"
width="320" height="240" />
這裡改成 onLoad 加 embed,頁面一載入就把圖片嵌進來。
xlink:actuate 與 xlink:show 都是可選的,不寫就使用預設行為。另外還有兩個描述性屬性:
xlink:title:包含一小段描述遠端資源的文字。xlink:role:包含一個 URI,指向該資源較長篇的描述。使用 XLink 時別忘了在根元素上宣告 xmlns:xlink 命名空間,否則前綴無法解析。