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>

ブラウザで表示。

上記の XML ドキュメントでは、リンクしたい各要素に id 属性が使用されていることに注意してください!


XML リンクドキュメント

XPointer を使用すると、XLink を使用してドキュメント全体にリンクするだけでなく、ドキュメントの特定の部分にリンクすることもできます。ページの特定の部分にリンクするには、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>
その他の拡張