Schema的定义:
一种文档类型定义,它遵循xml的语言规范。可扩展,支持命名空间
支持更多的数据类型与元素类型。
1. schema的文档结构
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://mynamespace/myschema">
<!--放入实际内容-->
</xs:schema>
解析:1) xs:schema:作为所有Schema文档的根元素
2) xmlns:xs="http://www.w3.org/2001/XMLSchema":用于构造Schema的元素和数据类型来自该命名空间(是固定的)
3)?targetNamespace="http://mynamespace/myschema":本schema定义的元素和数据类型属于http://mynamespace/myschema命名空间。
4) schema文档的后缀名是xsd.
2. schema的数据类型
1)简单类型
A:内置的数据类型(built-in data types)
a 内置的数据类型
b 扩展的数据类型
B: 用户自定义的数据类型(通过simpleType定义)
2)复杂类型(通过complexType定义的数据类型)
3. schema的元素类型
1) schema :作用:包含已经定义的schema;用法<xs:schema xmlns="" targetNamespace=""></xs:schema>
2) element :作用:声明一个元素 ;用法:<xs:element name="" type="" ref="" minOccurs="" maxOccurs="" substitutionGroup="" fixed="" default=""></xs:element>
3) attribute:声明一个属性:用法:<xs:attribute name="" type="" ref="" use=""/>
4) group
5)attributeGroup:作用:把一组属性声明在一起,以便可以被符合类型应用;用法<xs:attributeGroup name="" ref=""><xs:attribute/></xs:attributeGroup>
6)simpleType: 作用:它决定了元素和属性值的约束和相关信息;用法<xs:simpleType name="myType"></xs:simpleType>
其有内容:内容是已经存在的简单类型,三种方式:
A) restrict --> 限定一个范围 ;用法:<xs:restriction base="xs:integer"></xs:restriction>
a.如果restrict里面的子元素是<xs:enumeration value="">:表示多选一
b.如果里面的子元素是:<xs:minlnclusive value="0" /><xs:maxInclusive value=""/>表示限定一个范围
B) list 从列表中选择;用法<xs:list>:从一个特定数据类型的结合中选择定义一个简单类型元素。
C)union 包含一个值的集合;用法<xs:union>
7)simpleContent:作用:应用于complexType,对它的内容进行约束和扩展。<xs:complexType><xs:simpleContent></xs:simpleContent></xs:complexType>
8)complexType:作用:定义一个复合类型,它决定了一组元素和属性值的约束和相关信息;用法:<xs:complexType name=""></xs:complexType>
分析:complexType和simpleType区别:
1) simpleType类型的元素中不能包含元素或者属性。
2)当需要声明一个元素的子元素和/或属性时,用complexType
3)当需要基于内置的基本数据类型定义一个新的数据类型时,用simpleType
9) choice元素:允许唯一的一个元素从一个组中被选择;<xs:choiceType minOccurs="" maxOccurs=""></xs:choiceType>
10) sequence元素 :作用:给一组元素一个特定的序列<xs:sequence minOccurs="' maxOccurs="unbounded"></xs:sequence>
4. 用schema的数据及元素类型声明xml文档的元素和属性
1) 声明元素 <xs:element>
2) 声明属性<xs:attribute name="" type="simpleType | complexType" use="required | optional">