目录

XML 、XLink 和 XPointer


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.91xjr.com">Visit 91xjr</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-like" 链接(表示"click here to go there")。

xlink:href 属性指定要链接到的 URL。


XLink 示例

以下 XML 文档包含 XLink 功能:

<?xml version="1.0" encoding="UTF-8"?>

<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">

<book title="Harry Potter">
  <description
  xlink:type="simple"
  xlink:href="/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="/images/XQuery.gif"
  xlink:show="new">
  XQuery Kick Start delivers a concise introduction
  to the XQuery standard.......
  </description>
</book>

</bookstore>

示例解释:

  • XLink 命名空间在文档顶部声明 (xmlns:xlink="http://www.w3.org/1999/xlink")
  • xlink:type="simple" 创建一个简单的"HTML-like" 链接
  • xlink:href 属性指定要链接到的 URL(在本例中为图片)
  • xlink:show="new" 指定链接应在新窗口中打开


XLink - 走得更远

在上面的示例中,我们演示了简单的 XLink。当将远程位置作为资源而不是独立页面访问时,XLink 变得更加有趣。

如果我们将 xlink:show 属性的值设置为 "embed",则应在页面内内联处理链接的资源。例如,当您认为这可能是另一个 XML 文档时,您可以构建 XML 文档的层次结构。

您还可以使用 xlink:actuate 属性指定资源应出现的时间。


XLink 属性参考

Attribute Value Description
xlink:actuate onLoad
onRequest
other
none
Defines when the linked resource is read and shown:
  • onLoad - the resource should be loaded and shown when the document loads
  • onRequest - the resource is not read or shown before the link is clicked
xlink:href URL Specifies the URL to link to
xlink:show embed
new
replace
other
none
Specifies where to open the link. Default is "replace"
xlink:type simple
extended
locator
arc
resource
title
none
Specifies the type of link

X指针

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"。

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>