XML引入多scheme文件约束简单示例
XML引入多scheme文件约束简单示例,用company.xsd和department.xsd来约束company.xml:
company.xsd
<?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"></element> </sequence> <!-- 为employee元素添加属性 --> <attribute name="age" type="int"></attribute> </complexType> </element> </sequence> </complexType> </element> </schema>
department.xsd
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/department" elementFormDefault="qualified"> <!-- 部门名称 --> <element name="name" type="string"></element> </schema
company.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 数据文件 引用多个Schema --> <company xmlns = "http://www.example.org/company" xmlns:dept="http://www.example.org/department" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/company company.xsd http://www.example.org/department department.xsd" > <employee age="30"> <!-- 部门名称 --> <dept:name>人力资源部</dept:name> <!-- 员工名称 --> <name>王晓晓</name> </employee> </company>