XML、XLink 和 XQuery

XLink 用于在 XML 文檔中創建超鏈接。

XPath

  • XLink 用于在 XML 文檔中創建超鏈接
  • XML 文檔中的任何元素都可以充當鏈接
  • 通過 XLink,可以在被鏈接的文件外部定義鏈接
  • XLink 是 W3C 推薦標準

XLink 瀏覽器支持

瀏覽器不支持 XML 文檔中的 XLink。

但是,所有主流瀏覽器都支持 SVG 中的 XLink

XLink 語法

在 HTML 中,<a> 元素定義超鏈接。然而,這不是它在 XML 中的工作方式。

在 XML 文檔中,您可以使用任何您想要的元素名稱 - 因此瀏覽器無法預測 XML 文檔中將調用哪些鏈接元素。

下面是如何使用 XLink 在 XML 文檔中創建鏈接的簡單示例:

<?xml version="1.0" encoding="UTF-8"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
  <homepage xlink:type="simple" xlink:href="https://www.codew3c.com">Visit CodeW3C.coms</homepage>
  <homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>

如果我們需要訪問 XLink 功能,則必須聲明 XLink 命名空間。XLink 命名空間是:“http://www.w3.org/1999/xlink”。

<homepage> 元素中的 xlink:type 和 xlink:href 屬性來自這個 XLink 命名空間。

xlink:type="simple" 創建一個簡單的“類似 HTML 的”鏈接(意思是“單擊此處轉到別處”)。

xlink:href 屬性指定要鏈接到的 URL。

XLink 實例

下面的 XML 文檔包含 XLink 功能:

<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="雅舍談吃">
  <description
  xlink:type="simple"
  xlink:href="/images/cswd.jpg"
  xlink:show="new">
  《雅舍談吃》堪稱潮汕美食的指南書,其中為美食愛好者和尋味者提供了所需要的指導和幫助;
  這本書又可說是潮汕美食的人文讀本,書中介紹的是潮汕美食而非潮汕飲食,充分地將食物背后所隱藏的鄉土文化、飲食習俗和歷史細節挖掘展現出來。
  </description>
</book>
<book title="了不起的狐貍爸爸">
  <description
  xlink:type="simple"
  xlink:href="/images/mrfox.jpg"
  xlink:show="new">
  博吉斯、邦斯和比恩是你所能遇到的最卑鄙、最小氣的人,他們憎恨狐貍先生,決定把他全家除掉。
  于是,這三個人拿著槍,陰險地等在狐貍先生的洞外……但狐貍先生自有對付他們的妙計!
  于是,一場智慧和力量的較量開始了……
  </description>
</book>
</bookstore>

例子解釋:

  1. XLink 命名空間在文檔頂部聲明 (xmlns:xlink="http://www.w3.org/1999/xlink")
  2. xlink:type="simple" 創建一個簡單的“類似 HTML”的鏈接
  3. xlink:href 屬性指定要鏈接到的 URL(在本例中為圖像)
  4. xlink:show="new" 指示鏈接應在新窗口中打開

XLink - 走得更遠

在上面的例子中,我們演示了簡單的 XLink。

如果我們將遠程的位置作為資源而不是獨立頁面訪問時,XLink 將變得更加有趣。

如果我們將 xlink:show 屬性值設置為 "embed",則被鏈接的資源應在頁面內內聯處理。例如,如果您認為這可能是另一個 XML 文檔,則可以構建 XML 文檔的層次結構。

您還可以使用 xlink:actuate 屬性指定資源應出現的時間。

XLink 屬性參考

屬性 值 描述 xlink:actuate onLoad onRequest other none 定義何時讀取并顯示被鏈接的資源: onLoad - 資源應該在文檔加載時加載并顯示 onRequest - 單擊鏈接之前不會讀取或顯示資源 xlink:href URL 規定要鏈接到的 URL。 xlink:show embed new replace other none 規定在哪里打開鏈接。默認為 "replace"。 xlink:type simple extended locator arc resource title none 規定鏈接的類型。

XPointer

XPath
  • XPointer 允許鏈接指向 XML 文檔的特定部分
  • XPointer 使用 XPath 表達式在 XML 文檔中導航
  • XPointer 是 W3C 推薦標準

XPointer 瀏覽器支持

沒有瀏覽器支持 XPointer。但 XPointer 也用于其他 XML 語言。

XPointer 實例

在這個例子中,我們將結合使用 XPointer 和 XLink,指向另一個文檔中的特定部分。

我們首先看一下這個目標 XML 文檔(我們鏈接到的文檔):

<?xml version="1.0" encoding="UTF-8"?>
<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
  <picture url="https://dog.com/rottweiler.gif" />
  <history>The Rottweiler's ancestors were probably Roman
  drover dogs.....</history>
  <temperament>Confident, bold, alert and imposing, the Rottweiler
  is a popular choice for its ability to protect....</temperament>
</dog>
<dog breed="FCRetriever" id="FCRetriever">
  <picture url="https://dog.com/fcretriever.gif" />
  <history>One of the earliest uses of retrieving dogs was to
  help fishermen retrieve fish from the water....</history>
  <temperament>The flat-coated retriever is a sweet, exuberant,
  lively dog that loves to play and retrieve....</temperament>
</dog>
</dogbreeds>

請注意,上面的 XML 文檔在每個元素上都使用了 id 屬性!

因此,XPointer 允許您鏈接到文檔的特定部分,而不是鏈接到整個文檔(如 XLink)。

如需鏈接到頁面的特定部分,請在 xlink:href 屬性中的 URL 后面添加井號 (#) 和 XPointer 表達式,如下所示:

xlink:href="https://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"

該表達式引用目標文檔中的 id 值為 "Rottweiler" 的元素。

xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('Rottweiler'))"。

XPointer 還允許使用簡寫方法鏈接到帶 id 的元素。您可以直接使用 id 的值,如下所示:

xlink:href="https://dog.com/dogbreeds.xml#Rottweiler

下面的 XML 文檔包含的的鏈接指向了有關每只狗的品種的更多信息:

<?xml version="1.0" encoding="UTF-8"?>
<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog>
  <description>
  Anton is my favorite dog. He has won a lot of.....
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#Rottweiler">
  Fact about Rottweiler
  </fact>
</mydog>
<mydog>
  <description>
  Pluto is the sweetest dog on earth......
  </description>
  <fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#FCRetriever">
  Fact about flat-coated Retriever
  </fact>
</mydog>
</mydogs>