XLink和XPointer的介绍
XLink defines a standard way of creating hyperlinks in XML documents.
XLink 是定义XML文档超文本连接的标准方法.
XPointer allows the hyperlinks to point to more specific parts (fragments) in the XML document.
XPointer 允许在XML文档中指向更为明确的部分.
What You Should Already Know
你应该具备哪些知识
Before you continue you should have a basic understanding of the following:
在你继续学习之前你应该有下面这些知识点的基础
- HTML / XHTML
- XML / XML Namespaces
- XPath
What is XLink?
什么是XLink?
- XLink is short for the XML Linking Language
XLink是XML连接语言的缩写 - XLink is a language for creating hyperlinks in XML documents
XLink是一种能在XML文档中建立超文本连接的语言 - XLink is similar to HTML links - but it is a lot more powerful
XLink与HTML的连接比较类似 - 但它更为有力 - ANY element in an XML document can behave as an XLink
在XML文档中的任何元素都可以表现为一个XLink - XLink supports simple links (like HTML) and extended links (for linking multiple resources together)
XLink支持简单的连接(就像HTML)和大范围的连接(多个资源连接的集合) - With XLink, the links can be defined outside of the linked files
使用XLink,可以在已经被连接的文件外定义连接 - XLink is a W3C Recommendation
XLink是W3C标准
What is XPointer?
什么是XPointer?
- XPointer is short for the XML Pointer Language
XPointer是XML指标语言的缩写 - XPointer allows the hyperlinks to point to specific parts of the XML document
XPointer可以让超文本连接指向XML文档中特殊的部分 - XPointer uses XPath expressions to navigate in the XML document
XPointer使用XPath表达式来导航XML文档(这里的导航其实就是有方向性的到达某一区域内的某一特定部位) - XPointer is a W3C Recommendation
Xpointer也是W3C标准
XLink and XPointer Browser Support
XLink和XPointer浏览器的支持
The browser support for XLink and XPointer is minimal.
浏览器只能支持它们的地方很少
There is some XLink support in Mozilla 0.98+, Netscape 6.02+, and Internet Explorer 6.0. Earlier versions of all of these browsers have no XLinks support at all!
在Mozilla 0.98+, Netscape 6.02+, 和 Internet Explorer 6.0以及早期版本中有部分对XLink支持,但这些浏览器没有一个完全支持XLink的。
XLink Syntax
XLink语法
In HTML, we know (and all the browsers know!) that the <a> element defines a hyperlink. However, this is not how it works with XML. In XML documents, you can use whatever element names you want - therefore it is impossible for browsers to predict what hyperlink elements will be called in XML documents
在HTML中我们知道(所有浏览器也明白!)<a>元素定义了超连接。然而在XML里并不是这样运作的。在XML文档里你可以使用任何你想使用的元素名称 - 因此浏览器是无法预料什么样的超连接元素会被使用到。
The solution for creating links in XML documents was to put a marker on elements that should act as hyperlinks.
解决在XML文档中建立连接的方法是加入扮演超连接元素的标记。
Below is a simple example of how to use XLink to create links in an XML document:
下面是一个简单的举例来演示怎样使用XLink来建立XML文档中的连接:
<?xml version="1.0"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
<homepage xlink:type="simple" xlink:href="http://www.w3schools.com">Visit W3Schools</homepage>
<homepage xlink:type="simple" xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>
To get access to the XLink attributes and features we must declare the XLink namespace at the top of the document.
要得到使用XLink的属性和特征我们必须在文档的顶部声明XLink的命名空间
The XLink namespace is: "http://www.w3.org/1999/xlink".
XLink命名空间为:"http://www.w3.org/1999/xlink"
The xlink:type and the xlink:href attributes in the <homepage> elements define that the type and href attributes come from the xlink namespace.
在<homepage>元素中xlink:type和xlink:href属性定义的类型和href属性都来自于xlink命名空间
The xlink:type="simple" creates a simple, two-ended link (means "click from here to go there"). We will look at multi-ended (multidirectional) links later.
xlink:type="simple"建立了一个简单的,两点连接(意思就是点击后从这到那)。等下我们将观察多点的(多方向的)连接
XPointer Syntax
XPointer语法
In HTML, we can create a hyperlink that either points to an HTML page or to a bookmark inside an HTML page (using #).
在HTML中我们可以建立连接可以到达HTML页面中的书签位置(使用#)
Sometimes it is more useful to point to more specific content. For example, let's say that we want to link to the third item in a particular list, or to the second sentence of the fifth paragraph. This is easy with XPointer.
有些时候指向特殊内容是很有帮助的。举个例子,我们要连接到练习表中的第三条,或第5段落的第二句话。使用XPointer就变得容易了
If the hyperlink points to an XML document, we can add an XPointer part after the URL in the xlink:href attribute, to navigate (with an XPath expression) to a specific place in the document.
如果连接指向一XML文档,我们可以在xlink:href属性URL的后面添加一个XPointer部分,来导航(使用XPath表达式)来定位到文档中的指定位置。
For example, in the example below we use XPointer to point to the fifth item in a list with a unique id of "rock":
举个例子,下面这个例子中我们使用了XPointer并通过唯一的id"rock"来指向一列表中的第5条:
href=http://www.example.com/cdlist.xml#id('rock').child(5,item)
Let's try to learn some basic XLink syntax by looking at an example.
让我们尝试通过观察实例来学习一些基本的XLink语法。
The XML Example Document
XML举例文档Look at the following XML document, "bookstore.xml", that represents a few books:
观察下面的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>In the example above the XLink namespace is declared at the top of the document (xmlns:xlink="http://www.w3.org/1999/xlink"). This means that the document has access to the XLink attributes and features.
上面的实例中XLink的命名空间位于文档的顶部(xmlns:xlink="http://www.w3.org/1999/xlink")。这意味着文档可以访问到XLink的属性以及其特征。The xlink:type="simple" creates a simple "HTML-like" link. You can also specify more complex links (multidirectional links), but for now, we will only use simple links.
xlink:type="simple"建立了形似HTML的简单连接。你还可以指定复杂些的连接(多向性的连接),但现在我们只使用简单的连接The xlink:href attribute specifies the URL to link to, and the xlink:show attribute specifies where to open the link. xlink:show="new" means that the link (in this case, an image) should open in a new window.
xlink:href属性指定了连接到何处的URL,并且xlink:show属性指定了在哪打开连接。xlink:show="new"意思为连接会在新的窗口中打开(图片也如此)
XLink - Going Further
XLink - 更多方面In the example above we have only demonstrated simple links. XLink is getting more interesting when we want to access remote locations as resources, instead of standalone pages. The <description> element in the example above sets the value of the xlink:show attribute to "new". This means that the link should open in a new window. We could have set the value of the xlink:show attribute to "embed". This means that the resource should be processed inline within the page. When you consider that this could be another XML document and not just an image, you could, for example, build a hierarchy of XML documents.
上面的举例中我们仅仅演示了简单的连接。当我们要访问远端资源时XLink能变得更有趣,而不光光是独立的一张页面。举例中的<description>元素设置了xlink:show属性的值为"new"。这意味着连接会在新的窗口中打开。我们可以将该属性设置为"embed"这就意味着连接资源会以嵌入的形式显示在页面中。当考虑到这可能是另一个XML文档而非只是一张图片,你可以,举例来说,建立一个XML文档阶层With XLink, you can also specify WHEN the resource should appear. This is handled by the xlink:actuate attribute. xlink:actuate="onLoad" specifies that the resource should be loaded and shown when the document loads. However, xlink:actuate="onRequest" means that the resource is not read or shown before the link is clicked. This is very handy for low-bandwidth settings.
通过XLink,你还可以指定资源在什么时候显示。这由xlink:actuate属性来处理。 xlink:actuate="onLoad" 指定资源在文档被加载的时候显示。然而,xlink:actuate="onRequest"意味着资源在连接点击前是不回读取显示的。这对于针对低带宽配置来说是非常便利的。
Let's try to learn some basic XPointer syntax by looking at an example.
让我们尝试通过观察一个实例来学习一些XPointer的基础语法。
XPointer 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.
在这个举例中,我们将告诉你怎样联合XLink使用XPointer来指向另一个文档中的指定部位。We will start by looking at the target XML document (the document we are going to link to).
我们从观察要到达的XML文档开始(我们要连接到的文档)
The Target XML Document
目标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"> <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>Note that the XML document above uses id attributes on each element we may want to link to!
注意,上面XML文档中每个元素使用到的id属性是我们将要去连接的!
The Linking XML Document
连接XML文档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属性的URL后面加上XPointer表达式。The expression: #xpointer(id("Rottweiler")) refers to the element in the target document, with the id value of "Rottweiler".
表达式:#xpointer(id("Rottweiler"))通过与id"Rottweiler"的值与目标文档中的元素相关联So the xlink:href attribute would look like this:
因此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:
然而,当通过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" 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>XLink Attribute Reference
Attribute Value Description xlink:actuate onLoad
onRequest
other
noneDefines when the linked resource is read and shown xlink:href URL The URL to link to xlink:show embed
new
replace
other
noneWhere to open the link. Replace is default xlink:type simple
extended
locator
arc
resource
title
noneThe type of link