XLink 定义了一套标准的在 XML 文档中创建超级链接的方法。
XPointer 使超级链接可以指向 XML 文档中更多具体的部分(片断)。
在继续学习之前,您需要对以下知识点有基本的了解:
如果您希望首先学习这些项目,请在我们对 首页 访问这些教程。
在 HTML 中,我们知道 <a> 元素可定义超级链接。不过 XML 不是这样工作的。在 XML 文档中,您可以使用任何你需要的名称 - 因此对于浏览器来说是无法预知在 XML 文档中可调用何种超级链接元素。
在 XML 文档中定义超级链接的方法是在元素上放置可用作超级链接的标记。
下面是在 XML 文档中使用 XLink 来创建链接的简单实例:
</>code
- <?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" 可创建一个简单的两端链接(意思是“从这里到哪里”)。稍后我们会研究多端链接(多方向)。
在 HTML 中,我们可创建一个既指向某个 HTML 页面又指向 HTML 页面内某个书签的超级链接(使用#)。
有时,可指向更多具体的内容会更有好处。举例,我们需要指向某个特定的列表的第三个项目,或者指向第五段的第二行。通过 XPointer 是很容易做到的。
假如超级链接指向某个 XML 文档,我们可以在 xlink:href 属性中把 XPointer 部分添加到 URL 后面,这样就可以导航(通过 XPath 表达式)到文档中某个具体的位置了。
举例,在下面的例子中,我们通过唯一的 id “rock” 使用 XPointer 指向某个列表中的第五个项目。
</>code
- href="http://www.example.com/cdlist.xml
#id('rock').child(5,item)
"
请看下面的 XML 文档,"bookstore.xml",它用来呈现书籍:
</>code
- <?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>
在上面的例子中,XLink 文档命名空间被声明于文档的顶部:
</>code
- xmlns:xlink="http://www.w3.org/1999/xlink"
这意味着文档可访问 XLink 的属性和特性。
xlink:type="simple" 可创建简单的类似 HTML 的链接。您也可以规定更多的复杂的链接(多方向链接),但是目前,我们仅使用简易链接。
xlink:href 属性规定了要链接的 URL,而 xlink:show 属性规定了在何处打开链接。xlink:show="new" 意味着链接(在此例中,是一幅图像)会在新窗口打开。
在本例中,我们会为您展示如何使用 XPointer 并结合 XLink 来指向另外一个文档的某个具体的部分。
我们将通过研究目标 XML 文档开始(即我们要链接的那个文档)。
目标XML文档名为 "dogbreeds.xml",它列出了一些不同的狗种类:
</>code
- <?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>
注释:上面的 XML 文档在每个我们需要链接的元素上使用了 id 属性!
不止能够链接到整个文档(当使用 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 来引用:
</>code
- <?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>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛