一、什么事Schema(XSD)
XML Schema是微軟定義的一套用來驗證XML技術。是一套預先規定的XML元素和屬性創建的,這些元素和屬性定義了XML文檔的結構和內容模式。
DTD的局限性:
1、DTD不遵循XML語法。
2、DTD的數據類型有限,與數據庫類型不一致。
3、DTD不可以擴展。
4、DTD是不支持命名空間的。
Schema的優勢:
1、Schema是一種XML語法結構,編寫更加方便。
2、Schema可以支持數據類型。
3、Schema是可以擴展的。
4、Schema支持命名空間。
二、Schema文檔結構
Schema文檔本身是一個XML文檔,所以必須滿足XML文檔結構。在每個Schema文檔中必須包含一個根元素<xs:schema>。
<?xml version="1.0"?> <!--每個Schema中都必須包含xs:schema根節點,然后在根節點中定義內容!--> <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:schema>
說明:
xmlns:xs="http://www.w3.org/2001/XMLSchema":顯示 schema 中用到的元素和數據類型來自命名空間 "http://www.w3.org/2001/XMLSchema"。同時它還規定了來自命名空間 "http://www.w3.org/2001/XMLSchema" 的元素和數據類型應該使用前綴 xs:
targetNamespace="http://mynamespace/myschema" :顯示被此 schema 定義的元素驗證的XML來自的命名空間。
xmlns="http://www.w3school.com.cn" :指定默認的命名空間是 。
elementFormDefault="qualified" :指目標XML是否遵循本Schema,qualified表示遵循,unqualified表示不遵循。
三、在XML中引用Schema文檔
<?xml version="1.0"?> <!--引用Schema文檔--> <note xmlns="http://www.w3school.com.cn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3school.com.cn note.xsd"> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
說明:
xmlns="http://www.w3school.com.cn" :規定了默認命名空間的聲明。此聲明會告知 schema 驗證器,在此 XML 文檔中使用的所有元素都被聲明於 "http://www.w3school.com.cn" 這個命名空間。
xsi:schemaLocation="http://www.w3school.com.cn note.xsd":指定文件路徑。
四、Schema數據類型:
Schema中支持豐富的數據類型,可以簡單分為簡單類型和復合類型。
1、簡單類型包括:
1)內置的數據類型。
a、基本的數據類型
| 基本的數據類型 | |
| 數據類型 | 描述 |
| string | 字符串 |
| boolean | 布爾類型 |
| decimal | 特定精度的數字 |
| float | 單精度浮點數 |
| double | 雙精度浮點數 |
| duration | 表示持續時間/日期格式 |
| dateTime | 完整日期格式 |
| time | 代表時分秒 |
| date | 代表日期 |
b、擴展的數據類型
| 擴展的數據類型 | |
| 數據類型 | 描述 |
| ID | 用於唯一表示元素 |
| IDREF | 應用ID元素的屬性或屬性 |
| ENTITY |
實體類型 |
| long |
表示長整型:-9223372036854775808~9223372036854775807 |
| int | 表示整型:-2147483648~--2147483647 |
| short | 表示短整型:-32768~32767 |
| byte |
整型:-128~127 |
2)用戶自定義的簡單類型(通過simpleType定義)
2、復合類型(通過complexType定義)
五、Schema中的元素類型
1、根元素:schema。
包含已經定義的schema。
屬性:
xmlns:schema的命名空間。
targetNamespace:要驗證的XML文件的命名空間。
elementFormDefault:要驗證的XML是否遵循當前的驗證命名空間。
2、用於定義元素和屬性的元素:element、attribute、group、attributeGroup。
1)element:聲明一個元素
屬性:
name:需要限定XML文檔元素的名稱。
type:需要限定XML文檔元素的類型。
ref:引用外部定義的元素
minOccurs:元素最小出現的次數。
maxOccurs:元素最大出現的次數。
例:聲明student節點,類型為string類型,最小出現1次,最多出現3次。
<xs:element name="student" type="xs:string" minOccurs="1" maxOccurs="unbounded" />
2)group:聲明一個分組
將多個元素聲明放到一個分組中,然后在其他元素中通過group引用。
屬性:name分組的名稱
例:將name和age定義為一個分組,然后在student中引用這個分組。
<!--外部定義一個標記--> <xs:element name="student"> <xs:complexType> <!--引用分組標記--> <xs:group ref="stuinfo" maxOccurs="unbounded"></xs:group> </xs:complexType> </xs:element> <!--定義一個分組標記--> <xs:group name="stuinfo"> <xs:sequence> <!--自定義的子元素:name和age,name類型為string,age類型為byte,只能出現1次--> <xs:element name="name" type="xs:string"></xs:element> <xs:element name="age" type="xs:byte"></xs:element> </xs:sequence> </xs:group>
3)attribute元素:
用於聲明一個屬性:
屬性:
name:屬性名稱
type:屬性類型
user:是否必選,required必選,optional可選,默認可選
default:默認值
fixed:固定值
注意:默認值和固定值不能同時出現。
示例:屬性的用法
Schema文檔:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="students"> <xs:complexType> <xs:sequence> <xs:element name="student" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="name" type="xs:string" /> <xs:attribute name="age" type="xs:byte" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
XML文檔:
<?xml version="1.0" encoding="UTF-8"?> <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd"> <student name="zhangsan" age="29"/> <student name="lisi" age="19"/> </students>
4)attributeGroup屬性組
和前面的元素組類型,在外部定義屬性組,然后在元素中引用屬性組。
示例:
Schema文檔:
<!--定義屬性組:stuattgroup--> <xs:attributeGroup name="stuattgroup"> <xs:attribute name="name" type="xs:string"/> <xs:attribute name="age" type="xs:byte"/> </xs:attributeGroup> <!--引用屬性組--> <xs:element name="student" maxOccurs="unbounded"> <xs:complexType> <xs:attributeGroup ref="stuattgroup"></xs:attributeGroup> </xs:complexType> </xs:element>
3、用於定義簡單類型:simpleType。
它決定了元素和屬性值得約束和相關信息。
屬性:name
常用的兩種方式:
1)restriction:約束
對現有類型進行擴充
示例:設置年齡必須在18-100之間
<xs:attributeGroup name="stuattgroup"> <xs:attribute name="name" type="xs:string"/> <!--將原來的xs:int換位age類型--> <xs:attribute name="age" type="age"/> </xs:attributeGroup> <!--擴展int類型,設置int取值只能是18到100,包含18和100--> <xs:simpleType name="age"> <xs:restriction base="xs:int"> <xs:minInclusive value="18" /> <xs:maxInclusive value="100" /> </xs:restriction> </xs:simpleType>
2)list:列表
從一個特定的數據類型的集合中選擇定義一個簡單類型元素。
Schema文檔:
<!--Schema文檔--> <!--定義列表數據,其每項子元素為自定義擴展類型--> <xs:simpleType name="stuScore"> <xs:list itemType="stuScoreItem"></xs:list> </xs:simpleType> <!--定義擴展類型限定0-100之間--> <xs:simpleType name="stuScoreItem"> <xs:restriction base="xs:int"> <xs:minInclusive value="0" /> <xs:maxInclusive value="100" /> </xs:restriction> </xs:simpleType> <!--聲明Scores節點,包含list格式數據--> <xs:element name="scores" type="stuScore"> </xs:element> <!--XML文檔中的數據--> <scores xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="scores.xsd"> 10 20 300 </scores>
4、用於定義復雜類型:complexType。
需要使用在節點下包含子節點的情況。
Schema文檔:
<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xs:element name="students"> <xs:complexType> <xs:sequence> <!--通過ref來應用student標記 maxbounded表示不限定出現的次數--> <xs:element ref="student" maxOccurs="unbounded"></xs:element> </xs:sequence> </xs:complexType> </xs:element> <!--外部定義一個標記--> <xs:element name="student"> <xs:complexType> <xs:sequence> <!--自定義的子元素:name和age,name類型為string,age類型為byte,只能出現1次--> <xs:element name="name" type="xs:string"></xs:element> <xs:element name="age" type="xs:byte"></xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
XML文檔:
<?xml version="1.0" encoding="UTF-8"?> <students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="students.xsd"> <student> <name>張三</name> <age>20</age> </student> <student> <name>李四</name> <age>20</age> </student> </students>
5、用於進行約束:choice、list、sequence、restriction。
1)choice
把一組屬性聲明組合到一起,一邊被復合類型所應用,XML中只能出現限定選項中的一個元素。
例:
Schema文檔:
<!--設定交通工具只能是自行車,小汽車,摩托車中的一項--> <xs:element name="交通工具"> <xs:complexType> <xs:choice> <xs:element name="自行車" type="xs:string"></xs:element> <xs:element name="小汽車" type="xs:string"></xs:element> <xs:element name="摩托車" type="xs:string"></xs:element> </xs:choice> </xs:complexType> </xs:element>
XML文檔:
<交通工具 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vehicle.xsd"> <自行車></自行車> <!-- <小汽車></小汽車> <摩托車></摩托車> 包含任意一個,包含多個會報錯! --> </交通工具>
2)sequence
表示元素必須按照規定的序列進行顯示。
