schema(二)为xml文件添加多个schema约束

当一个xml 文件有多个schema约束是可以一个为默认的,其它的都加前缀。

department.xsd

<?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>

company.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/department"
    xmlns:tns="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>

Result:

schema(二)为xml文件添加多个schema约束