首页 > 代码库 > xml Schema include

xml Schema include

first.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="www.myweb.com"
           xmlns="www.myweb.com">
    <xs:simpleType name="bid">
        <xs:restriction base="xs:string">
            <xs:pattern value="[A]\d{6}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
second.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns="www.myweb.com"
targetNamespace="www.myweb.com">
    <xs:simpleType name="aid">
        <xs:restriction base="xs:string">
            <xs:pattern value="[c]\d{6}"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>
third.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="www.myweb.com"
           xmlns="www.myweb.com">
          
    <xs:include schemaLocation="first.xsd"/>
    <xs:include schemaLocation="second.xsd"/>

    <xs:element name="books" type="infotype"/>
    
    <xs:complexType name="infotype">
        <xs:sequence>
            <xs:element name="book" type="booktype"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="booktype">
        <xs:sequence>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="author" type="atype"/>
        </xs:sequence>
        <xs:attribute name="bookid" type="bid"/>
    </xs:complexType>
    <xs:complexType name="atype">
        <xs:sequence>
            <xs:element name="firstname" type="xs:string"/>
            <xs:element name="lastname" type="xs:string"/>
        </xs:sequence>
        <xs:attribute name="authorid" type="aid"/>
    </xs:complexType>
</xs:schema>

说明:

first.xsd:定义简单类型, 并指定目标命名空间为"www.myweb.com";

second.xsd:定义简单类型, 并指定目标命名空间为"www.myweb.com";

third.xsd:定义复杂类型,并引用first.xsd, second.xsd,引用这两个schema文件前提是目标命名空间一致。因为third.xsd引用了first.xsd, second.xsd所经必须指定默认命名空间为"www.myweb.com",否则无法引用firtst.xsd, second.xsd中定义的简单类型数据;

目标命名空间:指定被此Schema文档约束的元素及数据类型都来自于的命名空间;

默认命名空间:指定当前XML文档中使用的元素及数据类型都来自于的命名空间;

xml Schema include