您现在的位置: 365建站网 > 建站教程 > XML DOM 教程 > XPointer 简介

XPointer 简介

此节有 254 人学习过     参与评论

XLink 定义了一套标准的在 XML 文档中创建超级链接的方法。

XPointer 使超级链接可以指向 XML 文档中更多具体的部分(片断)。

您应当具备的基础知识

在继续学习之前,您需要对以下知识点有基本的了解:

  • HTML / XHTML
  • XML / XML 命名空间
  • XPath

如果您希望首先学习这些项目,请在我们对 首页 访问这些教程。

什么是 XLink?

  • XLink 是 XML 链接语言(XML Linking Language)的缩写
  • XLink 是用于在 XML 文档中创建超级链接的语言
  • XLink 类似于 HTML 链接 - 但是更为强大
  • XML 文档中的任何元素均可成为 XLink
  • XLink 支持简易链接,也支持可将多重资源链接在一起的扩展链接
  • 通过 XLink,链接可在被链接文件外进行定义
  • XLink 是 W3C 推荐标准

XLink 语法

在 HTML 中,我们知道 <a> 元素可定义超级链接。不过 XML 不是这样工作的。在 XML 文档中,您可以使用任何你需要的名称 - 因此对于浏览器来说是无法预知在 XML 文档中可调用何种超级链接元素。

在 XML 文档中定义超级链接的方法是在元素上放置可用作超级链接的标记。

下面是在 XML 文档中使用 XLink 来创建链接的简单实例:

<?xml version="1.0"?>

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

  <homepage xlink:type="simple"
  xlink:href="https://www.365jz.com">Visit W3School</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 命名空间的 type 和 href 属性。

xlink:type="simple" 可创建一个简单的两端链接(意思是“从这里到哪里”)。稍后我们会研究多端链接(多方向)。

XPointer 语法

在 HTML 中,我们可创建一个既指向某个 HTML 页面又指向 HTML 页面内某个书签的超级链接(使用#)。

有时,可指向更多具体的内容会更有好处。举例,我们需要指向某个特定的列表的第三个项目,或者指向第五段的第二行。通过 XPointer 是很容易做到的。

假如超级链接指向某个 XML 文档,我们可以在 xlink:href 属性中把 XPointer 部分添加到 URL 后面,这样就可以导航(通过 XPath 表达式)到文档中某个具体的位置了。

举例,在下面的例子中,我们通过唯一的 id “rock” 使用 XPointer 指向某个列表中的第五个项目。

href="http://www.example.com/cdlist.xml#id('rock').child(5,item)"

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.xml" 文件

在上面的例子中,XLink 文档命名空间被声明于文档的顶部:

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

这意味着文档可访问 XLink 的属性和特性。

xlink:type="simple" 可创建简单的类似 HTML 的链接。您也可以规定更多的复杂的链接(多方向链接),但是目前,我们仅使用简易链接。

xlink:href 属性规定了要链接的 URL,而 xlink:show 属性规定了在何处打开链接。xlink:show="new" 意味着链接(在此例中,是一幅图像)会在新窗口打开。


XPointer 实例

在本例中,我们会为您展示如何使用 XPointer 并结合 XLink 来指向另外一个文档的某个具体的部分。

我们将通过研究目标 XML 文档开始(即我们要链接的那个文档)。

目标XML文档

目标XML文档名为 "dogbreeds.xml",它列出了一些不同的狗种类:

<?xml version="1.0" encoding="ISO-8859-1"?>

<dogbreeds>

<dog breed="Rottweiler" id="Rottweiler">
  <picture url="http://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="http://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>

在您的浏览器查看 "dogbreeds.xml" 文件

注释:上面的 XML 文档在每个我们需要链接的元素上使用了 id 属性!

XML 链接文档

不止能够链接到整个文档(当使用 XLink 时),XPointer 允许您链接到文档的特定部分。如需链接到页面的某个具体的部分,请在 xlink:href 属性中的 URL 后添加一个井号 (#) 以及一个 XPointer 表达式。

表达式:#xpointer(id("Rottweiler")) 可引用目标文档中 id 值为 "Rottweiler" 的元素。

因此,xlink:href 属性会类似这样:xlink:href="http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))"

不过,当使用 id 链接到某个元素时,XPointer 允许简写形式。您可以直接使用 id 的值,就像这样:xlink:href="http://dog.com/dogbreeds.xml#Rottweiler"

下面的 XML 文档可引用每条狗的品种信息,均通过 XLink 和 XPointer 来引用:

<?xml version="1.0" encoding="ISO-8859-1"?>

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

<mydog xlink:type="simple"
  xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
  <description xlink:type="simple"
  xlink:href="http://myweb.com/mydogs/anton.gif">
  Anton is my favorite dog. He has won a lot of.....
  </description>
</mydog>

<mydog xlink:type="simple"
  xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">
  <description xlink:type="simple"
  xlink:href="http://myweb.com/mydogs/pluto.gif">
  Pluto is the sweetest dog on earth......
  </description>
</mydog>

</mydogs>

如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (254人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------
XML DOM 教程目录

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号