XML引入多scheme文件約束簡單示例

XML引入多scheme文件約束簡單示例,用company.xsd和department.xsd來約束company.xml:spa

company.xsdcode

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

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

<?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>
相關文章
相關標籤/搜索