XML XLink

创建于 2024-12-03 / 32
字体: [默认] [大] [更大]

XLink 用于在 XML 文档中创建超链接。


XPath
  • XLink 用于在 XML 文档中创建超链接
  • XML 文档中的任何元素都可以充当链接
  • 使用 XLink,可以在链接文件之外定义链接
  • XLink 是 W3C 推荐标准

XLink 浏览器支持

浏览器不支持 XML 文档中的 XLink。

但是,所有主流浏览器都支持 SVG 中的 XLinks


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.w3ccoo.com">访问 W3Schools</homepage>
  <homepage xlink:type="simple" xlink:href="http://www.w3.org">访问 W3C</homepage>
</homepages>

要访问 XLink 功能,我们必须声明 XLink 命名空间。 XLink 命名空间是:"http://www.w3.org/1999/xlink"。

<homepage> 元素中的 xlink:type 和 xlink:href 属性来自 XLink 命名空间。

xlink:type="simple" 创建一个简单的 "HTML-like" 链接(表示 "点击这里去那里")。

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 属性参考

属性 描述
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"。

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>

0 人点赞过