ตัวอย่าง 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.xml" ในเว็บเบราเซอร์ของคุณ

หมายเหตุ:เอกสาร XML ที่แสดงด้านบนใช้ id บนองค์ประกอบที่เราต้องการลิงก์!

เอกสารลิงก์ XML

ไม่เพียงแค่สามารถลิงก์ไปยังเอกสารทั้งหมด (เมื่อใช้ XLink) แต่ XPointer ยังอนุญาตให้คุณลิงก์ไปยังส่วนเฉพาะของเอกสาร。ถ้าต้องการลิงก์ไปยังส่วนเฉพาะของหน้าที่คุณต้องการ โปรดเพิ่มเลขเล็ก (#) และ XPointer แสดงที่ xlink:href ใน URL。

การแสดงที่เรียกว่า:#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 คือสุนัขที่ชอบของฉัน. เขาได้ชนะในการแข่งขันมากมาย.....
  </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 คือสุนัขที่น่ารักที่สุดบนโลก......
  </description>
</mydog>
</mydogs>