ML Schema 是基于 XML 的 DTD 替代者。
XML Schema 可描述 XML 文档的结构。
访问:http://www.w3.org/2001/XMLSchema,“A (non-normative) DTD XMLSchema.dtd for XML Schema”(http://www.w3.org/2001/XMLSchema.dtd),本身是用dtd约束的。
demo1.xsd
complexType:复合类型。pets元素中包含cat和dog两个元素
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo01" elementFormDefault="qualified">
<xs:element name="cat" type="xs:string" />
<xs:element name="dog" type="xs:string" />
<xs:complexType name="mytype">
<xs:sequence maxOccurs="2" minOccurs="1">
<!-- 要求 cat dog元素按顺序出现 -->
<xs:element ref="cat" />
<xs:element ref="dog" />
</xs:sequence>
</xs:complexType>
<xs:element name="pets" type="mytype" />
</xs:schema>
demo1.xml
<?xml version="1.0" encoding="UTF-8"?>
<pets xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo01.xsd">
<cat >mao</cat>
<dog >gou</dog>
</pets>
demo2.xsd group:将元素组合一起进行生命。暂时思考认为:便于复用的目的。如果除了person元素外,还有student元素,仍可复用使用。
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/demo02" elementFormDefault="qualified">
<!-- Group元素。作用:把一组元素声明组合在一起,以便他们能够一起被复合类型应用 -->
<xs:group name="myGroup">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="birthday" type="xs:date"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:group>
<xs:element name="person">
<xs:complexType>
<xs:group ref="myGroup"/>
</xs:complexType>
</xs:element>
</xs:schema>
demo2.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo02.xsd">
<name>eg366</name>
<birthday>1987-04-18</birthday>
<age>25</age>
</person>
demo3.xsd
attribute:声明属性
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo03" elementFormDefault="qualified">
<!-- attribute元素 -->
<xs:attribute name="interest" type="xs:integer" />
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="hello" type="xs:string" />
<xs:element name="world" type="xs:string" />
</xs:sequence>
<xs:attribute ref="interest" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
demo3.xml
<?xml version="1.0" encoding="UTF-8"?>
<person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo03.xsd"
interest="1">
<hello></hello>
<world></world>
</person>
demo4.xsd
和demo2.xsd中group元素的使用目的应该是类似的,只不过attributeGroup应用于元素的属性
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo04" elementFormDefault="qualified">
<!-- AttributeGroup:把一组属性声明组合在一起,以便可以被复合类型应用 -->
<xs:attributeGroup name="myAttributeGroup">
<xs:attribute name="hello" type="xs:string" use="required" />
<xs:attribute name="world" type="xs:string" use="optional" />
</xs:attributeGroup>
<xs:element name="myElement">
<xs:complexType>
<xs:attributeGroup ref="myAttributeGroup" />
</xs:complexType>
</xs:element>
</xs:schema>
demo4.xml
<?xml version="1.0" encoding="UTF-8"?>
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo04.xsd"
hello="" world=""></myElement>
demo5.xsd
simpleType:简单元素;restriction:限制,对元素的内容进行限制
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo05" elementFormDefault="qualified">
<xs:simpleType name="myType">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="100" />
</xs:restriction>
</xs:simpleType>
<xs:element name="hello" type="myType" />
</xs:schema>
demo5.xml
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo05.xsd">100</hello>
demo6.xsd
指定元素内容的值,用枚举类型进行限制
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo05" elementFormDefault="qualified">
<xs:simpleType name="myType">
<xs:restriction base="xs:integer">
<xs:enumeration value="5" />
<xs:enumeration value="7" />
<xs:enumeration value="9" />
</xs:restriction>
</xs:simpleType>
<xs:element name="hello" type="myType" />
</xs:schema>
demo6.xml
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo06.xsd">5</hello>
demo7.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo07" elementFormDefault="qualified">
<!-- xs:list 从一个特定数据类型的集合中选择定义一个简单类型 -->
<xs:simpleType name="myType">
<xs:list itemType="xs:integer" />
</xs:simpleType>
<xs:element name="hello" type="myType" />
</xs:schema>
demo7.xml
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo07.xsd">
1 2 3 4 5
</hello>
demo8.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo08" elementFormDefault="qualified">
<!-- 属性:联合类型rodbikesize或mountainbikesize,用空格分隔各个组合 -->
<xs:attribute name="allFrameSize">
<xs:simpleType>
<xs:union memberTypes="rodbikesize mountainbikesize" />
</xs:simpleType>
</xs:attribute>
<xs:simpleType name="rodbikesize">
<xs:restriction base="xs:integer">
<xs:enumeration value="46" />
<xs:enumeration value="55" />
<xs:enumeration value="60" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="mountainbikesize">
<xs:restriction base="xs:string">
<xs:enumeration value="small" />
<xs:enumeration value="medium" />
<xs:enumeration value="large" />
</xs:restriction>
</xs:simpleType>
<xs:element name="hello">
<xs:complexType>
<xs:sequence>
<xs:element name="welcome" type="xs:string" />
</xs:sequence>
<xs:attribute ref="allFrameSize" use="required" />
</xs:complexType>
</xs:element>
</xs:schema>
demo8.xml
<?xml version="1.0" encoding="UTF-8"?>
<hello xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo08.xsd" allFrameSize="46">
<welcome>
</welcome>
</hello>
complexType和simpleType的区别
simpleType类型的元素中不能包含元素或属性
(1)当需要声明一个元素的子元素和/或属性时,用complexType(复合类型)
(2)当需要基于一个内置的基本数据类型定一个新的数据类型时,用simpleType。simpleType类型的元素没有子元素,也没有属性。
demo9.xsd(simpleContent)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo09" elementFormDefault="qualified">
<!-- simpleContent:应用于complexType,对他的内容进行约束及扩展 -->
<!-- simpleContent用于complexType元素上,用于限定该complexType的内容类型,表示该complexType没有子元素,同时该complexType需要有属性,否则就变成simpleType了。 -->
<xs:element name="showsize">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="sizing" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="us" />
<xs:enumeration value="europe" />
<xs:enumeration value="uk" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:schema>
demo9.xml
<?xml version="1.0" encoding="UTF-8"?>
<showsize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo09.xsd"
sizing="us">1.2</showsize
demo10.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo10" elementFormDefault="qualified">
<!-- choice元素:允许唯一的一个元素从一个组中被选择。比restriction-enumeration要灵活,可设置最多、最少出现次数(针对一组元素) -->
<xs:complexType name="myType">
<xs:choice maxOccurs="1" minOccurs="1">
<xs:element name="hello" type="xs:string" />
<xs:element name="world" type="xs:string" />
</xs:choice>
</xs:complexType>
<xs:element name="helloWorld" type="myType" />
</xs:schema>
demo10.xml<?xml version="1.0" encoding="UTF-8"?>
<helloWorld xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo10.xsd">
<hello>1</hello>
<!-- <world>s</world> -->
</helloWorld>
demo11.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/demo11" elementFormDefault="qualified">
<!-- sequence元素:元素需要按照顺序出现且必须出现。(出现次数以一组元素为单位) -->
<xs:complexType name="myType">
<xs:sequence minOccurs="1" maxOccurs="3">
<xs:element name="hello" type="xs:string" />
<xs:element name="world" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:element name="helloWorld" type="myType" />
</xs:schema>
demo11.xml
<?xml version="1.0" encoding="UTF-8"?>
<helloWorld xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/demo11.xsd">
<!-- 出现第一次 -->
<hello>1</hello>
<world>s</world>
<!-- 第二次 -->
<hello>1</hello>
<world>s</world>
<!-- 第三次 -->
<hello>1</hello>
<world>s</world>
</helloWorld>
student.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/student" elementFormDefault="qualified">
<xs:attribute name="xuehao" type="xs:int" />
<xs:complexType name="student">
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="姓名" type="xs:string" />
<xs:element name="性别" type="sex" />
<xs:element name="年龄" type="age" />
</xs:sequence>
<xs:attribute name="学号" type="xs:int" />
</xs:complexType>
<xs:simpleType name="sex">
<xs:restriction base="xs:string">
<xs:enumeration value="男" />
<xs:enumeration value="女" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="age">
<xs:restriction base="xs:int">
<xs:minInclusive value="1" />
<xs:maxInclusive value="150" />
</xs:restriction>
</xs:simpleType>
<xs:element name="学生名册">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="学生" type="student" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
student2.xsd<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.example.org/student" elementFormDefault="qualified">
<xs:element name="学生名册">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="学生">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="unbounded">
<xs:element name="姓名" type="xs:string" />
<xs:element name="性别">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="男" />
<xs:enumeration value="女" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="年龄">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:minInclusive value="1" />
<xs:maxInclusive value="150" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
<xs:attribute name="学号" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
student.xml
<?xml version="1.0" encoding="UTF-8"?>
<学生名册 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="file:///H:/develop/workspace/learn_workspace/demo01/schema/student2.xsd">
<学生 学号="1">
<姓名>张三</姓名>
<性别>男</性别>
<年龄>20</年龄>
</学生>
<学生 学号="2">
<姓名>李四</姓名>
<性别>女</性别>
<年龄>19</年龄>
</学生>
<学生 学号="3">
<姓名>王五</姓名>
<性别>男</性别>
<年龄>21</年龄>
</学生>
</学生名册>