XML Schema使用技巧——include

       在一个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>
posted @ 2010-01-10 03:21  Asharp  阅读(2531)  评论(0编辑  收藏  举报