XML Schema 是基于 XML 的 DTD 替換者。
XML Schema 描寫 XML 文檔的結構。
XML Schema 語言也稱作 XMLSchema 定義(XML Schema Definition,XSD)。
實例
<?xml version="1.0"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget themeeting!</body> </note>
下面這個例子是1個名為"note.xsd" 的 XML Schema 文件,它定義了上面那個 XML 文檔的元素:<?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified"> <xs:element name="note"> <xs:complexType> <xs:sequence> <xs:element name="to" type="xs:string"/> <xs:element name="from" type="xs:string"/> <xs:element name="heading"type="xs:string"/> <xs:element name="body" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
規定了默許命名空間的聲明。此聲明會告知schema 驗證器,在此 XML 文檔中使用的所有元素都被聲明于 "http://www.w3school.com.cn" 這個命名空間。
1旦具有了可用的 XML Schema 實例命名空間:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
您就能夠使用 schemaLocation屬性了。此屬性有兩個值。第1個值是需要使用的命名空間。第2個值是供命名空間使用的 XML schema 的位置:
xsi:schemaLocation="http://www.w3school.com.cnnote.xsd"
簡易元素
簡易元素指那些只包括文本的元素。它不會包括任何其他的元素或屬性。文本有很多類型。它可以是 XML Schema 定義中包括的類型中的1種(布爾、字符串、數據等等),或它也能夠是您自行定義的定制類型。
定義簡易元素的語法:
<xs:element name="xxx" type="yyy"/>
最經常使用的類型是:
xs:string
xs:decimal
xs:integer
xs:boolean
xs:date
xs:time
簡易元素的默許值和固定值
<xs:element name="color" type="xs:string" default="red"/> <xs:element name="color" type="xs:string" fixed="red"/>
屬性
義屬性的語法是:
<xs:attribute name="xxx" type="yyy"/>
可選的和必須的屬性
在缺省的情況下,屬性是可選的。如需規定屬性為必選,請使用 "use" 屬性:
<xs:attribute name="lang" type="xs:string" use="required"/>
屬性也可設置默許值和固定值,與元素的設置方法相似
限定
對值的限定
下面的例子定義了帶有1個限定且名為"age" 的元素。age 的值不能低于 0 或高于 120:
<xs:element name="age"> <xs:simpleType> <xs:restriction base="xs:integer"> <xs:minInclusive value="0"/> <xs:maxInclusive value="120"/> </xs:restriction> </xs:simpleType> </xs:element>
對1組值的限定如需把 XML 元素的內容限制為1組可接受的值,要使用枚舉束縛(enumeration constraint)。下面的例子定義了帶有1個限定的名為 "car" 的元素。可接受的值只有:Audi, Golf, BMW:
<xs:element name="car"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType> </xs:element>
上面的例子也能夠被寫為:
<xs:element name="car" type="carType"/> <xs:simpleType name="carType"> <xs:restriction base="xs:string"> <xs:enumeration value="Audi"/> <xs:enumeration value="Golf"/> <xs:enumeration value="BMW"/> </xs:restriction> </xs:simpleType>
注釋:在這類情況下,類型"carType" 可被其他元素使用,由于它不是 "car" 元素的組成部份。對1系列值的限定
如需把 XML 元素的內容限制定義為1系列可以使用的數字或字母,我們要使用模式束縛(pattern constraint)。可以使用正則表達式進行束縛。下面的例子定義了帶有1個限定的名為 "letter" 的元素。可接受的值只有小寫字母 a - z 其中的1個:
<xs:element name="letter"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[a-z]"/> </xs:restriction> </xs:simpleType> </xs:element>
下1個例子定義了帶有1個限定的名為"initials" 的元素。可接受的值是大寫字母 A - Z 其中的3個:<xs:element name="initials"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="[A-Z][A-Z][A-Z]"/> </xs:restriction> </xs:simpleType> </xs:element>
數據類型的限定限定 描寫
enumeration 定義可接受值的1個列表
fractionDigits 定義所允許的最大的小數位數。必須大于等于0。
length 定義所允許的字符或列表項目的精確數目。必須大于或等于0。
maxExclusive 定義數值的上限。所允許的值必須小于此值。
maxInclusive 定義數值的上限。所允許的值必須小于或等于此值。
maxLength 定義所允許的字符或列表項目的最大數目。必須大于或等于0。
minExclusive 定義數值的下限。所允許的值必須大于此值。
minInclusive 定義數值的下限。所允許的值必須大于或等于此值。
minLength 定義所允許的字符或列表項目的最小數目。必須大于或等于0。
pattern 定義可接受的字符的精確序列。
totalDigits 定義所允許的阿拉伯數字的精確位數。必須大于0。
whiteSpace 定義空白字符(換行、回車、空格和制表符)的處理方式。
復合元素
有4種類型的復合元素:
l 空元素
l 包括其他元素的元素
l 僅包括文本的元素
l 包括元素和文本的元素
在 XML Schema 中,有兩種方式來定義復合元素:
1. 通過命名此元素,可直接對"employee"元素進行聲明,就像這樣:
<xs:element name="employee"> <xs:complexType> <xs:sequence> <xs:element name="firstname"type="xs:string"/> <xs:element name="lastname"type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
注:被包圍在唆使器 <sequence>中的子元素必須以它們被聲明的次序出現2."employee" 元素可使用 type 屬性,這個屬性的作用是援用要使用的復合類型的名稱:
<xs:element name="employee" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType>
如果使用了上面所描寫的方法,那末若干元素都可以使用相同的復合類型,比如這樣:<xs:element name="employee" type="personinfo"/> <xs:element name="student" type="personinfo"/> <xs:element name="member" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType>
也能夠在已有的復合元素之上以某個復合元素為基礎,然后添加1些元素,就像這樣:<xs:element name="employee" type="fullpersoninfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:element name="firstname"type="xs:string"/> <xs:element name="lastname"type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="fullpersoninfo"> <xs:complexContent> <xs:extension base="personinfo"> <xs:sequence> <xs:element name="address"type="xs:string"/> <xs:element name="city"type="xs:string"/> <xs:element name="country"type="xs:string"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>
僅含文本的復合元素可包括文本和屬性。此類型僅包括簡易的內容(文本和屬性),因此我們要向此內容添加 simpleContent 元素。當使用簡易內容時,我們就必須在 simpleContent 元素內定義擴大或限定
下面這個例子聲明了1個復合類型,其內容被定義為整數值,并且 "shoesize" 元素含著名為 "country" 的屬性:
<xs:element name="shoesize"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country" type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:element>
也可為 complexType 元素設定1個名稱,并讓"shoesize" 元素的 type 屬性來援用此名稱(通過使用此方法,若干元素都可援用相同的復合類型):<xs:element name="shoesize" type="shoetype"/> <xs:complexType name="shoetype"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="country"type="xs:string" /> </xs:extension> </xs:simpleContent> </xs:complexType>
XML 元素,"letter",含有文本和其他元素:<letter> DearMr.<name>John Smith</name>. Your order<orderid>1032</orderid> will be shippedon <shipdate>2001-07⑴3</shipdate>. </letter>
下面這個 schema 聲明了這個"letter" 元素:<xs:element name="letter"> <xs:complexType mixed="true"> <xs:sequence> <xs:element name="name"type="xs:string"/> <xs:element name="orderid"type="xs:positiveInteger"/> <xs:element name="shipdate"type="xs:date"/> </xs:sequence> </xs:complexType> </xs:element>
注釋:為了使字符數據可以出現在"letter" 的子元素之間,mixed 屬性必須被設置為 "true"。<xs:sequence>標簽 (name、orderid 和 shipdate ) 意味著被定義的元素必須順次出現在"letter" 元素內部。唆使器
有7種唆使器:
Order 唆使器:All Choice Sequence
Occurrence 唆使器:maxOccurs minOccurs
Group 唆使器:Groupname attributeGroup name
All 唆使器
<all> 唆使器規定子元素可以依照任意順序出現,且每一個子元素必須只出現1次:
<xs:element name="person"> <xs:complexType> <xs:all> <xs:element name="firstname"type="xs:string"/> <xs:element name="lastname"type="xs:string"/> </xs:all> </xs:complexType> </xs:element>
注釋:當使用 <all> 唆使器時,可以把<minOccurs> 設置為 0 或 1,而只能把 <maxOccurs> 唆使器設置為 1Choice 唆使器
<choice> 唆使器規定可出現某個子元素或可出現另外1個子元素(非此即彼):
<xs:element name="person"> <xs:complexType> <xs:choice> <xs:element name="employee" type="employee"/> <xs:element name="member" type="member"/> </xs:choice> </xs:complexType> </xs:element>
提示:如需設置子元素出現任意次數,可將<maxOccurs> (稍后會講授)設置為 unbounded(無窮次)。Sequence 唆使器
<sequence>規定子元素必須依照特定的順序出現:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element>
Occurrence 唆使器用于定義某個元素出現的頻率。注釋:對所有的"Order" 和 "Group" 唆使器(any、all、choice、sequence、group name 和 group reference),其中的 maxOccurs 和 minOccurs 的默許值均為 1。
maxOccurs 唆使器
<maxOccurs>唆使器可規定某個元素可出現的最大次數:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"/> </xs:sequence> </xs:complexType> </xs:element>
上面的例子表明,子元素"child_name" 可在 "person" 元素中最少出現1次(其中 minOccurs 的默許值是 1),最多出現 10 次。minOccurs 唆使器
<minOccurs>唆使器可規定某個元素能夠出現的最小次數:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="full_name" type="xs:string"/> <xs:element name="child_name" type="xs:string" maxOccurs="10"minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
上面的例子表明,子元素"child_name" 可在 "person" 元素中出現最少0 次,最多出現 10 次。提示:如需使某個元素的出現次數不受限制,請使用maxOccurs="unbounded" 這個聲明:
元素組
元素組通過 group 聲明進行定義:
<xs:group name="組名稱">
...
</xs:group>
必須在 group 聲明內部定義1個 all、choice 或 sequence 元素。下面這個例子定義了名為 "persongroup" 的 group,它定義了必須依照精確的順序出現的1組元素:
<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group>
在把 group 定義終了以后,就能夠在另外一個定義中援用它了:<xs:group name="persongroup"> <xs:sequence> <xs:element name="firstname" type="xs:string"/> <xs:element name="lastname" type="xs:string"/> <xs:element name="birthday" type="xs:date"/> </xs:sequence> </xs:group> <xs:element name="person" type="personinfo"/> <xs:complexType name="personinfo"> <xs:sequence> <xs:group ref="persongroup"/> <xs:element name="country" type="xs:string"/> </xs:sequence> </xs:complexType>
屬性組屬性組通過 attributeGroup 聲明來進行定義:
下面這個例子定義了名為"personattrgroup" 的1個屬性組:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname"type="xs:string"/> <xs:attribute name="lastname"type="xs:string"/> <xs:attribute name="birthday"type="xs:date"/> </xs:attributeGroup>
在已定義終了屬性組以后,就能夠在另外一個定義中援用它了,就像這樣:
<xs:attributeGroup name="personattrgroup"> <xs:attribute name="firstname"type="xs:string"/> <xs:attribute name="lastname"type="xs:string"/> <xs:attribute name="birthday"type="xs:date"/> </xs:attributeGroup> <xs:element name="person"> <xs:complexType> <xs:attributeGroup ref="personattrgroup"/> </xs:complexType> </xs:element>
<any> 元素
<any> 元素使我們有能力通過未被schema 規定的元夙來拓展 XML 文檔
下面這個例子是從名為"family.xsd" 的 XML schema 中援用的片斷。它展現了1個針對 "person" 元素的聲明。通過使用 <any> 元素,我們可以通過任何元素(在 <lastname> 以后)擴大 "person" 的內容:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname"type="xs:string"/> <xs:element name="lastname"type="xs:string"/> <xs:any minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element>
現在,我們希望使用"children" 元夙來擴大 "person" 元素。這此種情況下我們就能夠這么做,即便以上這個 schema 的作者沒有聲明任何 "children" 元素。請看這個 schema 文件,名為"children.xsd":
<?xml version="1.0" encoding="ISO⑻859⑴"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified"> <xs:element name="children"> <xs:complexType> <xs:sequence> <xs:element name="childname"type="xs:string" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
下面這個 XML 文件(名為"Myfamily.xml"),使用了來自兩個不同的 schema 中的成份,"family.xsd" 和"children.xsd":<?xml version="1.0" encoding="ISO⑻859⑴"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.comfamily.xsd http://www.w3school.com.cn children.xsd"> <person> <firstname>David</firstname> <lastname>Smith</lastname> <children> <childname>mike</childname> </children> </person> <person> <firstname>Tony</firstname> <lastname>Smith</lastname> </person> </persons>
上面這個 XML 文件是有效的,這是由于schema "family.xsd" 允許我們通過在 "lastname" 元素后的可選元夙來擴大 "person" 元素。<any> 和<anyAttribute> 都可用于制作可擴大的文檔,它們使文檔有能力包括未在主 XML schema 中聲明過的附加元素。
下面的例子是來自名為"family.xsd" 的 XML schema 的1個片斷。它為我們展現了針對 "person" 元素的1個聲明。通過使用 <anyAttribute> 元素,我們就能夠向 "person" 元素添加任意數量的屬性:
<xs:element name="person"> <xs:complexType> <xs:sequence> <xs:element name="firstname"type="xs:string"/> <xs:element name="lastname"type="xs:string"/> </xs:sequence> <xs:anyAttribute/> </xs:complexType> </xs:element>
現在,我們希望通過"gender" 屬性來擴大 "person" 元素。在這類情況下我們就能夠這樣做,即便這個 schema 的作者從未聲明過任何 "gender" 屬性。請看這個 schema 文件,名為"attribute.xsd":
<?xml version="1.0" encoding="ISO⑻859⑴"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3school.com.cn" xmlns="http://www.w3school.com.cn" elementFormDefault="qualified"> <xs:attribute name="gender"> <xs:simpleType> <xs:restriction base="xs:string"> <xs:pattern value="male|female"/> </xs:restriction> </xs:simpleType> </xs:attribute> </xs:schema>
下面這個 XML(名為"Myfamily.xml"),使用了來自不同 schema 的成份,"family.xsd" 和"attribute.xsd":<?xml version="1.0" encoding="ISO⑻859⑴"?> <persons xmlns="http://www.microsoft.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.microsoft.comfamily.xsd http://www.w3school.com.cnattribute.xsd"> <person gender="female"> <firstname>Jane</firstname> <lastname>Smith</lastname> </person> <person gender="male"> <firstname>David</firstname> <lastname>Smith</lastname> </person> </persons>
上一篇 log4j配置(詳細版)――――詳細的日志才是解決bug的王道
下一篇 解決 Previous operation has not finihsed; run cleanup if it was interrupted Please