XML Schema

XML Schema<一>

       (本文属于自摸心得)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 >

上面是个简单的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 >

命名空间不必多说
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为:
 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
如果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约束可以为:
 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次

未完待续。。。

你可能感兴趣的:(XML Schema)