课程表

XLink/XPointer课程

工具箱
速查手册

XPointer 实例

当前位置:免费教程 » XML相关 » XLink/XPointer

让我们通过研究一个实例来学习一些基础的 XPointer 语法。

XPointer 实例

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

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

目标XML文档

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

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <dogbreeds>
  4.  
  5. <dog breed="Rottweiler" id="Rottweiler">
  6. <picture url="http://dog.com/rottweiler.gif" />
  7. <history>
  8. The Rottweiler's ancestors were probably Roman
  9. drover dogs.....
  10. </history>
  11. <temperament>
  12. Confident, bold, alert and imposing, the Rottweiler
  13. is a popular choice for its ability to protect....
  14. </temperament>
  15. </dog>
  16.  
  17. <dog breed="FCRetriever" id="FCRetriever">
  18. <picture url="http://dog.com/fcretriever.gif" />
  19. <history>
  20. One of the earliest uses of retrieving dogs was to
  21. help fishermen retrieve fish from the water....
  22. </history>
  23. <temperament>
  24. The flat-coated retriever is a sweet, exuberant,
  25. lively dog that loves to play and retrieve....
  26. </temperament>
  27. </dog>
  28.  
  29. </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 来引用:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
  4.  
  5. <mydog xlink:type="simple"
  6. xlink:href="http://dog.com/dogbreeds.xml#Rottweiler">
  7. <description xlink:type="simple"
  8. xlink:href="http://myweb.com/mydogs/anton.gif">
  9. Anton is my favorite dog. He has won a lot of.....
  10. </description>
  11. </mydog>
  12.  
  13. <mydog xlink:type="simple"
  14. xlink:href="http://dog.com/dogbreeds.xml#FCRetriever">
  15. <description xlink:type="simple"
  16. xlink:href="http://myweb.com/mydogs/pluto.gif">
  17. Pluto is the sweetest dog on earth......
  18. </description>
  19. </mydog>
  20.  
  21. </mydogs>
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号