1.簡介
XSD即XML結構定義, XML Schemas Definition。其自己就是用xml描述的, 且遵循xml語法規則。一份XML schema文件描述了XML文檔的結構.
基本規則:
.必須以 XML 聲明開頭
.必須擁有惟一的根元素
.標籤必須與結束標籤相匹配
.元素對大小寫敏感
.全部的元素都必須關閉
.全部的元素都必須正確地嵌套
.必須對特殊字符使用實體
<!-- xsd -->
<?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>
<!-- 對應的xml一個例子 -->
<?xml version="1.0"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
2.定義一個元素
<xs:element name="xxx" type="yyy"/>
<xs:element表示定義一個元素
xxx 指元素的名稱,yyy 指元素的數據類型。XML Schema 擁有不少內建的數據類型
3.定義一個屬性
<xs:attribute name="xxx" type="yyy"/>
xs:attribute 表示定義一個屬性
xxx 指屬性名稱,yyy 則規定屬性的數據類型。XML Schema 擁有不少內建的數據類型
4.自定義簡單類型(用於複合元素)
<xs:simpleType name="sinceType">
<xs:restriction base="xs:int">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="goal" type="sinceType"/>
xs:simpleType 表示接下來是簡單類型的定義. 而使用此類型的元素,通常被稱做複合元素
5.自定義結構體(用於複合元素)
<xs:complexType name="combinationType">
<xs:element name="Name" type="xs:string" />
<xs:element name="Age" type="xs:string" />
</xs:complexType>
<xs:element name="person" type="combinationType"/>
xs:complexType 表示接下來是結構體的定義. 而使用此類型的元素,通常被稱做複合元素.
6.混合內容的自定義類型元素
<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>
xs:complexType mixed="true" 指明瞭letter裏邊的內容是混合內容, 其能夠是以下形式:
<letter>
Dear Mr.<name>John Smith</name>.
Your order <orderid>1032</orderid>
will be shipped on <shipdate>2001-07-13</shipdate>.
</letter>
7.約束條件
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示接下來是對元素/屬性的約束
8.可選的和必需出現(屬性)
<xs:attribute name="lang" type="xs:string" use="required"/>
use 表示出現的要求,required/optional/prohibited, 分別是必須出現/可選/不出現
9.出現次數(元素)
<xs:element name="child_name" type="xs:string" maxOccurs="10" minOccurs="0"/>
minOccurs 表示最小出現的次數,並由數值/unbounded 來指定
maxOccurs 表示最大出現的次數,並由數值/unbounded 來指定
10.默認值和固定值(元素/屬性)
<xs:element name="pen" type="xs:string" default="minipen"/>
<xs:attribute name="color" type="xs:string" fixed="red"/>
default 指定默認值, fixed 指定該元素值固定
11.數據類型限制(元素/屬性)
<xs:element name="Name">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="2" />
<xs:maxLength value="4" />
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:restriction 表示對元素/屬性的限制.
限制種類有如下類別:
.length // 定義所容許的字符或者列表項目的精確數目。必須大於或等於0。
.minLength // 定義所容許的字符或者列表項目的最小數目。必須大於或等於0。
.maxLength // 定義所容許的字符或者列表項目的最大數目。必須大於或等於0。
.minInclusive // 定義數值的下限。所容許的值必需大於或等於此值。
.maxInclusive // 定義數值的上限。所容許的值必須小於或等於此值。
.minExclusive // 定義數值的下限。所容許的值必需大於此值。
.maxExclusive // 定義數值的上限。所容許的值必須小於此值。
.enumeration // 定義可接受值的一個列表
.pattern // 定義可接受的字符的精確序列(規則表達式)。
.fractionDigits // 定義所容許的最大的小數位數。必須大於等於0
.totalDigits // 定義所容許的阿拉伯數字的精確位數。必須大於0。
.whiteSpace // 定義空白字符(換行、回車、空格以及製表符)的處理方式。
12.空白字符的限制
<xs:element name="address">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:whiteSpace value="preserve"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
xs:whiteSpace 指明對空白字符的限制, 取值有preserve/collapse/replace, 一次是保留/只保留字符中間必須的空格,且多個合併爲一個/所有去掉
13.子元素順序
<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>
子元素順尋有:
.xs:all // 順序不限, 每一個子元素只准出現一次
.xs:choice // 順序不限, 每一個子元素出現次數不限
.xs:sequence // 順序固定, 每一個子元素出現次數不限
14.元素組
<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>
xs:group 指明定義一個元素組或者引用一個已經定義的元素組. 這樣能夠減小沒必要要的重複書寫.
15.屬性組
<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>
xs:attributeGroup 指明定義一個屬性組或者引用一個已經定義的屬性組. 這樣能夠減小沒必要要的重複書寫.
16.不限定子元素
<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="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
xs:any 指明能夠包含一個不限定子元素. 如如下xml也是符合xsd的:
<person>
<firstname>David</firstname>
<lastname>Smith</lastname>
<children>
<childname>mike</childname>
</children>
</person>
17.不限定屬性
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
<xs:anyAttribute minOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
anyAttribute 指明能夠包含一個不限定屬性
18.替換元素
<xs:element name="name" type="xs:string"/>
<xs:element name="navn" substitutionGroup="name"/>
substitutionGroup 指明本元素能夠替換的元素. 可替換元素的類型必須和主元素相同,同時二者必須是全局元素。
如如下xml片斷都是合法的:
<customer>
<name>John Smith</name>
</customer>
<customer>
<navn>John Smith</navn>
</customer>
19.阻止被替換
<xs:element name="name" type="xs:string" block="substitution"/>
<xs:element name="navn" substitutionGroup="name"/>
block 指明阻止的內容,這裏爲被替換. 可替換元素的類型必須和主元素相同,同時二者必須是全局元素。
本例就阻止用任何來替換name的聲明,如下xml片斷是錯誤的:
<customer>
<navn>John Smith</navn>
</customer>
20.全局元素/本地元素
全局元素指 "schema" 元素的直接子元素
本地元素(Local elements)指嵌套在其餘元素中的元素
21.xsd內置數據類型
xs:byte // 字節型,8位二進制位長
xs:unsignedByte // 無符號字節型
xs:short // 短整型, 16位二進制位長
xs:unsignedShort // 無符號短整型
xs:integer // 整型, 32位二進制位長
xs:positiveInteger // 只有正值的整型
xs:nonPositiveInteger // 只有非正值的整型
xs:negativeInteger // 只有負值的整型
xs:nonNegativeInteger // 只有非負值的整型
xs:int // 整型, 32位二進制位長
xs:long // 長整型, 64位二進制位長
xs:unsignedLong // 無符號長整型
xs:float // 浮點數, 32位二進制位長
xs:double // 雙精度, 64位二進制位長
xs:string // 字符串
xs:boolean // 合法的布爾值是 true、false、1(表示 true) 以及 0(表示 false)
xs:hexBinary // 十六進制編碼的二進制數據
xs:base64Binary // Base64 編碼的二進制數據
xs:date // 日期
xs:decimal // 貨幣類型
xs:anyURI // 假如某個 URI 含有空格,請用 替換它們
22.調用其餘程序/xml外部類型
<xs:notation name="gif" public="image/gif" system="view.exe"/>
xs:notation 表示此元素非xml類型, 可指定解析或執行的程序.
xml文檔中的內容相似以下:
<image type="gif">demopic.gif</image>
23.註釋
<xs:annotation>
<xs:documentation xml:lang="zh">
這是一份用於企業僱員信息描述的模式文檔
</xs:documentation>
</xs:annotation>
"<!--"和"-->"仍可使用,可是爲了方便其餘讀者和應用程序來理解模式文檔,XML Schema提供了三個元素來爲模式提供註解:
.xs:annotation // 做爲documentation/appinfo的父元素
.xs:documentation // 放置適合人閱讀的信息
.xs:appinfo // 工具、樣式表和其餘應用程序提供信息
24.命名空間
名稱空間聲明的通常形式爲
第一部分是一個關鍵字xmlns:
第二部分是名稱空間的前綴(標識符)
第三部分是一個等號
第四部分是雙引號
第五部分是名稱空間標識URI
須要注意的是,名稱空間的前綴不能爲xml,由於在XML中這個字符串是保留做特殊用途的。
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
</xs:schema>
25.源命名空間
在Schema中的定義和聲明也能夠引用其餘的命名空間,咱們能夠把這種命名空間取名爲源命名空間(source namespaces)。每個Schema能夠有多個源命名空間。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</xs:schema>
26.目標命名空間
每個Schema能夠有且只有一個目標命名空間。在一個給定的Schema中,每個名稱都是屬於一個特定的名字空間的, 同時能夠由targetNamespace來命名
<?xml version=」1.0」>
<xsd:schema xmlns:xsd=」http://www.w3.org/XML_Schema」 targetNamespace=「http://www.test.com/ns/ns_test「>
</xsd:schema>
xsd文件中定義了一個targetNameSpace後,其內部定義的元素,屬性,類型等都屬於該targetNameSpace
其自身或外部xsd文件使用這些元素,屬性等都必須從定義的targetNameSpace中找
27.引入xsd命名空間
<xs:schema xmlns:xs