前面文章已经介绍过DTD约束文件约束效果不好,这个问题schema可以解决。xsd也是一个文件类型,和前面dtd一样。xsd文件中可以写更详细精确的语法来控制xml文件的内容。例如可以在xsd文件中定义性别的值采用枚举方式,一个为male,另外一个为femal。还可以定义元素的值或者属性值为字符串或者整数类型。下面xsd文件出现过的语法,我会写一遍注释。
1.带schema的student.xsd文件内容
<?xml version="1.0" ?>
<xsd:schema xmlns="http://www.xxx.com/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xxx.com/xml" elementFormDefault="qualified">
<xsd:element name="students" type="studentsType"/>
<xsd:complexType name="studentsType">
<xsd:sequence>
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="studentType">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType"/>
<xsd:element name="gender" type="genderType"/>
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="genderType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="200"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="j2ee_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
2.xml引入student.xsd约束
<?xml version="1.0" encoding="UTF-8"?>
<students
xmlns="http://www.xxx.com/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.xxx.com/xml student.xsd" >
<student number="j2ee_0001">
<name>ZS</name>
<age>18</age>
<gender>male</gender>
</student>
</students>
先来解释下xml文件中如何引用xsd文件的语法
1.编写根标签
2.引入实例名称空间 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3.引入名称空间 xsi:schemaLocation="http://www.xxx.com/xml student.xsd"
4.引入默认的名称空间 http://www.xxx.com/xml
注意三个引入都是些在根标签里,默认的名称空间,这里随便写一个,一般习惯就是一个网页地址。实例名称空间这个不变,正是有了这个w3制定的语法规则,我们在xsd文件里才可以使用字符串,Int整形,枚举等这样的语法来控制。
解释下schema xsd文件内容的含义和语法
<?xml version="1.0" ?>
<xsd:schema xmlns="http://www.xxx.com/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.xxx.com/xml" elementFormDefault="qualified">
<!-- 这里name为students的元素就是根元素,然后自定义了一个studentsType的类型,具体到下面studentsType里定义 -->
<xsd:element name="students" type="studentsType"/>
<!-- 下面这个就是定义studentsType这个类型 -->
<xsd:complexType name="studentsType">
<!-- sequence是有序的意思 -->
<xsd:sequence>
<!-- 定义一个student的元素,类型是下面定义的studetnType类型 后面最小出现次数为0,最大出现次数没有限制 -->
<xsd:element name="student" type="studentType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<!-- 这个是复杂标签的关闭标签 -->
</xsd:complexType>
<!-- 定义一个复杂标签,类型是studentType -->
<xsd:complexType name="studentType">
<!-- 有序控制 -->
<xsd:sequence>
<!-- student标签下三个嵌套子标签,这里就name设置了字符串的类型,其他两个类型在下文定义 -->
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="age" type="ageType"/>
<xsd:element name="gender" type="genderType"/>
</xsd:sequence>
<!-- 定义了student标签的number属性,下面定义了类型,这个属性是必填的 -->
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<!-- 定义一个简单类型,这里定义性别类型 -->
<xsd:simpleType name="genderType">
<!-- 基础类型是字符串 -->
<xsd:restriction base="xsd:string">
<!-- 枚举类型,两个值 -->
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<!-- 基础类型是整形 -->
<xsd:restriction base="xsd:integer">
<!-- 最小年龄0,最大200 -->
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="200"/>
</xsd:restriction>
</xsd:simpleType>
<!-- 定义numberType -->
<xsd:simpleType name="numberType">
<!-- 基础类型是字符串 -->
<xsd:restriction base="xsd:string">
<!-- 正则匹配模式,以j2ee_开头 后面带4位数字 -->
<xsd:pattern value="j2ee_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
根据我上面写的注释,你可以修改student.xml相应位置,看看能不能有相关错误反应。