1.基本用法
参考链接:
添加链接描述
2.XML的文档约束
2.1 DTD约束
- 定义约束
- 引入约束,使用约束
中间直接写内容,不需要
2.2 Schema约束(模式约束)
在根元素前面加不加前缀都可以
加 xsd:schema
不加 schema
该约束文档本质上就是一个xml文件
使用这个约束文件中定义的元素或者属性,必须使用前缀xsd
创建新的命名空间
命名空间是一组具有结构的名称的集合,是XML正式标准的补充部分
以下详细信息:可参考:添加链接描述
- 定义约束
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.kk.com/xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.kk.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="sex" type="sexType"/>
</xsd:sequence>
<xsd:attribute name="number" type="numberType" use="required"/>
</xsd:complexType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minExclusive value="0"/>
<xsd:maxExclusive value="200"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="sexType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="女"/>
<xsd:enumeration value="男"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="numberType">
<xsd:restriction base="xsd:string">
<!--数字,最多允许有4个数字-->
<xsd:pattern value="hehe_\d{4}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
- 使用约束
<?xml version="1.0" encoding="UTF-8" ?>
<!--使用自己写的命名空间和w3c的命名空间-->
<students xmlns="http://www.kk.com/xml"
xmlns:xss="http://www.w3.org/2001/XMLSchema-instance"
xss:schemaLocation="http://www.kk.com/xml studentsSchema.xsd">
<student number="hehe_1234">
<name>小河</name>
<age>20</age>
<sex>女</sex>
</student>
<student number="hehe_3456">
<name>小海</name>
<age>19</age>
<sex>女</sex>
</student>
</students>