XML - 管理資料交換/一對一關係
| 上一章 | 下一章 |
| ← 一對多關係 | 多對多關係 → |
|
學習目標
|
在上一章中,介紹了 XML 架構、文件和樣式表的一些新功能,以及如何對一對多關係進行建模。在本章中,我們將介紹在 XML 中對一對一關係的建模。我們還將介紹 XML 架構的更多功能。
下圖顯示了一對一和一對多關係。一對一關係記錄每個國家/地區作為單個頂級目的地。
圖 4-1:1:1 關係的資料模型
一對一 (1:1) 關係在圖 4-1 中的資料模型中表示。將國家/地區和目的地新增到資料模型中允許名為 topDestination 的 1:1 關係。一個國家/地區有許多不同的目的地,但只有一個頂級目的地。圖 4-2 中的 XML 架構顯示瞭如何在 XML 架構中表示 1:1 關係。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
<!--
Tour Guide
-->
<xsd:element name="tourGuide">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="country" type="countryDetails" minOccurs="1" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<!--
Country
-->
<xsd:complexType name="countryDetails">
<xsd:sequence>
<xsd:element name="countryName" type="xsd:string" minOccurs="1" maxOccurs="1"/>
<xsd:element name="population" type="xsd:integer" minOccurs="0" maxOccurs="1" default="0"/>
<xsd:element name="continent" minOccurs="0" maxOccurs="1">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Asia"/>
<xsd:enumeration value="Africa"/>
<xsd:enumeration value="Australasia"/>
<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="topDestination" type="destinationDetails" minOccurs="0" maxOccurs="1"/>
<xsd:element name="destination" type="destinationDetails" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<!--
Destination
-->
<xsd:complexType name="destinationDetails">
<xsd:all>
<xsd:element name="destinationName" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>
<xsd:element name="streetAddress" type="xsd:string" minOccurs="0"/>
<xsd:element name="telephoneNumber" type="xsd:string" minOccurs="0"/>
<xsd:element name="websiteURL" type="xsd:anyURI" minOccurs="0"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
圖 4-2:一對一關係的 XML 架構
讓我們檢查一下圖 4-2 中架構中的新元素和屬性。
- Country 是在 City 中定義的複雜型別,用於表示國家/地區與其城市之間的 1:M 關係。
- Destination 是在 Country 中定義的複雜型別,用於表示國家/地區與其多個目的地之間的 1:M 關係。
- topDestination 是在 Country 中定義的複雜型別,用於表示國家/地區與其頂級目的地之間的 1:1 關係。
在上一章中介紹了對元素施加限制;但是,還可以對元素施加更多可能很有用的限制。可以對影響處理器如何處理空格字元的元素和屬性施加限制。
<xsd:element name="streetAddress">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:whiteSpace value="preserve"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
whiteSpace 約束設定為“preserve”,這意味著 XML 處理器將不會刪除任何空格字元。其他有用的限制包括:
- 替換 - XML 處理器將用空格替換所有空格字元。
- <xsd:whiteSpace value="replace"/>
- 摺疊 - 處理器將刪除所有空格字元。
- <xsd:whiteSpace value="collapse"/>
- length、maxLength、minLength - 元素的長度可以是固定的,也可以具有預定義的範圍。
- <xsd:length value="8"/>
- <xsd:minLength value="5"/>
- <xsd:maxLength value="8"/>
除了對元素施加限制之外,還可以使用順序指示器來定義元素應該出現的順序。
<all> 指示器預設指定子元素可以以任何順序出現,並且每個子元素必須出現一次且僅出現一次。
<xsd:element name="person">
<xsd:complexType>
<xsd:all>
<xsd:element name="firstname" type="xsd:string"/>
<xsd:element name="lastname" type="xsd:string"/>
</xsd:all>
</xsd:complexType>
</xsd:element>
<choice> 指示器指定可以出現一個子元素或另一個子元素。
<xsd:element name="person">
<xsd:complexType>
<xsd:choice>
<xsd:element name="employee" type="employee"/>
<xsd:element name="visitor" type="visitor"/>
</xsd:choice>
</xsd:complexType>
</xsd:element>
<sequence> 指示器指定子元素必須以特定順序出現。
<xsd:element name="person">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstname" type="xsd:string"/>
<xsd:element name="lastname" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
圖 4-3 中的 XML 文件顯示瞭如何在 XML 文件中使用在圖 4-2 中找到的 XML 架構中定義的新元素(country 和 destination)。請注意,由於在架構中使用了 <xsd:all> 順序指示器,因此 <topDestination> 的子元素可以以任何順序出現。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="newXMLSchema.xsl" media="screen"?>
<tourGuide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="XMLSchema.xsd">
<!--
Malaysia
-->
<country>
<countryName>Malaysia</countryName>
<population>22229040</population>
<continent>Asia</continent>
<topDestination>
<description>A popular duty-free island north of Penang.</description>
<destinationName>Pulau Langkawi</destinationName>
</topDestination>
<destination>
<destinationName>Muzium Di-Raja</destinationName>
<description>The original palace of the Sultan</description>
<streetAddress>122 Muzium Road</streetAddress>
<telephoneNumber>48494030</telephoneNumber>
<websiteURL>www.muziumdiraja.com</websiteURL>
</destination>
<destination>
<destinationName>Kinabalu National Park</destinationName>
<description>A national park</description>
<streetAddress>54 Ocean View Drive</streetAddress>
<telephoneNumber>4847101</telephoneNumber>
<websiteURL>www.kinabalu.com</websiteURL>
</destination>
</country>
<!--
Belize
-->
<country>
<countryName>Belize</countryName>
<population>249183</population>
<continent>South America</continent>
<topDestination>
<destinationName>San Pedro</destinationName>
<description>San Pedro is an island off the coast of Belize</description>
</topDestination>
<destination>
<destinationName>Belize City</destinationName>
<description>Belize City is the former capital of Belize</description>
<websiteURL>www.belizecity.com</websiteURL>
</destination>
<destination>
<destinationName>Xunantunich</destinationName>
<description>Mayan ruins</description>
<streetAddress>4 High Street</streetAddress>
<telephoneNumber>011770801</telephoneNumber>
</destination>
</country>
</tourGuide>
圖 4-3:一對一關係的 XML 文件
| 架構設計人員可以對元素的長度以及處理器如何處理空格施加限制。架構設計人員還可以為元素指定固定值或預設值。順序指示器可用於指定元素必須在 XML 文件中出現的順序。 |
