XLink 實例
讓我們通過研究一個實例來學習一些基礎的 XLink 語法。
XML 實例文檔
請看下麵的 XML 文檔,"bookstore.xml",它用來呈現書籍:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Harry Potter">
<description
xlink:type="simple"
xlink:href="http://book.com/images/HPotter.gif"
xlink:show="new">
As his fifth year at Hogwarts School of Witchcraft and
Wizardry approaches, 15-year-old Harry Potter is.......
</description>
</book>
<book title="XQuery Kick Start">
<description
xlink:type="simple"
xlink:href="http://book.com/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</description>
</book>
</bookstore>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Harry Potter">
<description
xlink:type="simple"
xlink:href="http://book.com/images/HPotter.gif"
xlink:show="new">
As his fifth year at Hogwarts School of Witchcraft and
Wizardry approaches, 15-year-old Harry Potter is.......
</description>
</book>
<book title="XQuery Kick Start">
<description
xlink:type="simple"
xlink:href="http://book.com/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</description>
</book>
</bookstore>
在您的流覽器查看 "bookstore.xml" bookstore.xml檔。
在上面的例子中,XLink 文檔命名空間(xmlns:xlink="http://www.w3.org/1999/xlink")被聲明於文檔的頂部。 這意味著文檔可訪問 XLink 的屬性和特性。
xlink:type="simple" 可創建簡單的類似 HTML 的鏈接。您也可以規定更多的複雜的鏈接(多方向鏈接),但是目前,我們僅使用簡易鏈接。
xlink:href 屬性規定了要鏈接的 URL,而 xlink:show 屬性規定了在何處打開鏈接。xlink:show="new" 意味著鏈接(在此例中,是一幅圖像)會在新窗口打開。
XLink - 深入學習
在上面的例子中,我們只展示了簡單的鏈接。當我們要訪問遠程位置的資源,而不是獨立的頁面時,XLink是變得更有趣。在上面的例子<description>元素集的XLINK屬性顯示的值為:"new"。這意味著,應該在新窗口打開鏈接。我們可以設置XLINK中的值:顯示屬性"embed"。這意味著資源應嵌入到頁面處理。你認為這可能是另一個XML文檔,而不是只是一個圖像,你可以建立一個XML文檔中層次結構的例子。
使用XLink,你還可以指定資源時才顯示。這是由XLink的actuate屬性處理。XLINK:actuate"="onLoad"指定的資源檔應加載和顯示。XLINK:actuate="onRequest"意味著鏈接被點擊之前無法讀取或顯示資源。這對低帶寬設置非常方便。