XPointer 實例
讓我們透過研究一個實例來學習一些基礎的 XPointer 語法。
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>
<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>
<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>