XML , XLink và XPointer
XLink được sử dụng để tạo siêu liên kết trong tài liệu XML.
|
Hỗ trợ trình duyệt XLink
Không có trình duyệt hỗ trợ XLink trong tài liệu XML.
Tuy nhiên, tất cả các trình duyệt chính đều hỗ trợ XLinks ở dạng SVG .
Cú pháp XLink
Trong HTML, phần tử <a> xác định một siêu liên kết. Tuy nhiên, đây không phải là cách nó hoạt động trong XML. Trong tài liệu XML, bạn có thể sử dụng bất kỳ tên thành phần nào bạn muốn - do đó, trình duyệt không thể dự đoán thành phần liên kết nào sẽ được gọi trong tài liệu XML.
Dưới đây là một ví dụ đơn giản về cách sử dụng XLink để tạo liên kết trong tài liệu XML:
<?xml version="1.0" encoding="UTF-8"?>
<homepages xmlns:xlink="https://www.w3.org/1999/xlink">
<homepage xlink:type="simple"
xlink:href="https://www.example.com.vn">Visit W3Schools</homepage>
<homepage xlink:type="simple"
xlink:href="https://www.w3.org">Visit W3C</homepage>
</homepages>
Để có quyền truy cập vào các tính năng XLink, chúng ta phải khai báo không gian tên XLink. Không gian tên XLink là: "https://www.w3.org/1999/xlink".
Các thuộc tính xlink:type và xlink:href trong phần tử <homepage> đến từ không gian tên XLink.
Xlink:type="simple" tạo một liên kết "giống HTML" đơn giản (có nghĩa là "nhấp vào đây để đến đó").
Thuộc tính xlink:href chỉ định URL cần liên kết tới.
Ví dụ XLink
Tài liệu XML sau chứa các tính năng XLink:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore xmlns:xlink="https://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>
Ví dụ giải thích:
- Không gian tên XLink được khai báo ở đầu tài liệu (xmlns:xlink="https://www.w3.org/1999/xlink")
- Xlink:type="simple" tạo một liên kết "giống HTML" đơn giản
- Thuộc tính xlink:href chỉ định URL cần liên kết đến (trong trường hợp này - một hình ảnh)
- Xlink:show="new" chỉ định rằng liên kết sẽ mở trong một cửa sổ mới
XLink - Tiến Xa Hơn
Trong ví dụ trên, chúng tôi đã trình diễn các XLink đơn giản. XLink ngày càng thú vị hơn khi truy cập các vị trí từ xa dưới dạng tài nguyên, thay vì các trang độc lập.
Nếu chúng tôi đặt giá trị của thuộc tính xlink:show thành "nhúng", tài nguyên được liên kết sẽ được xử lý nội tuyến trong trang. Ví dụ: khi bạn cho rằng đây có thể là một tài liệu XML khác, bạn có thể xây dựng một hệ thống phân cấp các tài liệu XML.
Bạn cũng có thể chỉ định KHI tài nguyên sẽ xuất hiện bằng thuộc tính xlink:actuate.
Tham chiếu thuộc tính 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
|
Hỗ trợ trình duyệt XPPointer
Không có trình duyệt hỗ trợ cho XPointer. Nhưng XPointer được sử dụng trong các ngôn ngữ XML khác.
Ví dụ XPointer
Trong ví dụ này, chúng tôi sẽ sử dụng XPointer kết hợp với XLink để trỏ đến một phần cụ thể của tài liệu khác.
Chúng ta sẽ bắt đầu bằng cách xem xét tài liệu XML mục tiêu (tài liệu chúng ta đang liên kết tới):
<?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>
Lưu ý rằng tài liệu XML ở trên sử dụng thuộc tính id trên mỗi phần tử!
Vì vậy, thay vì liên kết đến toàn bộ tài liệu (như với XLink), XPointer cho phép bạn liên kết đến các phần cụ thể của tài liệu. Để liên kết đến một phần cụ thể của trang, hãy thêm ký hiệu số (#) và biểu thức XPointer sau URL trong thuộc tính xlink:href, như sau: xlink:href="https://dog.com/dogbreeds.xml #xpointer(id('Rottweiler'))". Biểu thức đề cập đến phần tử trong tài liệu đích, với giá trị id là "Rottweiler".
XPointer cũng cho phép một phương thức tốc ký để liên kết đến một phần tử có id. Bạn có thể sử dụng trực tiếp giá trị của id, như sau: xlink:href="https://dog.com/dogbreeds.xml#Rottweiler".
Tài liệu XML sau đây chứa các liên kết đến nhiều thông tin hơn về giống chó của từng con chó của tôi:
<?xml version="1.0" encoding="UTF-8"?>
<mydogs xmlns:xlink="https://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>