在一个Schema文档过大或一部分声明需要被多个Schema重用时,可以将部分声明或类型定义分离出来,作为一个单独的Schema文档。如果那个Schema需要使用,可以利用include包含进来。include将一个外部Schema文档包含到现有的Schema文档中。需要注意的是,两个文档的目标命名空间(targetNamespace)必须一致。

include.xsd:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.cnblogs.com">
         <xs:simpleType name="sexType">
            <xs:restriction base="xs:string">
                <xs:enumeration value="Male"/>
                <xs:enumeration value="Female"/>        
            </xs:restriction>
        </xs:simpleType>    
        <xs:complexType name="address">
            <xs:sequence>
                <xs:element name="country" type="xs:string"/>
                <xs:element name="province" type="xs:string"/>
                <xs:element name="city" type="xs:string"/>
                <xs:element name="street" type="xs:string"/>
            </xs:sequence>        
        </xs:complexType>        
    </xs:schema>

包含include.xsd:

  1. <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.cnblogs.com" targetNamespace="http://www.cnblogs.com">
        <xs:include schemaLocation="include.xsd"/>
        <xs:element name="employees">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="employee" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="name" type="xs:string"/>
                                <xs:element name="age" type="xs:integer"/>
                                <xs:element name="sex" type="xs:sexType"/>
                                <xs:element name="address" type="address" maxOccurs="unbounded"/>
                            </xs:sequence>
                            <xs:attribute name="id" type="xs:integer"/>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>

相关文章:

  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2021-11-24
  • 2022-01-02
  • 2021-07-24
猜你喜欢
  • 2021-10-21
  • 2021-06-27
  • 2022-01-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案