XML/數據schemas

維基教科書,自由的教學讀本
< XML

XML > 數據schemas


學習目標[編輯]

完成本章內容的學習後,您應該能夠:

  • 在XML文檔中使用命名空間來識別不同schema文檔中具有相同名字的元素。
  • 詳細闡述在XML文檔中如何使用命名空間。
  • 使用 <xsd:redefine>對數據類型的定義進行擴充。
  • 使用 <xsd:import>導入XML schema文檔並將其與命名空間綁定。
  • 使schema模塊化。

本章將複習以往所學與XML schema相關的全部內容。

概述[編輯]

在前面的章節中,我們介紹了大量關於XML schema文檔以及其它主題的知識。本章將回溯先前所學內容。為此首先需要引

入一些新的概念。XML schema文檔能夠規定命名空間在XML實例文檔(*.xml file)中的使用方式,因此我們很有必要了 解如何有效地使用命名空間。在前面的章節中我們曾介紹過<xsd:include>標籤的使用。本章將介紹另外兩種可以包含外部 schema文檔的方法,即使用<xsd:redefine> 和<xsd:import>標籤。通過這幾種機制(include(包含), redefine (重新定義), import(導入)),XML schemas可以實現模塊化從而被方便地使用。

「A rose by any other name…」: 命名空間[編輯]

什麼是命名空間?需要使用命名空間嗎?後者的回答很簡單——是。對於前者的回答就不那麼簡單了。命名空間的使用在

XML中主要有兩個目的:

  1. 用於區分那些名稱相同但來自不同schema庫,具有不同含義的元素和屬性。

  2. 在一個單一的XML應用中,將相關的元素和屬性組合起來,使它們可以很容易地被軟件包識別。
通過將元素、屬性與URL相關聯,命名空間能夠區分具有相同名稱的元素。比較常見的是將一個XML應用程式中的元素分配給

一個URL(有時被稱為命名空間的名稱)。對於XML應用程式來說,利用多個命名空間來區別該應用程式的不同部分或任務, 有時是十分有用的。

接下來您可能會問:「如何聲明被使用的命名空間呢?」有兩種聲明方式:
  1. 默認命名空間聲明:不帶前綴,所有的元素和屬性在該命名空間的範圍內被引用,且引用時使用的名稱無規範限制。
  2. 或者
  3. 顯式命名空間聲明:聲明時指定前綴,所有元素和屬性命名時必須採用該前綴作為規範名稱的一部分,以表示屬於該命名空間。
命名空間的名稱(URL)在被引用時不能使用無效的XML字符(like /, %, ~)即使是簡短的前綴也是如此。我們通常採用

這些前綴(xsd和xsl),它們在XML schema文檔和XML樣式表被廣泛採用。它們充當着命名空間名稱的佔位符(或直接引 用)。命名空間聲明將前綴與特定的命名空間名稱綁定。

讓我們來看一個例子,當我們創建一個新的XML schema文檔時,NetBeans給出的命名空間聲明:


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/examples/targetNS"
xmlns="http://xml.netbeans.org/examples/targetNS"
elementFormDefault="unqualified">
</xsd:schema>

圖 7-1:默認的XML schema

xmlns:xsd
xmlns機制表示一個命名空間聲明。注意,這個聲明將命名空間名稱"http://www.w3.org/2001/XMLSchema" 與xsd前綴綁定在了一起。

targetNamespace
這個聲明將schema文檔與目標命名空間名稱"http://xml.netbeans.org/examples/targetNS". 關聯起來。儘管目標命名空間 已被聲明,仍然需要以默認的命名空間或者顯式的命名空間(通過與前綴綁定的方式)加以定義。

xmlns
注意該聲明中沒有使用前綴。不帶冒號和前綴的xmlns機制表明這是一個默認的命名空間聲明。

elementFormDefault

這一XML的代表屬性用於表示在一個實例文檔中被局部聲明的元素是否必須被目標命名空間所規範。屬性值決定了schema中

所用元素的命名空間是否在xml文檔中被隱藏。值為「unqualified」表示在XML實例文檔「隱藏」該命名空間,因此在實例 文檔中僅需對目標命名空間進行命名空間聲明。值為「qualified」表示任何xml實例文檔必須聲明其在schema文檔中使用 的每一個命名空間。該屬性僅對該實例文檔所應用的schema適用。為了「隱藏」包含外部schema文檔的shema文檔中的所有 命名空間,每一個外部schema文檔必須也將該屬性設為「unqualified」。

在XML文檔中,可通過使用命名空間名稱前綴顯式定義的命名空間分辨一個元素。但是,屬於默認命名空間的元素不需要使

用前綴加以規範。請觀察下面的XML文件:


<?xml version="1.0" encoding="UTF-8"?>

<th:theatres xmlns="http://www.opentourism.org/xmtext/theatres”
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opentourism.org/xmtext/theatres theatres.xsd"
xmlns:th="http://www.opentourism.org/xmtext/theatres"
xmlns:addr=”http://www.opentourism.org/xmtext/address”?>

<theatre>
<name>Theatre at Madison Square Garden</name>
<address>
<addr:street1>Seventh Avenue</addr:street1>
<addr:city>New York</addr:city>
<addr:state>NY</addr:state>
<addr:zip>10001</addr:zip>
</address>
</theatre>
</th:theatres>
</font>

表 7-2: theatre.xml


假定該文件合法,其元素theatre和name都屬於默認命名空間(http://www.opentourism.org/xmtext/theatres)。剩下的元素使用規範的名稱(前綴:元素)來表

明它們屬於與addr前綴綁定的命名空間(http://www.opentourism.org/xmtext/address)


既然所有的schema文檔至少使用兩個命名空間:目標命名空間,以及XML schema 命名空間(包括無命名空間的

schemas),可用以下三種方式來設計您的shemas,它們都涉及這兩種命名空間的處理:

  1. 將XML schema 作為默認命名空間,顯式地規範所有對目標命名空間組件的引用。

  2. 與第一種方法相反,將目標命名空間作為默認命名空間,顯式地規範所有來自XML schema命名空間的組件。

  3. 不使用默認命名空間,顯式地規範對目標命名空間組件的引用,同時也顯式地規範所有來自XML schema命名空間的組件。

想了解更多關於這三種方法的信息,以及它們的優缺點,請訪問以下Web站點:

http://www.xfront.com/DefaultNamespace.html

我們已經基本了解了XML schema文檔中命名空間的用法,那麼就可以使用一些新的標籤,實現在一個XML schema文檔中包

含多個XML schema庫(文檔)。

對世界的重新定義: <xsd:redefine> 標籤的魔力[編輯]

假設存在一個擴展類型庫(定義了多種數據類型的XML schema文檔)。<xsd:redfine> 機制允許在新的

XML schema文檔里擴展類型庫(在獨立XML schema文檔中定義的類型)中的基本類型。請看下面這個簡短的用於描述世界 的複雜類型定義:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/examples/targetNS"
xmlns="http://xml.netbeans.org/examples/targetNS"
elementFormDefault="unqualified">
<xsd:include schemaLocation = “http://www.opentourism.org/xmtext/continentLib.xsd” /> 
<xsd:include schemaLocation = “http://www.opentourism.org/xmtext/oceanLib.xsd” />
<xsd:complexType name=”worldType”> 
<xsd:sequence>
<xsd:element name=”Continent” type = “continentType” minOccurs = “0” maxOccurs=“unbounded” /> 
<xsd:element name=”Ocean” type=”oceanType” minOccurs =“0”  maxOccurs =”unbounded” />
</xsd:sequence> 
</xsd:complexType>
</xsd:schema> 
</font>

表 7-3: XML Schema worldLib.xsd


注意被包含的類型庫中包括這兩個複雜類型:oceanType和continentType。
現在我們要重新定義worldType,將行星信息包含進去。請看下面的包含新的worldType定義的XML schema文檔:


<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://xml.netbeans.org/examples/targetNS" xmlns="http://xml.netbeans.org/examples/targetNS" elementFormDefault="unqualified">
 <!-- New XML schema documents are included to define the types that are being added to the worldType definition.  -->
 <xsd:include schemaLocation = "http://www.opentourism.org/xmtext/RotationLib.xsd" />
 <xsd:include schemaLocation = "http://www.opentourism.org/xmtext/GalaxyLib.xsd" /> 
 <!-- The <code><xsd:redefine></code>tag indicates the schema location containing the type(s) to be included or redefined  -->
 <xsd:redefine schemaLocation="http://www.opentourism.org/xmtext/worldLib.xsd">
  <xsd:complexType name="worldType">
   <!-- The <code><xsd:complexContent></code> element is nested within the <code><xsd:complexType></code>element as the first step in deriving the newly defined data type.  -->
   <xsd:complexContent>
    <!-- For the second step in the derivation of the new data type, the <code><xsd:extension></code> element is nested within the <code><xsd:complexContent></code> element.   Notice that the extension element's base is specified as "worldType" which is the base type from which the new data type is derived.   -->
    <xsd:extension base ="worldType">
     <!-- The <code><xsd:sequence></code> tag defines the sequence of the elements being appended to the "worldType" base type.  -->
     <xsd:sequence>
      <xsd:element name="Rotation" type = "rotationType" /> 
      <xsd:element name="Galaxy" type="galaxyType" />
     </xsd:sequence> 
    </xsd:extension>
   </xsd:complexContent>
  </xsd:complexType>
 </xsd:redefine>
</xsd:schema>

表 7-4: XML Schema newWorldType.xsd


注意,新的schema 文檔不包括與原先相同的類型庫,因為redefine標籤將包含來自原schema文檔的所有聲明和定義(與

include類似)。並且與include一樣,redefine標籤必須出現在類型定義與元素聲明之前。

這個新的XML schema 文檔用於重新定義「worldType」數據類型,並將其應用於XML實例文檔的元素,旋轉

(rotation)、星系(galaxy)的行星信息需要按照原schema中「worldType」複雜類型的次序被包含進來。這樣,一種 新的數據類型就出現了,我們可以用它來描述包含行星信息的世界。

當然,在這樣一個簡單的schema文檔中使用<xsd:redefine>機制也許意義不大,但是這裏使用這個簡單例子的目的是使我們易於了解它的使用。

極為重要的標籤:<xsd:import> tag[編輯]

<xsd:import> 標籤用於導入schema 文檔以及與此schema文檔所定義數據類型相關的命名空間。這使

XML schema文檔能夠通過使用命名空間名稱(前綴)來引用一個類型庫。下面來看一個簡單的描述店鋪的XML 實例文檔,它 使用了多個命名空間名稱:

<?xml version="1.0" encoding="UTF-8"?>
<store:SimpleStore 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.opentourism.org/xmtext/Store SimpleStore.xsd"
<!-- Note the explicitly defined namespace declarations, the prefix store
represents data types defined in the     
<code>http://www.opentourism.org/xmtext/Store</code> namespace and the
prefix MGR represents data types defined in the 
<code>http://www.opentourism.org/xmtext/CoreSchema</code> namespace.
Also, notice that there is no default namespace declaration – every element
and attribute must be associated with a namespace (we will see this is
necessary weh we examine the schema document)  
-->
<font color="darkblue">
'''xmlns:store="http://www.opentourism.org/xmtext/Store"'''</font>
<font color="darkblue">
'''xmlns:MGR="http://www.opentourism.org/xmtext/CoreSchema">'''</font>
<store:Store>
<MGR:Name xmlns:MGR=" http://www.opentourism.org/xmtext/CoreSchema ">
<MGR:FirstName>Micahel</MGR:FirstName>
<MGR:MiddleNames>Jay</MGR:MiddleNames> 
<MGR:LastName>Fox</MGR:LastName>
</MGR:Name> 
<store:StoreName>The Gap</store:StoreName>
<store:StoreAddress > 
<store:Street>86 Nowhere Ave.</store:Street>
<store:City>Los Angeles</store:City> 
<store:State>CA</store:State>
<store:ZipCode>75309</store:ZipCode> 
</store:StoreAddress>
<!-- More store information would go here. --> 
</store:Store>
<!-- More stores would go here. --> 
</store:SimpleStore>

表 7-5 XML 實例文檔 – store.xml


來看看下面這個schema文檔如何使用 <xsd:import> 標籤從一個類型庫(外部schema 文檔)中導入數

據類型:

<?xml version="1.0" encoding="UTF-8"?>
<store:SimpleStore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:store="http://www.opentourism.org/xmtext/Store" xmlns:MGR="http://www.opentourism.org/xmtext/CoreSchema" xsi:schemaLocation=" http://www.opentourism.org/xmtext/Store SimpleStore.xsd">
	<!-- Note the explicitly defined namespace declarations, the prefix store represents data types defined in the <code>http://www.opentourism.org/xmtext/Store</code> namespace and the prefix MGR represents data types defined in the <code>http://www.opentourism.org/xmtext/CoreSchema</code> namespace. Also, notice that there is no default namespace declaration – every element and attribute must be associated with a namespace (we will see this is necessary weh we examine the schema document) -->
	<store:Store>
		<MGR:Name xmlns:MGR=" http://www.opentourism.org/xmtext/CoreSchema ">
			<MGR:FirstName>Micahel</MGR:FirstName>
			<MGR:MiddleNames>Jay</MGR:MiddleNames> 
			<MGR:LastName>Fox</MGR:LastName>
		</MGR:Name> 
		<store:StoreName>The Gap</store:StoreName>
		<store:StoreAddress > 
			<store:Street>86 Nowhere Ave.</store:Street>
			<store:City>Los Angeles</store:City> 
			<store:State>CA</store:State>
			<store:ZipCode>75309</store:ZipCode> 
		</store:StoreAddress>
		<!-- More store information would go here. --> 
	</store:Store>
	<!-- More stores would go here. --> 
</store:SimpleStore>

表 7-6: XML Schema SimpleStore.xsd


與include標籤、redefine標籤相同,import標籤可以使外部schema文檔中的數據類型引入到另一個schema文檔中,但

這種引用必須在任何元素和屬性聲明之前進行。這些機制對於XML schemas 模塊化起了非常重要的作用,它們使類型庫得以 在多個schema文檔中被維護和使用。

整體大於各個部分的總和:
Schema 模塊化
[編輯]

在介紹了三種引用外部XML schemas的方法之後,我們分析一下這些機制的重要性。在編寫程序代碼時常常感到頭痛的是代

碼的冗餘問題,在自定義數據類型時同樣存在這一問題。假如一個自定義數據類型已經存在並應用於某一schema文檔中的元 素,那麼在新的schema文檔中是應該繼續使用這個數據類型,還是再創建一次?此外,如果一個簡單數據類型將在多個應用 程序中被多次重用,那麼當需要這個數據類型時,是否應該採用某種方法引用它?

Schema模塊化的目的是檢驗schema的作用,選取一種或多種形式頻繁使用的數據類型,創建一個類型庫。需要時可不斷向

庫中加入更複雜的schemas,重用類型庫中的數據類型,必要時可重新定義那些數據類型。舉一個重用的例子,一個消費者信 息的schema:當不同部門僅需要消費者的部分信息時,它們使用不同的schema。但是有時即使不是所有的部門,也是大多數 部門都需要消費者的一些特定信息,如姓名與合同信息,這些信息應該可以引入到各個部門的schema文檔進行共享。

Schema模塊化是一種「最佳實踐」(best practice)。通過建立類型庫,重用和重定義庫中的數據類型,XML schema

文檔將變得既不過分冗餘,也易於閱讀和理解。文檔的可讀性是十分重要的,因為使用這些schemas的並不只有你一個,讓其 他人可以輕鬆地理解schema文檔也很重要。

「不僅要選擇,還要明智地選擇……」: Schema 替代方案[編輯]

本書到目前為止僅僅討論了由World Wide Web協會(W3C)定義的XML schemas。但仍然存在其它方法可以定義那些包含

在XML實例文檔中的數據,這裏只提兩種最流行和為人熟知的替代方法:文檔類型定義(Document Type Definition,

DTD)和Relax NG Schema。
由於本書介紹的是W3C XML schema,所以我們不會詳細描述這些替代方法。DTD是第一種用於定義包含在XML文檔中的數據

的方法,並沿用至今。如果熟悉HTML中的DTD,就會知道在XML中它們的功能是一樣的,但是DTD並不善於定義XML文檔中的 特殊數據類型。而Relax NG schema則誕生較晚,具有許多與W3C XML schema相同的功能;Relax NG聲稱它更加簡單, 更易於掌握,但這是比較主觀的看法。


想了解更多的DTD相關知識,請訪問://www.w3schools.com/dtd/dtd_intro.asp
想了解更多的Relax NG相關知識,請訪問: http://www.relaxng.org/

複習[編輯]

現在我們需要回過頭去複習一下目前為止所學過的全部schema 數據類型、元素、屬性(其中可能有些我們還未介紹過)。

下面的表格將詳細描述那些可在XML schema 中使用的XML數據類型,元素和屬性:

簡單類型

類型 使用
xsd:anyURI 示例 <xsd:element name = 「url」 type = 「xsd:anyURI」 /> '
合法取值示例 http://www.w3.com
約束條件 length, minLength, maxLength, pattern, enumeration, whitespace
xsd:boolean 示例 <xsd:element name = 「hasChildren」 type = 「xsd:boolean」 /> '
合法取值示例 true or false or 1 or 0
約束條件 pattern and whitespace
xsd:byte 示例 <xsd:element name = 「stdDev」 type = 「xsd:byte」 /> '
合法取值示例 -128 through 127
約束條件 length, minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace, and totalDigits
xsd:date 示例 <xsd:element name = 「dateEst」 type = 「xsd:date」 /> '
合法取值示例 2004-03-15
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whitespace
xsd:dateTime 示例 <xsd:element name = 「xMas」 type = 「xsd:dateTime」 /> '
合法取值示例 2003-12-25T08:30:00
Constraining facets minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, and whitespace
xsd:decimal 示例 <xsd:element name = 「pi」 type = 「xsd:decimal」 /> '
合法取值示例 3.1415292
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, fractionDigits, and totalDigits
xsd:double 示例 <xsd:element name = 「pi」 type = 「xsd:double」 /> '
合法取值示例 3.1415292 or INF or NaN
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:duration 示例 <xsd:element name = 「MITDuration」 type = 「xsd:duration」 /> '
合法取值示例 P8M3DT7H33M2S
xsd:float 示例 <xsd:element name = 「pi」 type = 「xsd:float」 /> '
合法取值示例 3.1415292 or INF or NaN
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:gDay 示例 <xsd:element name = 「dayOfMonth」 type = 「xsd:gDay」 /> '
合法取值示例 ---11
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:gMonth 示例 <xsd:element name = 「monthOfYear」 type = 「xsd:gMonth」 /> '
合法取值示例 --02--
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:gMonthDay 示例 <xsd:element name = 「valentine」 type = 「xsd:gMonthDay」 /> '
合法取值示例 --02-14
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:gYear 示例 <xsd:element name = 「year」 type = 「xsd:gYear」 /> '
合法取值示例 1999
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:gYearMonth 示例 <xsd:element name = 「birthday」 type = 「xsd:gYearMonth」 /> '
合法取值示例 1972-08
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:ID 示例 <xsd:attribute name="id" type="xsd:ID"/> '
合法取值示例 id-102
約束條件 length, minLength, maxLength, pattern, enumeration,   and whitespace
xsd:IDREF 示例 <xsd:attribute name="version" type="xsd:IDREF"/> '
合法取值示例 id-102
約束條件 length, minLength, maxLength, pattern, enumeration,   and whitespace
xsd:IDREFS 示例 <xsd:attribute name="versionList" type="xsd:IDREFS"/> '
合法取值示例 id-102 id-103 id-100
約束條件 length, minLength, maxLength, pattern, enumeration,   and whitespace
xsd:int 示例 <xsd:element name = 「age」 type = 「xsd:int」 /> '
合法取值示例 77
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration, whitespace, and totalDigits
xsd:integer 示例 <xsd:element name = 「age」 type = 「xsd:integer」 /> '
合法取值示例 77
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:long 示例 <xsd:element name = 「cannelNumber」 type = 「xsd:int」 /> '
合法取值示例 214
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace
xsd:negativeInteger 示例 <xsd:element name = 「belowZero」 type = 「xsd:negativeInteger」 /> '
合法取值示例 -123
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, and totalDigits
xsd:nonNegativeInteger 示例 <xsd:element name = 「numOfchildren」 type = 「xsd:nonNegativeInteger」 /> '
合法取值示例 2
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, and totalDigits
xsd:nonPositiveInteger 示例 <xsd:element name = 「debit」 type = 「xsd:nonPositiveInteger」 /> '
合法取值示例 0
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, and totalDigits
xsd:positiveInteger 示例 <xsd:element name = 「credit」 type = 「xsd:positiveInteger」 /> '
合法取值示例 500
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, and totalDigits
xsd:short 示例 <xsd:element name = 「numOfpages」 type = 「xsd:short」 /> '
合法取值示例 476
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   whitespace, and totalDigits
xsd:string 示例 <xsd:element name = 「name」 type = 「xsd:string」 /> '
合法取值示例 Joeseph
約束條件 length, minLength, maxLength, pattern, enumeration,   whitespace, and totalDigits
xsd:time 示例 <xsd:element name = 「credit」 type = 「xsd:time」 /> '
合法取值示例 13:02:00
約束條件 minInclusive, maxInclusive, minExclusive, maxExclusive, pattern, enumeration,   and whitespace,

Schema 元素
( 來源於 http://www.w3schools.com/schema/schema_elements_ref.asp )

元素 釋義
all 規定其子元素可以任意順序出現。每個子元素可出現0或1 次
annotation 指定進行schema註釋的第一級元素
any 允許文檔創建者使用未列入schema 的元素來擴展XML文檔
anyAttribute 允許文檔創建者使用未列入schema 的屬性來擴展XML文檔
appInfo 規定用於該應用程式的信息(必須列在annotation之內)
attribute 定義一個屬性
attributeGroup 定義一個用於複雜類型定義的屬性組
choice <choice>聲明中包含的多個元素只允許出現一個
complexContent 對含有混合內容或只含有多個元素的複雜類型進行擴展定義和約束條件的設置
complexType 定義一個複雜類型元素
documentation 定義schema中的文本註釋(必須列在annotation之內)
element 定義一個元素
extension 擴展已存在的simpleType 或 complexType 元素
field 指定一個XPath表達式,用於限定定義特徵約束(identity constraint)的值
group 定義一組用於複雜類型定義的元素
import 將多個具有不同目標命名空間的schema加入一個文檔
include 將多個具有相同目標命名空間的schema加入一個文檔
key 在一個實例文檔中指定一個屬性或元素為key(唯一的,非空的,總是存在的),該文檔必須包含該元素
keyref 規定某個屬性或元素的值與key或唯一元素相相對應
list 將一個簡單類型元素定義為一串值的列表
notation 描述XML文檔中非XML數據的格式
redefine 重新定義來自外部schema的簡單類型和複雜類型,組,以及屬性組
restriction 定義 simpleType,simpleContent,或complexContent的約束條件
schema 定義一個schema的根元素
selector 指定一個XPath 表達式,為特徵約束選擇一個元素集合
sequence 指定子元素必須按順序排列,每個子元素可以出現0到任意次
simpleContent 含有對全文本的複雜類型或無元素的簡單類型的擴展及約束條件
simpleType 定義一個簡單類型,並對屬性值或全文本元素值的約束條件和信息進行規定
union 定義一個特定簡單數據類型值集合的簡單類型
unique 定義一個元素或屬性,其值在規定範圍內是唯一值

Schema 數據類型的約束條件及其他
( 來源於 http://www.w3schools.com/schema/schema_elements_ref.asp )

約束條件 描述
enumeration 定義可接受值列表
fractionDigits 指定所允許的十進制的最大位數,必須大於或等於0
length 指定所允許的字符或列表項目的精確數目,必須大於或等於0
maxExclusive 指定數值的上限(數值必須小於該值)
maxInclusive 指定數值的上限(數值必須小於或等於該值)
maxLength 指定所允許的字符或列表項目的最大數目,必須大於或等於0
minExclusive 指定數值的下限(數值必須大於該值)
minInclusive 指定數值的下限(數值必須大於或等於該值)
minLength 指定字符或列表項目的最小數目,必須大於或等於0
pattern 定義可接受的準確的字符排列順序
totalDigits 指定所允許的數字位數的精確數目,必須大於0
whiteSpace 指定如何處理空白符(開始符,tab,空格,和結束符)

實例文檔屬性
這些屬性在schema中無需聲明

屬性 使用
xsi:nil 解釋 表示某特定元素沒有值或值不確定。在schema文檔中該元素必須設為nillable


<xsd:element name=」last_name」 type=」xsd:string」 nillable=true」/>

示例 <full_name xmlns:xsi= 」http://www.w3.org/2001/XMLSchema-instance”>    <first_name>Madonna</first_name>

<last_name xsi:nil=」true」/> </full_name>

xsi:noNamespaceSchemaLocation 解釋 為不在於任何命名空間的元素定位其schema
示例 <radio xsi:noNamespaceSchemaLocation= 」http://www.opentourism.org/xmtext/radio.xsd”>

<!—radio stuff goes here -- > </radio>

xsi:schemaLocation 解釋 為特定命名空間的元素和屬性定位其schema
示例 <radio xmlns= 」http://www.opentourism.org/xmtext/NS/radio xmlns:xsi= 」http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation= 」http://www.arches.uga.eduNS/radio”http://www.opentourism.org/xmtext/radio.xsd”>

<!—radio stuff goes here -- > </radio>

xsi:type 解釋 用於在實例文檔中表明元素的類型
示例 <height xsi:type=」xsd:decimal」>78.9</height>

總結[編輯]

在複習了主要的XML schema 數據類型、元素和屬性之後,我們回顧一下本章所介紹的新概念。
回想一下本章開頭所介紹的XML命名空間,命名空間用於區分那些具有相同名稱但含義不同的元素(處於不同的

schema中)。命名空間還可以用於為一個簡單的XML應用組合相關的元素和屬性,,使數據處理更加簡便。


本章還介紹了通過使用<xsd:redefine> 標籤,擴展一個已存在的來自類型庫的自定義數據類

型,以得到一個新的數據類型。含有新數據類型的schema將可用於定義該類型的新元素。

另外,我們還學習了如何使用 <xsd:import> 標籤來導入一個schema文檔並將其與命名空間關

聯起來。導入的schema中的元素和屬性可以用命名空間名稱來引用(例如與命名空間名稱綁定的前綴)。

通過include、redefine、import機制,我們可以將不同schema中的元素、屬性和數據類型引入到一個單一的

schema內。這使我們能夠靈活地將schema模塊化,在需要時建立可重用和重定義的類型庫。

請仔細複習上一節中的表格,可以在前面的章節中找到更詳盡的示例。


想了解更多關於XML schema結構、數據類型和工具的知識,請訪問以下網址:

http://www.w3.org/XML/Schema http://www.w3schools.com/schema/default.asp

練習[編輯]

  1. [[The_one-to-one_relationship(XML)#Exercises|在第4章中,曾要求創建一個定義餐館的schema文檔; 在第5章中曾要求創建一個定義特許經營類餐館的schema 文檔。本題要求創建一個新的XML schema文檔,運用redefine機制,通過添加所需的特許經營信息來擴展原先的 餐館數據類型。修改原先的XML 實例文檔,使之成為特許經營類餐館文檔,並利用新創建的XML schema文檔來驗證 其有效性。

  2. 創建一個schema文檔,要求包含下列表格中的類型庫。創建一個新的schema文檔,用於描述一個城市中的酒吧,
    通過import機制導入包含USAddressType 的schema和命名空間,並利用USAddressType來定義酒吧地址。創建一個XML實例文檔,至少包括兩所位於美國任意城市中的酒吧,以檢驗新創建的schema文檔。
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema&quot
    targetNamespace="http://www.opentourism.org/xmtext/NS/USAddress&quot

    xmlns="http://www.opentourism.org/xmtext/NS/USAddress&quot
    elementFormDefault="unqualified"&gt
    <xsd:complexType name="USAddressType"&gt
      <xsd:sequence>
        <xsd:element name="Street" type="xsd:string"/&gt
        <xsd:element name="City" type="xsd:string"/&gt
        <xsd:element name="State" type="xsd:string"/>
        <xsd:element name="ZipCode" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
    </xsd:schema>

    表7-11 练习2 – 类型库 USAddress.xsd – USAddress.xsd
    
  3. 找出第2章練習中 創建的XML實例文檔和XML schema文檔。把
    elementFormDefault 值改為「qualified」,並對XML實例文檔作適當修改,使之能通過有效性檢驗。

  4. 為第4章的tourGuide schema建立類型庫。利用常識組合相關元素和數據類型。記住,某些數據類型可能具有重
    用性,能夠被重用或重新定義。