当前位置: 首页 > 网络学院 > XML相关教程 > XLink/XPointer > XPointer 实例
Let's try to learn some basic XPointer syntax by looking at an example.
讲了这么多,还是让我们通过一些实例来学习一些基本的XPointer语法吧。
In this example, we will show you how to use XPointer in conjunction with XLink to point to a specific part of another document.
在这个实例中,我们会给你展示如何通过XPointer与XLink来指向其它文档中的一段具体部分。
We will start by looking at the target XML document (the document we are going to link to).
我们会通过查阅目标XML文件(我们准备链接的文档)开始我们的学习。
The target XML document is called "dogbreeds.xml" and it lists a few different dog breeds:
目标XML文档名为“dogbreeds.xml”,它列举了一些狗的种类:
<?xml version="1.0" encoding="ISO-8859-1"?> <dogbreeds> <dog breed="Rottweiler" id="Rottweiler"> <dog breed="FCRetriever" id="FCRetriever"> </dogbreeds> |
View the "dogbreeds.xml" file in your browser
在你的浏览器中查阅“dogbreeds.xml”的文件
Note that the XML document above uses id attributes on each element we may want to link to!
注意上面的XML文档,它在每个我们希望链接的元素上使用了ID属性!
Instead of linking to the entire document (as with XLink), XPointer allows you to link to specific parts of the document. To link to a specific part of a page, add a number sign (#) and an XPointer expression after the URL in the xlink:href attributes.
与XLink链接到完整的文档不同的是,XPointer允许你链接到文档中的具体指定的部分。如果你希望链接到页面的某个精确部分,就需要在 xlink:href 属性的URI中添加一个“#”和“XPointer”表达式。
The expression: #xpointer(id("Rottweiler")) refers to the element in the target document, with the id value of "Rottweiler".
表达式:#xpointer(id("Rottweiler")) 通过 "Rottweiler" 的ID值引用了目标文档中元素。
So the xlink:href attribute would look like this: xlink:href=http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))
因此,xlink:href 的属性如下:xlink:href=http://dog.com/dogbreeds.xml#xpointer(id('Rottweiler'))
However, XPointer allows a shorthand form when linking to an element with an id. You can use the value of the id directly, like this: xlink:href=http://dog.com/dogbreeds.xml#Rottweiler
当然,当链接到一个包含ID的元素时,XPointer允许使用缩写格式。你可以直接使用ID值,如:xlink:href=http://dog.com/dogbreeds.xml#Rottweiler
The following XML document refers to information of the dog breed for each of my dogs :-), all through XLink and XPointer references:
下述XML文档通过XLink和XPointer参数引用了我的每只狗的种类:
<?xml version="1.0" encoding="ISO-8859-1"?> <mydogs xmlns:xlink="http://www.w3.org/1999/xlink"> <mydog xlink:type="simple" <mydog xlink:type="simple" </mydogs> |