1、首先编写XML数据文件
<?xml version="1.0" encoding="UTF-8"?>
<books xmlns="http://www.example.org/book" 名称空间
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 声明是一个schema实例
xsi:schemaLocation="http://www.example.org/book book.xsd"> 说明文件的约束位置
<book>
<name></name>
<price>12</price>
</book>
</books>
2、编写schema约束文档数据
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 引用W3C中文档标准
targetNamespace="http://www.example.org/book" 目标名称空间,为其他文档引用提供方便
elementFormDefault="qualified"> 良好的格式
<element name="books">
<complexType>
<sequence>
<element name="book">
<complexType>
<sequence>
<element name = "name" type = "string"></element>
<element name = "price" type = "double"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
3、引用多个schema文件
(1)、数据文件
<?xml version="1.0" encoding="UTF-8"?>
<company xmlns="http://www.example.org/company"
xmlns:aa = "http://www.example.org/depatment"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/company company.xsd http://www.example.org/depatment depatment.xsd"
>
<employee>
<aa:name>人力资源</aa:name>
<name>孔明</name>
</employee>
</company>
(2)、约束文件
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/company"
elementFormDefault="qualified">
<element name="company">
<complexType>
<sequence>
<element name="employee">
<complexType>
<sequence>
<any></any>
<element name = "name" type = "string"></element>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
(3)约束文件2
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/depatment"
elementFormDefault="qualified">
<element name="name" type="string"></element>
</schema>