(本文屬于自摸心得)XML Schema的作用是對XML文檔進行約束,其中包含許多內嵌數據類型并且自定義類型。SOAP標準、WSDL都使用XML Schema來進行數據類型格式的限制,如果想繼續深入使用webservice這些知識不可避免的要先學習,下面先從一個最簡單的XML開始,然后一步步擴展、增加復雜度,最后在寫出對應的XML Schema。
1
<?xml version="1.0"?>
2
<note>
3
<to>George</to>
4
<from>John</from>
5
<heading>Reminder</heading>
6
<body>Don't forget the meeting!</body>
7
</note>

2

3

4

5

6

7

上面是個簡單的XM ,根元素是note、子元素是to、from、heading、body。那么對應的XML Schema:
1
<?xml version="1.0"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
targetNamespace="http://www.w3school.com.cn"
4
xmlns="http://www.w3school.com.cn"
5
elementFormDefault="qualified">
6
7
<xs:element name="note">
8
<xs:complexType>
9
<xs:sequence>
10
<xs:element name="to" type="xs:string"/>
11
<xs:element name="from" type="xs:string"/>
12
<xs:element name="heading" type="xs:string"/>
13
<xs:element name="body" type="xs:string"/>
14
</xs:sequence>
15
</xs:complexType>
16
</xs:element>
17
18
</xs:schema>

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

命名空間不必多說
complexType表示:如果一個元素內部包含超過至少2個以上的元素,那么該元素就被認為是復雜類型元素。
sequence表示:子元素按照順序依次列出。(這里的sequence可以替換為all、choice。all代表可以按照任意順序出現、choice代表選擇性即非此即彼)
xs:string代表XML Schema的內置數據類型也不必多說即表示字符串,內置數據類型有許多種這里不一一列舉。
如果根據這個XML Schema書寫XML即可寫成上面的XML文檔。
如果note有屬性,即以下這種形式:
1
<?xml version="1.0"?>
2
<note name="must">
3
<to>George</to>
4
<from>John</from>
5
<heading>Reminder</heading>
6
<body>Don't forget the meeting!</body>
7
</note>
那么與之對應的XML Schema為:
2

3

4

5

6

7

1
<?xml version="1.0"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
targetNamespace="http://www.w3school.com.cn"
4
xmlns="http://www.w3school.com.cn"
5
elementFormDefault="qualified">
6
7
<xs:element name="note">
8
<xs:complexType>
9
<xs:sequence>
10
<xs:element name="to" type="xs:string"/>
11
<xs:element name="from" type="xs:string"/>
12
<xs:element name="heading" type="xs:string"/>
13
<xs:element name="body" type="xs:string"/>
14
</xs:sequence>
15
<xs:attribute name="must" type="xs:string" use="required">
16
</xs:complexType>
17
</xs:element>
18
19
</xs:schema>
其中屬性緊跟著定義在元素之后(sequence與complextype之間),use="required"表示該屬性必須出現,即must
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

如果note里面的子元素比如to要出現兩次(表示不但要給喬治說也要給老大說。。。),XML如下所示:
1
<?xml version="1.0"?>
2
<note name="must">
3
<to>George</to>
4
<to>老大</to>
5
<from>John</from>
6
<heading>Reminder</heading>
7
<body>Don't forget the meeting!</body>
8
</note>
那么對應的XML Schema約束可以為:
2

3

4

5

6

7

8

1
<?xml version="1.0"?>
2
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
3
targetNamespace="http://www.w3school.com.cn"
4
xmlns="http://www.w3school.com.cn"
5
elementFormDefault="qualified">
6
7
<xs:element name="note">
8
<xs:complexType>
9
<xs:sequence>
10
<xs:element name="to" type="xs:string" maxOccurs="2" minOccurs="1"/>
11
<xs:element name="from" type="xs:string"/>
12
<xs:element name="heading" type="xs:string"/>
13
<xs:element name="body" type="xs:string"/>
14
</xs:sequence>
15
<xs:attribute name="must" use="required"/>
16
</xs:complexType>
17
</xs:element>
18
19
</xs:schema>
其中maxOccurs表示最多出現2次,minOccurs最少出現1次
2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

未完待續。。。