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.w3school.com.cn">Visit W3Schools</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>