XML系列:XML Schema
一,什麼是XML Schema?
1,從XML文檔看:
用XML編寫的,用於描述XML文檔結構,驗證XML文檔的結構和數據類型有效性的規則。
2,從自身組成來看:
Schema是遵循DTD的XML文檔,定義Schema的結構。DTD與分析程序一起驗證XML文檔的有效性。
3,從數據來看:
是描述數據存儲的模型。Schema的二種重要模型:Microsoft XML Schema和W3C XML Schema。
二,XML和DTD
DTD的缺點:
只描述XML文檔結構,不支持數據類型檢查。
不支持命名空間。
不可擴展性。
數據類型有限。
Schema的優點正是DTD的缺點,另外還有:
使用XML編寫,具有XML文檔的優點。
可重用性:屬性分組,使關係顯示化等。
模型化。
可自定義數據類型。
三,Microsoft XML Schema 數據類型
要使用任意一種 XML Schema 數據類型,需指定XML Schema 數據類型的命名空間。
1,內置和派生數據類型:
ID, IDREF, IDREFS
ENTITY, ENTITIES
NMTOKEN ,NMTOKENS
NOTATION
數值類型:
Number:表數字值;
Decimal:表任意精度的十進制數,基礎類型是Number;
float和double表浮點數,使用時用Decimal替代。
Integer:表整型,基礎類型是Number;
Real:表指數,基礎類型是Number;
Binary:表二進制數據的字符串;
字符類型:
String:Unicode字符集。
Char:字符。
日期類型:
DateTime:給定日期的特定時間的字符串。表示格式爲CCYY-MM_DDThh:mm:ss的時間。
Date:表示日期。表示CCYY-MM-DD格式的時間。
Time:表示時間,基礎類型是DateTime。表示HH:MM:SS格式的時間。
gYearMonth 表示CCYY-MM格式的時間 。
gYear 表示CCYY格式的時間 。
gMonthDay 表示-MM-DD格式的時間 -16-04 。
gDay 表示-DD格式的時間 -16 。
gMonth 表示-MM格式的時間 。
TimePeriod:表DateTime開始和結束之間的時間。
布爾類型:
Boolean:true(數值1) 或 false(數值0);
等;
2, 自定義數據類型。
提供了一種功能強大的複雜數據類型定義機制,可以實現包括結構描述在內的複雜的數據類型。
三,Microsoft XML Schema 中的元素
Schema 中的元素和屬性使用 <element> 和 <attribute> 標籤來聲明。
1,schema元素
XML Schema的根元素,以下爲屬性:
http://www.w3school.com.cn/schema/el_schema.asp
(1)xmls:建立Schema元素的命名空間。
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema">
<!-- Schema 主體-->
</xs:schema>
(2)taggetNameSpace:指定用戶自己的命名空間,指定該屬性的時候要指定xmls爲taggetNameSpace中指定的命名空間。
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
taggetNameSpace = "http://www.mySpace.com"
xmls = "http://www.mySpace.com">
<!-- Schema 主體-->
</xs:schema>
(3)elementFormDefault:可選,表用戶自己的命名空間中的元素是否受約束(是否遵循本Schema),unqualfied或qualified。
(4)attributeFormDefault:可選,表用戶自己的命名空間中的樹新風是否受約束(是否遵循本Schema),unqualfied或qualified。
(5)versoin:可選,版本號。
2,element元素
聲明元素,以下爲常用屬性
http://www.w3school.com.cn/schema/el_element.asp
(1)name:元素名;
(2)type:元素數據類型;
(3)ref:引用已定義元素;
(4)fixed:元素值是否可以改變,不能與default一起使用。
(5)default:元素的默認值;
(6)substitutionGroup:表元素的父類;
(7)minOccurs:元素在父元素類型中出現的最少次數:該值可以是大於或等於零的整數,默認值爲 1;
(8)maxOccurs :元素在父元素中出現的最多次數:該值可以是大於或等於零的整數,默認值爲 1;
若不想對最大次數設置任何限制,使用字符串 "unbounded"。 默認值爲 1;
例子:
<?xml version="1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
taggetNameSpace = "http://www.mySpace.com"
xmls = "http://www.mySpace.com">
....
<xs:element name = "name" type = "string" minOcurs = "0" maxOccurs = "*"/>
<xs:element name = "sex" type = "string" minOcurs = "0" maxOccurs = "*"/>
...
</xs:schema>
3,attribute元素
定義一個屬性。
http://www.w3school.com.cn/schema/el_attribute.asp
name:屬性名。
ref:引用已定義屬性。name 和 ref 屬性不能同時出現。如果 ref 出現,則 simpleType 元素、form 和 type 不能出現。
type:屬性類型,type 屬性只能在內容不包含 simpleType 元素時出現。
fixed:屬性固定值,不能與default一起使用。
default:屬性類型的默認值;
required:屬性是否必須,yes或 no;
例子1:
<xs:attribute name = "state" type = "xs:boolean" default = "true" required = "yes"/>
例子2:
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:complexType name="someComplexType">
<xs:attribute ref="code"/>
</xs:complexType>
4,simpleType元素
定義一個簡單類型,規定與具有純文本內容的元素或屬性的值有關的信息以及對它們的約束。
http://www.w3school.com.cn/schema/el_simpletype.asp
id:唯一標識號;
name:元素名。在所有 simpleType 和 complexType 元素之間必須是唯一的;
如果 simpleType 元素是 schema 元素的子元素,則爲必選項,在其他時候則是不允許的。
例子:age元素的約束:值在0 - 100之間。
<xs:element name="age">
<xs:simpleType>
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
或
<xs:simpleType name = "ageSim">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:element name = "age" type = "ageSim"/>
例子: code屬性有一個限定。唯一可接受的值是大寫字母 A 到 Z 中的兩個字母。
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
5,complexType元素
定義複雜類型。複雜類型的元素是包含其他元素和/或屬性的 XML 元素。
http://www.w3school.com.cn/schema/el_complextype.asp
例子:
<xs:complexType name = "studentType">
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
<xs:element name = "sex" type = "xs:string"/>
</xs:sequence>
<xs:attribute name = "state" type = "xs:boolean"/>
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<xs:element name = "student" type = "studentType"/>
6,sequence元素
要求子元素必須按順序出現。每個子元素可出現 0 到任意次數。
http://www.w3school.com.cn/schema/el_sequence.asp
(1)minOccurs:元素在父元素中出現的最少次數:該值可以是大於或等於零的整數,默認值爲 1。
要組是可選的,將此屬性設置爲0。 默認值爲 1;
(2)maxOccurs:元素在父元素中出現的最多次數:該值可以是大於或等於零的整數。
最大次數不設置任何限制,使用字符串 "unbounded"。 默認值爲 1。
例子:針對 type 元素的聲明,可包含零個或多個student 和teacher 元素:
<xs:complexType name = "typeType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name = "student" type = "studentType"/>
<xs:element name = "teacher" type = "teacherType"/>
</xs:sequence>
</xs:complexType>
<xs:element name = "type" type = "typeType">
7,group元素
用於定義在複雜類型定義中使用的元素組;以下爲常用屬性:
http://www.w3school.com.cn/schema/el_group.asp
(1)name:組名,僅當 schema 元素是該 group 元素的父元素時才使用該屬性。
在此情況下,group 是由 complexType、choice 和 sequence 元素使用的模型組。
(2)ref:引用另一個組的名稱。 ref 可以包含命名空間前綴。name 屬性和 ref 屬性不能同時出現。
(3)minOccurs:group 元素可在父元素中出現的最小次數。該值可以是大於或等於零的整數。默認值爲 1;
(4)maxOccurs:group 元素可在父元素中出現的最大次數。該值可以是大於或等於零的整數。
若不想對最大次數設置任何限制,使用字符串 "unbounded"。默認值爲 1;
例子:
<xs:element name="name" type="xs:string"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
<xs:group name="studentGroup">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="sex"/>
</xs:sequence>
</xs:group>
<xs:complexType name="studentType">
<xs:group ref="studentGroup"/>
</xs:complexType>
<xs:element name = "student" type = "studentType"/>
8,attributeGroup元素
用於對屬性聲明進行組合,這樣這些聲明就能夠以組合的形式合併到複雜類型中。
http://www.w3school.com.cn/schema/el_attributegroup.asp
name:屬性組的名稱。name 和 ref 屬性不能同時出現。
ref :對指定的屬性組的引用。name 和 ref 屬性不能同時出現。
例子:
<xs:attributeGroup name = "stuAttGroup">
<xs:attribute name = "state" type = "xs:string"/>
<xs:attribute name = "code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:attributeGroup>
<xs:group name ="studentGroup">
<xs:sequence>
<xs:element name = "name" type = "xs:string" minOccurs = "0" maxOccurs = "unbounded"/>
<xs:element name = "sex" type = "xs:string" minOccurs = "0" maxOccurs = "unbounded"/>
</xs:sequence>
</xs:group>
<xs:complexType name = "studentType">
<xs:group ref = "studentGroup"/>
<xs:attributeGroup ref = "stuAttGroup"/>
</xs:complexType>
<xs:element name = "student" type = "studentType"/>
1 - 8綜合例子:
<?xml version="1.0"?>
<xs:schema xmlns:xs = "http://www.w3.org/2001/XMLSchema"
taggetNameSpace = "http://www.mySpace.com"
xmls = "http://www.mySpace.com">
<xs:element name="name" type="xs:string"/>
<xs:element name="sex" type="xs:string"/>
<xs:element name="subject" type="xs:string"/>
<xs:attribute name="state" type="xs:boolean"/>
<xs:attribute name="code">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[A-Z][A-Z]"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:group name="studentGroup">
<xs:sequence>
<xs:element ref="name"/>
<xs:element ref="sex"/>
</xs:sequence>
</xs:group>
<xs:group name="teacherGroup">
<xs:sequence>
<xs:group ref="studentGroup"/>
<xs:element ref="subject"/>
</xs:sequence>
</xs:group>
<xs:attributeGroup name="attGroup">
<xs:attribute ref="state"/>
<xs:attribute ref="code"/>
</xs:attributeGroup>
<xs:complexType name="studentType">
<xs:group ref="studentGroup"/>
<xs:attributeGroup ref="attGroup"/>
</xs:complexType>
<xs:complexType name="teacherType">
<xs:group ref="teacherGroup"/>
<xs:attributeGroup ref="attGroup"/>
</xs:complexType>
<xs:element name="student" type="studentType"/>
<xs:element name="teacher" type="teacherType"/>
<xs:complexType name="typeType">
<xs:sequence>
<xs:element ref="student"/>
<xs:element ref="teacher"/>
</xs:sequence>
</xs:complexType>
<xs:element name="type" type="typeType" />
</xs:schema>
9,simpleContent元素
包含對 complexType 元素(它以字符數據或 simpleType 元素爲內容)的擴展或限制並且不包含任何元素。
應用於complexType元素中用於包含extension和restriction元素。
父元素: complexType
http://www.w3school.com.cn/schema/el_simpleContent.asp
10,complexContent元素
定義對複雜類型(包含混合內容或僅包含元素)的擴展或限制。
父元素 :complexType .
http://www.w3school.com.cn/schema/el_complexcontent.asp
11,extension元素
對 simpleType 或 complexType 的元素進行擴展
http://www.w3school.com.cn/schema/el_extension.asp
例子:通過添加屬性,對一個已有的 simpleType 進行擴展:
<xs:simpleType name = "subjectSim">
<xs:restriction base="xs:string">
<xs:enumeration value="數學" />
<xs:enumeration value="語文" />
<xs:enumeration value="科學" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name = "teacherType">
<xs:simpleContent>
<xs:extension base = "subjectSim">
<xs:attribute name = "subject">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="防災" />
<xs:enumeration value="生理" />
</xs:restriction>
</xs:simpleType>
</xs:attribute >
</xs:extension>
</xs:simpleContent>
</xs:complexType>
例子:對complexType 元素進行擴展
<xs:complexType name = "studentType">
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
<xs:element name = "sex" type = "xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name = "student" type = "studentType"/>
<xs:complexType name = "teacherType">
<xs:complexContent>
<xs:extension base = "studentType">
<xs:sequence>
<xs:element name = "subject" type = "xs:string"/>
<xs:element name = "wedlock" type = "xs:boolean"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name = "teacher" type = "teacherType"/>
12,restriction元素
定義對 simpleType、simpleContent 或 complexContent 定義的約束。
父元素: complexContent
http://www.w3school.com.cn/schema/el_restriction.asp
id: 可選。規定該元素的唯一的 ID。
base: 必需。規定在該 schema(或由指定的命名空間指示的其他 schema)中定義的內建數據類型、simpleType 或 complexType 元素的名稱。
例子:定義對 simpleType定義的約束
<xs:simpleType name = "ageSim">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="100"/>
</xs:restriction>
</xs:simpleType>
<xs:element name = "age" type = "ageSim"/>
例子:定義對complexContent 定義的約束
<xs:simpleType name = "ageSimStudent">
<xs:restriction base="xs:integer">
<xs:minInclusive value="13"/>
<xs:maxInclusive value="20"/>
</xs:restriction>
</xs:simpleType>
<xs:complexType name = "studentType">
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
<xs:element name = "sex" type = "xs:string"/>
<xs:element name = "age" type = "ageSimStudent"/>
</xs:sequence>
</xs:complexType>
<xs:element name = "student" type = "studentType"/>
<xs:complexType name = "teacherType">
<xs:complexContent>
<xs:restriction base = "studentType">
<xs:sequence>
<xs:element name = "name" type = "xs:string"/>
<xs:element name = "sex" type = "xs:string"/>
<xs:element name = "age" type = "ageSimStudent" default = "18"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name = "teacher" type = "teacherType"/>
例子:定義對 simpleContent 定義的約束
13,list元素
定義單個 simpleType 定義的集合。該屬性把簡單類型定義爲指定數據類型的值的一個列表
http://www.w3school.com.cn/schema/el_list.asp
itemType 在該 schema(或由指定的命名空間指示的其他 schema)中定義的內置數據類型或 simpleType 元素的名稱。
包含 list 元素的 simpleType 元素是從 list 值指定的簡單類型派生的。
list 值必須是限定名 (QName)。 如果內容包含 simpleType 元素,則不允許使用該屬性,否則該屬性是必需的。
14,union元素
定義多個 simpleType 定義的集合。
http://www.w3school.com.cn/schema/el_union.asp
15,unique元素
指定屬性或元素值(或者屬性或元素值的組合)在指定範圍內必須是唯一的。 該值必須唯一或爲零。
http://www.w3school.com.cn/schema/el_unique.asp
unique 元素必須按順序包含下列元素:
selector 元素:selector 元素包含 XPath 表達式,指定一個元素集,在其中由 field 元素指定的值必須唯一。
必須有一個且只有一個 selector 元素。
field 元素:每一個 field 元素均包含一個 XPath 表達式,指定對於由 selector 元素指定的元素集而言必須唯一的值(屬性或元素值)。
如果有多個 field 元素,則 field 元素的組合必須是唯一的。 在此情況下,單個 field 元素的值對於選定元素不一定是唯一的,但所有字段的組合必須是唯一的。
必須有一個或多個 field 元素。
16,patttern元素
待續