XML - 資料交換管理/XLink
| 上一章 | 下一章 |
| ← XPath | CSS → |
|
學習目標
|
贊助商 佐治亞大學
|
透過使用統一資源識別符號 (URI),XLink 允許將元素插入 XML 文件中,從而在資源之間建立連結,例如文件、影像、檔案和其他頁面。XLink 的概念類似於 HTML 超連結,但功能更強大、更靈活。
本章將概述 XLink 語法。它還將介紹一些 XLink 的基本概念。有關完整的 XLink 規範,請參閱最新版本的標準,網址為
XLinks 在兩個或多個資源之間建立連結關係。它們允許在連結中指定任何 XML 元素、影像、文字或標記檔案。
透過使用類似於 XSL 樣式表的集中化格式的方法,XLinks 允許將文件的超連結隔離並集中在單獨的文件中。隨著連結文件的地址發生變化,XLink 仍然可以正常工作。
使用 XLink 需要宣告 XLink 名稱空間。此名稱空間提供了用於型別、href、角色、arcrole、標題、顯示、啟用、標籤、從和到的全域性屬性。以下示例將在 tourGuide 元素中使字首 xlink 可用。
<tourGuide
xmlns:xlink="http://www.w3.org/1999/xlink">
...
</tourGuide>
下表概述了可以與 xlink 名稱空間一起使用的屬性。全域性屬性是型別、href、角色、arcrole、標題、顯示、啟用、標籤、從和到。該表還包括有關如何使用屬性的說明。
示例 1:全域性屬性表
|
屬性 |
描述和有效值 |
|
型別 |
描述專案的含義
|
|
href |
資源的位置
|
|
角色 |
XLink 內容的描述
|
|
arcrole |
XLink 內容的描述
|
|
標題 |
顯示的名稱,通常是連結的簡短描述 |
|
顯示 |
描述 XLink 啟用並載入後瀏覽器的行為
|
|
啟用 |
指定何時檢索資源或連結處理發生
|
|
標籤、從和到 |
指定連結方向 |
以下 XML 架構定義了一個旅行指南,其中包含至少一個城市。每個城市包含一個或多個景點。每個景點的名稱都是一個 XLink。
示例 2:TourGuide 的 XML 架構
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : TourGuide.xsd
Created on : February 28, 2006
Author : Billy Timmins
-->
<!--
Declaration of usage of xlink Namespace
-->
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsd:element name="tourGuide">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="city" type="cityDetails" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
This section will contain the City details
-->
<xsd:complexType name="cityDetails">
<xsd:sequence>
<xsd:element name="cityName" type="xsd:string"/>
<xsd:element name="adminUnit" type="xsd:string"/>
<xsd:element name="country" type="xsd:string"/>
<xsd:element name="continent">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Asia"/>
<xsd:enumeration value="Africa"/>
<xsd:enumeration value="Australia"/>
<xsd:enumeration value="Europe"/>
<xsd:enumeration value="North America"/>
<xsd:enumeration value="South America"/>
<xsd:enumeration value="Antarctica"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="population" type="xsd:integer"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="attraction" type="attractionDetails" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="attractionDetails">
<xsd:sequence>
<!--
Note use of xlink
-->
<xsd:element name="attractionName" xlink:type="simple"/>
<xsd:element name="attractionDescription" type="xsd:string"/>
<xsd:element name="attractionRating" type="xsd:integer"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
以下 XML 文件顯示瞭如何在 XML 文件中使用在 XML 架構中定義的 XLink attractionName。請注意,為了定義連結的網站,必須在屬性標籤中包含 xlink:href=""。
示例 3:TourGuide.xsd 的 XML 文件(使用 XLink)
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : SomeTourGuide.xml
Created on : February 28, 2006
Author : Billy Timmins
-->
<!--
Declaration of usage of XLink Namespace
-->
<?xml-stylesheet href="TourGuide.xsl" type="text/xsl"?>
<tourGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xlink="http://www.w3.org/1999/xlink" xsi:noNamespaceSchemaLocation="TourGuide.xsd">
<city>
<cityName>Atlanta</cityName>
<adminUnit>Georgia</adminUnit>
<country>USA</country>
<continent>North America</continent>
<population>425000</population>
<description>Atlanta is the capital of and largest city in the U.S. state of Georgia.</description>
<attraction>
<!--
Declaration of XLink and associated link
-->
<attractionName xlink:href="http://www.georgiaaquarium.org/"> Georgia Aquarium </attractionName>
<attractionDescription>World’s Largest Aquarium</attractionDescription>
<attractionRating>5</attractionRating>
</attraction>
<attraction>
<!--
Declaration of XLink and associated link
-->
<attractionName xlink:href="http://www.high.org/"> High Museum of Art </attractionName>
<attractionDescription>The High Museum of Art, founded in 1905 as the Atlanta Art Association, is the leading art museum in the Southeastern United States.</attractionDescription>
<attractionRating>4</attractionRating>
</attraction>
<attraction>
<!--
Declaration of XLink and associated link
-->
<attractionName xlink:href="http://www.underground-atlanta.com/"> Underground Atlanta </attractionName>
<attractionDescription> Go beneath the streets of a bustling downtown, to the heart of a great American city. Underground Atlanta is at the center of it all.</attractionDescription>
<attractionRating>2</attractionRating>
</attraction>
</city>
<city>
<cityName>Tampa</cityName>
<adminUnit>Florida</adminUnit>
<country>USA</country>
<continent>North America</continent>
<population>303000</population>
<description>Tampa is a major United States city located in Hillsborough County, on the west coast of Florida.</description>
<attraction>
<!--
Declaration of XLink and associated link
-->
<attractionName xlink:href="http://www.buschgardens.com/buschgardens/fla/default.aspx"> Bush Gardens </attractionName>
<attractionDescription>The nation's fourth largest zoo, Bush Gardens is where you can see African animals roaming free and an exciting amusement park featuring its world-famous rides like Kumba and the new inverted roller-coaster, Montu.</attractionDescription>
<attractionRating>5</attractionRating>
</attraction>
<attraction>
<!--
Declaration of XLink and associated link
-->
<attractionName xlink:href="http://www.plantmuseum.com/"> Henry B. Plant Museum </attractionName>
<attractionDescription>Discover a museum which transports you to turn-of-the-century Florida.</attractionDescription>
<attractionRating>1</attractionRating>
</attraction>
</city>
</tourGuide>
以下 XML 樣式表顯示 XML 文件的內容。
示例 4:TourGuide 的 XML 樣式表
<?xml version="1.0" encoding="UTF-8"?>
<!--
Document : TourGuide.xsl
Created on : February 28, 2006
Author : Billy Timmins
-->
<!--
Declaration of usage of XLink Namespace
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xlink="http://www.w3.org/1999/xlink" exclude-result-prefixes="xlink" version="1.0">
<xsl:output method="html"/>
<!--
Attribute XLink defined as an href of simple type
-->
<xsl:template match="*[@xlink:type = 'simple' and @xlink:href]">
<a href="{@xlink:href}">
<xsl:apply-templates/>
</a>
</xsl:template>
<xsl:template match="/">
<html>
<head>
<title>Tour Guide XLink Example</title>
</head>
<body>
<h2>Cities</h2>
<xsl:apply-templates select="tourGuide"/>
</body>
</html>
</xsl:template>
<!--
template for handling a link
-->
<xsl:template match="attractionName">
<a href="{@xlink:href}">
<xsl:value-of select="."/>
</a>
</xsl:template>
<xsl:template match="tourGuide">
<table border="1" width="100%">
<xsl:for-each select="city">
<tr>
<td>
<br/>
<xsl:text>City: </xsl:text>
<xsl:value-of select="cityName"/>
<br/>
<xsl:text>County: </xsl:text>
<xsl:value-of select="adminUnit"/>
<br/>
<xsl:text>Continent: </xsl:text>
<xsl:value-of select="continent"/>
<br/>
<xsl:text>Population: </xsl:text>
<xsl:value-of select="population"/>
<br/>
<xsl:text>Description: </xsl:text>
<xsl:value-of select="description"/>
<br/>
<br/>
</td>
</tr>
<tr>
<td>
<xsl:text>Attraction: </xsl:text>
</td>
<td>
<xsl:text>Attraction Description: </xsl:text>
</td>
<td>
<xsl:text>Attraction Rating: </xsl:text>
</td>
</tr>
<xsl:for-each select="attraction">
<tr>
<td>
<!--
application of the template
-->
<xsl:apply-templates select="attractionName"/>
</td>
<td>
<xsl:value-of select="attractionDescription"/>
</td>
<td>
<xsl:value-of select="attractionRating"/>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
| XLink 是一個極其通用的規範,它標準化了連結到其他資料來源的過程。XLink 不僅支援類似於 HTML 中的錨標籤的單向連結,而且還可以用於建立雙向連結。此外,XLink 允許從任何 XML 元素連結。這給了開發人員很大的自由度。 |