Schema是新的xml约束,他与DTD的作用相同。
Schema与DTD的区别
- DTD约束文件扩展名是dtd。
- Schema约束文件扩展名xsd而不是xml。
- DTD不是xml。
- Schema本身就是xml。
- Schema比DTD强大,他有更加详细的类型。
students.xsd
<?xml version="1.0"?>
<xsd:schema xmlns="http://www.itcast.cn/xml"
xmlns:xsd="http://www.w2.org/2001/XMLSchema"
targetNamespace="http://www.itcast.cn/xml" elementFormDefault="qualitied">
<xsd:element name="students" type="studentsType"/>
<!-- 上句指定students元素,类型为studentsType类型 -->
<xsd:complexType name="studentsType"> <!-- 定义studentsType类型 -->
<xsd:sequence> <!-- 指子元素必须按顺序出现 -->
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded">
<!-- 上句指studentsType类型包含0~N个student元素 -->
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence> <!-- 上句指子元素必须按顺序出现 -->
<xsd:element name="name" type="xsd:string"/> <!-- studentType类型顺序包含子元素name,类型为字符串类型 -->
<xsd:element name="age" type="ageType"/> <!-- studentType类型顺序包含子元素age,类型为ageType类型 -->
<xsd:element name="sex" type="sexType"/>
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/> <!-- studentType类型包含属性number,类型为numberType,改属性是必须的 -->
</xsd:complexType>
<xsd:simpleType name="sexType"> <!-- 定义类型sexType -->
<xsd:restriction base="xsd:string"> <!-- 该类型是对string进行约束 -->
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/> <!-- 指定枚举选项-->
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType"> <!-- 定义类型ageType -->
<xsd:restriction base="xsd:integer"> <!-- 该类型是对integer进行约束 -->
<xsd:minInclusive valuse="0"/>
<xsd:maxInclusive valuse="100"/> <!-- 范围为0~100 -->
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType"> <!-- 定义类型numberType -->
<xsd:restriction base="xsd:string">
<xsd:pasttern value="ITCAST_\D{4}"/> <!-- 指定正则表达式 -->
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
students.xml
<?xml version="1.0"?>
<students xmlns="http://www.itcast.cn/xml"
xlmns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.itcast.cn/xml students.xsd"> <!-- 指定schema约束文件 -->
<student number="ITCAST_1001"> <!-- 子元素student,其中number属性是必须的,而且number属性值必须匹配正则表达式ITCAST_d[4] -->
<name>zhangsan</name>
<age>23</age>
<sex>male</sex>
</student>
<student number="ITCAST_1002">
<name>lisi</name>
<age>22</age>
<sex>female</sex>
</student>
Schema名称空间
targetNamespace指定当前xsd文件中定义的元素、属性、类型都在这个名称空间中,在xsd中指定与java的package相似。
1、指定目标名称空间
targetNamespace="http://www.itcast.cn/xml"
2、xsi:schemaLocation:与java的import相似,在xml中关联xsd文件,它可以关联多个xsd文件。
xsi:schemaLocation=”目标名称空间 xsd文件路径”
<students schemaLocation="http://www.itcast.cn/xml
students.xsd"
http://www.qdmmy6.com/xml
qdmmy6.xsd> <!-- 关联两个xsd文件,使用其中定义的元素和属性 -->
</students>
3、在xml中,指定名称空间前缀(定义名称空间)
- 在xml中定义名空间
格式:xmlns:前缀=”目标名称空间”
<q6:students xmlns:it="http://www.itcast.cn/xml"
xmlns:q6="http://www.qdmmy6.com/xml"
schemaLocation="http://www.itcast.cn/xml
students.xsd
http://www.qdmmy6.com/xml
qdmmy6.xsd"> <!-- 关联两个xsd文件 -->
<it:student> <!--使用元素前缀区别约束 -->
<q6:name>zhangsan</q6:name>
</it:student>
</q6:students>
- 定义默认名称空间
格式:xmlns=”目标名称空间”
注意:xmlns(xml name space)是特殊的属性,但schemaLocation不是特殊的属性,而且它没有指定前缀。它是W3C提供的,我们需要再定义一个前缀的名称空间,然后给schemaLocation使用。
<!-- 下面元素不带前缀的都在默认名称空间中 -->
<students xmlns:it="http://www.itcast.cn/xml"
xmlns="http://www.qdmmy6.com/xml"
xlmns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.itcast.cn/xml
students.xsd
http://www.qdmmy6.com/xml
qdmmy6.xsd"> <!-- 关联两个xsd文件 -->
<it:student> <!--使用元素前缀区别约束 -->
<name>zhangsan</name>
</it:student>
</students>