XML , XLink และ XPointer
XLink ใช้เพื่อสร้างไฮเปอร์ลิงก์ในเอกสาร XML
|
การสนับสนุนเบราว์เซอร์ XLink
ไม่มีการสนับสนุนเบราว์เซอร์สำหรับ XLink ในเอกสาร XML
อย่างไรก็ตาม เบราว์เซอร์หลักทั้งหมดรองรับXLinks ใน SVG
XLink ไวยากรณ์
ใน HTML <a> องค์ประกอบกำหนดไฮเปอร์ลิงก์ อย่างไรก็ตาม นี่ไม่ใช่วิธีการทำงานใน XML ในเอกสาร XML คุณสามารถใช้ชื่อองค์ประกอบใดก็ได้ตามต้องการ ดังนั้นจึงเป็นไปไม่ได้ที่เบราว์เซอร์จะคาดเดาว่าองค์ประกอบลิงก์ใดจะถูกเรียกในเอกสาร XML
ด้านล่างนี้เป็นตัวอย่างง่ายๆ ของวิธีใช้ XLink เพื่อสร้างลิงก์ในเอกสาร XML:
<?xml version="1.0" encoding="UTF-8"?>
<homepages xmlns:xlink="http://www.w3.org/1999/xlink">
<homepage xlink:type="simple"
xlink:href="https://www.w3schools.com">Visit W3Schools</homepage>
<homepage xlink:type="simple"
xlink:href="http://www.w3.org">Visit W3C</homepage>
</homepages>
ในการเข้าถึงคุณสมบัติ XLink เราต้องประกาศเนมสเปซ XLink เนมสเปซ XLink คือ: "http://www.w3.org/1999/xlink"
xlink:type และแอตทริบิวต์ xlink:href ในองค์ประกอบ <homepage> มาจากเนมสเปซ XLink
xlink:type="simple" สร้างลิงก์ "เหมือน HTML" อย่างง่าย (หมายถึง "คลิกที่นี่เพื่อไปที่นั่น")
แอตทริบิวต์ xlink:href ระบุ URL ที่จะลิงก์ไป
ตัวอย่าง XLink
เอกสาร XML ต่อไปนี้มีคุณลักษณะ XLink:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="http://www.w3.org/1999/xlink">
<book title="Harry Potter">
<description
xlink:type="simple"
xlink:href="/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="/images/XQuery.gif"
xlink:show="new">
XQuery Kick Start delivers a concise introduction
to the XQuery standard.......
</description>
</book>
</bookstore>
ตัวอย่างอธิบาย:
- มีการประกาศเนมสเปซ XLink ที่ด้านบนของเอกสาร (xmlns:xlink="http://www.w3.org/1999/xlink")
- xlink:type="simple" สร้างลิงก์ "เหมือน HTML" อย่างง่าย
- แอตทริบิวต์ xlink:href ระบุ URL ที่จะเชื่อมโยง (ในกรณีนี้ - รูปภาพ)
- xlink:show="new" ระบุว่าลิงก์ควรเปิดในหน้าต่างใหม่
XLink - ก้าวต่อไป
ในตัวอย่างข้างต้น เราได้สาธิต XLinks อย่างง่าย XLink มีความน่าสนใจมากขึ้นเมื่อเข้าถึงสถานที่ห่างไกลเป็นทรัพยากร แทนที่จะเป็นหน้าแบบสแตนด์อโลน
หากเราตั้งค่าแอตทริบิวต์ xlink:show เป็น "ฝัง" ทรัพยากรที่เชื่อมโยงควรได้รับการประมวลผลแบบอินไลน์ภายในหน้า เมื่อคุณพิจารณาว่านี่อาจเป็นเอกสาร XML อื่น คุณสามารถสร้างลำดับชั้นของเอกสาร XML ได้
คุณยังสามารถระบุเวลาที่ทรัพยากรควรปรากฏขึ้นได้ด้วยแอตทริบิวต์ xlink:actuate
การอ้างอิงแอตทริบิวต์ XLink
Attribute | Value | Description |
---|---|---|
xlink:actuate | onLoad onRequest other none |
Defines when the linked resource is read and shown:
|
xlink:href | URL | Specifies the URL to link to |
xlink:show | embed new replace other none |
Specifies where to open the link. Default is "replace" |
xlink:type | simple extended locator arc resource title none |
Specifies the type of link |
XPointer
|
รองรับเบราว์เซอร์ XPointer
ไม่มีการสนับสนุนเบราว์เซอร์สำหรับ XPointer แต่ XPointer ถูกใช้ในภาษา XML อื่นๆ
ตัวอย่าง XPointer
ในตัวอย่างนี้ เราจะใช้ XPointer ร่วมกับ XLink เพื่อชี้ไปยังส่วนใดส่วนหนึ่งของเอกสารอื่น
เราจะเริ่มต้นด้วยการดูเอกสาร XML เป้าหมาย (เอกสารที่เรากำลังเชื่อมโยง):
<?xml version="1.0" encoding="UTF-8"?>
<dogbreeds>
<dog breed="Rottweiler" id="Rottweiler">
<picture url="https://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="https://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 ในแต่ละองค์ประกอบ!
ดังนั้น แทนที่จะลิงก์ไปยังเอกสารทั้งหมด (เช่นเดียวกับ XLink) XPointer ช่วยให้คุณสามารถลิงก์ไปยังส่วนต่างๆ ของเอกสารได้ หากต้องการลิงก์ไปยังส่วนใดส่วนหนึ่งของหน้า ให้เพิ่มเครื่องหมายตัวเลข (#) และนิพจน์ XPointer หลัง URL ในแอตทริบิวต์ xlink:href ดังนี้ xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('ร็อตไวเลอร์'))". นิพจน์อ้างอิงถึงองค์ประกอบในเอกสารเป้าหมาย โดยมีค่ารหัสเป็น "ร็อตไวเลอร์"
XPointer ยังอนุญาตให้ใช้วิธีชวเลขสำหรับลิงก์ไปยังองค์ประกอบที่มีรหัส คุณสามารถใช้ค่าของ id ได้โดยตรง เช่น xlink:href="https://dog.com/dogbreeds.xml#Rottweiler"
เอกสาร XML ต่อไปนี้มีลิงก์ไปยังข้อมูลเพิ่มเติมของสายพันธุ์สุนัขสำหรับสุนัขแต่ละตัวของฉัน:
<?xml version="1.0" encoding="UTF-8"?>
<mydogs xmlns:xlink="http://www.w3.org/1999/xlink">
<mydog>
<description>
Anton is my favorite dog. He has won a lot of.....
</description>
<fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#Rottweiler">
Fact about Rottweiler
</fact>
</mydog>
<mydog>
<description>
Pluto is the sweetest dog on earth......
</description>
<fact xlink:type="simple" xlink:href="https://dog.com/dogbreeds.xml#FCRetriever">
Fact about flat-coated Retriever
</fact>
</mydog>
</mydogs>