XML - Schema之引入其他Schema的方式

一、Include

        使用Include引入时,要么引入和被引入的Schema都没有目标命名空间,要么他们具有的目标命名空间是相同的。


    1、都没有命名空间

        (1)定义Address的Schema(include_nons_address.xsd)如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 如果当前的schema没有命名空间时,需要把默认的XMLSchema的命名空间设置别名 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

	<xs:element name="address" type="addressType"/> <!-- 定义根元素customer  -->
	
	<xs:complexType name="addressType"> <!-- 定义address的数据类型  -->
		<xs:sequence>
			<xs:element name="code" type="codeType" /> <!-- 定义address的code子元素,数据类型引用codeType的定义 -->
			<xs:element name="info" type="infoType"/> <!-- 定义address的info子元素,数据类型引用infoType的定义 -->
		</xs:sequence>
		<xs:attribute name="id" type="idType"/><!-- 定义address的id属性,数据类型引用idType的定义  -->
	</xs:complexType>

	<xs:simpleType name="idType">  <!-- 定义ID类型 -->
		<xs:restriction base="xs:int"> <!-- 定义限定条件,基于int的类型  -->
			<xs:minInclusive value="1" /> <!-- 最小值 -->
			<xs:maxInclusive value="999999999" /> <!-- 最大值  -->
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="codeType"> <!-- 定义Code的类型 -->
		<xs:restriction base="xs:string"> <!-- 定义限定条件,基于string的类型  -->
			<xs:pattern value="[0-9]*"/>
			<xs:length value="6"/> <!-- 定义最小长度 -->
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="infoType" > <!-- 定义Info的类型 -->
		<xs:restriction base="xs:string"> <!-- 定义限定条件,基于string的类型  -->
			<xs:minLength value="0"/> <!-- 定义最小长度 -->
			<xs:maxLength value="255"/> <!-- 定义最大长度 -->
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

        (2)定义Customer的Schema(include_nons_customer.xsd),引入Address,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 如果当前的schema没有命名空间时,需要把默认的XMLSchema的命名空间设置别名 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
	
	<!-- 通过include引入没有命名空间的address的schema -->	
	<xs:include schemaLocation="include_nons_address.xsd"/>
	
	 <!-- 定义当前命名空间的根元素customer  -->
	<xs:element name="customer" type="customerType"/>

	<xs:complexType name="customerType"> <!-- 定义customer的数据类型  -->
		<xs:sequence>
			<xs:element name="name" type="nameType"/> <!-- 定义customer的name子元素,数据类型引用nameType的定义 -->
			<xs:element name="age" type="ageType" /> <!-- 定义customer的age元素,的数据类型引用ageType的定义 -->
			<xs:element name="sex" type="sexType" /> <!-- 定义customer的sex子元素,的数据类型引用sexType的定义 -->
			<xs:element name="addresses" type="addressesType"/>
		</xs:sequence>
		<xs:attribute name="id" type="idType"/> <!-- 定义customer的属性,数据类型重用address中的idType的定义 -->
	</xs:complexType>
	
	<xs:simpleType name="nameType"> <!-- 定义Customer的Name -->
		<xs:restriction base="xs:string">
			<xs:pattern value="[a-zA-Z][_a-zA-Z0-9]*" /> <!-- 定义正则来规范名称 -->
			<xs:minLength value="3" /> <!-- 定义最小长度 -->
			<xs:maxLength value="18" /> <!-- 定义最大长度 -->
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="ageType"> <!-- 定义age的类型  -->
		<xs:restriction base="xs:int"> <!-- 定义限定条件,基于int的类型 -->
			<xs:minInclusive value="0" /><!-- 最小包含0 -->
			<xs:maxExclusive value="150" /><!-- 最大不包含150 -->
		</xs:restriction>
	</xs:simpleType>
	
	<xs:simpleType name="sexType"> <!-- 定义sex的类型  -->
		<xs:restriction base="xs:string"> <!-- 定义限定条件,基于string的类型  -->
			<xs:enumeration value="MAN" /> <!-- 定义sex为枚举,只可以选择其一 -->
			<xs:enumeration value="WOMAN" /> 		
			<xs:enumeration value="OTHER" /> 		
		</xs:restriction>
	</xs:simpleType>
	
	<xs:complexType name="addressesType"> <!-- 定义addresses的类型 -->
		<xs:sequence minOccurs="1" maxOccurs="4">
			<xs:element name="address" type="addressType"/> <!-- 引用address中的addressType -->
		</xs:sequence>
	</xs:complexType>
</xs:schema>

        (3)使用Customer的Schema(include_nons_customer.xsd)生成xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 通过xsi:noNamespaceSchemaLocation指定Schema -->
<customer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:noNamespaceSchemaLocation="include_nons_customer.xsd" id="1">
	<name>Tom</name>
	<age>25</age>
	<sex>MAN</sex>
	<addresses>
		<address id="1">
			<code>100000</code>
			<info>BeiJing</info>
		</address>
		<address id="2">
			<code>510000</code>
			<info>GuangZhou</info>
		</address>
	</addresses>
</customer>
    2、具有相同的命名空间

        (1)定义Address的Schema(include_eqns_address.xsd)如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	targetNamespace="http://www.xilen.com/crm":设置当前Schema的命名空间
	xmlns:crm="http://www.xilen.com/crm":将当前命名空间别名为crm 
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/crm" xmlns:crm="http://www.xilen.com/crm">

	<element name="address" type="crm:addressType"/> <!-- 定义根元素customer  -->
	
	<complexType name="addressType"> <!-- 定义address的数据类型  -->
		<sequence>
			<element name="code" type="crm:codeType" /> <!-- 定义address的code子元素,数据类型引用codeType的定义 -->
			<element name="info" type="crm:infoType"/> <!-- 定义address的info子元素,数据类型引用infoType的定义 -->
		</sequence>
		<attribute name="id" type="crm:idType"/><!-- 定义address的id属性,数据类型引用idType的定义  -->
	</complexType>
	
	<simpleType name="idType">  <!-- 定义ID类型 -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型  -->
			<minInclusive value="1" /> <!-- 最小值 -->
			<maxInclusive value="999999999" /> <!-- 最大值  -->
		</restriction>
	</simpleType>
	
	<simpleType name="codeType"> <!-- 定义Code -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<pattern value="[0-9]*"/>
			<length value="6"/> <!-- 定义最小长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="infoType" > <!-- 定义Info的类型 -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<minLength value="0"/> <!-- 定义最小长度 -->
			<maxLength value="255"/> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
</schema>
        (2)定义Customer的Schema( include_eqns_customer.xsd ),引入Address,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	targetNamespace="http://www.xilen.com/crm":设置当前Schema的命名空间,和address相同 
	xmlns:crm="http://www.xilen.com/crm":将当前命名空间别名为crm
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/crm" xmlns:crm="http://www.xilen.com/crm">
	
	<!-- 通过include引入相同命名空间的address的schema -->	
	<include schemaLocation="include_eqns_address.xsd"/>
	
	 <!-- 定义当前命名空间的根元素customer  -->
	<element name="customer" type="crm:customerType"/>

	<complexType name="customerType"> <!-- 定义customer的数据类型  -->
		<sequence>
			<element name="name" type="crm:nameType"/> <!-- 定义customer的name子元素,数据类型引用nameType的定义 -->
			<element name="age" type="crm:ageType" /> <!-- 定义customer的age元素,的数据类型引用ageType的定义 -->
			<element name="sex" type="crm:sexType" /> <!-- 定义customer的sex子元素,的数据类型引用sexType的定义 -->	
			<element name="addresses" type="crm:addressesType"/>
		</sequence>
		<attribute name="id" type="crm:idType"/> <!-- 定义customer的属性,数据类型重用address中的idType的定义 -->
	</complexType>
	
	<simpleType name="nameType"> <!-- 定义Customer的Name -->
		<restriction base="string">
			<pattern value="[a-zA-Z][_a-zA-Z0-9]*" /> <!-- 定义正则来规范名称 -->
			<minLength value="3" /> <!-- 定义最小长度 -->
			<maxLength value="18" /> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="ageType"> <!-- 定义age的类型  -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型 -->
			<minInclusive value="0" /><!-- 最小包含0 -->
			<maxExclusive value="150" /><!-- 最大不包含150 -->
		</restriction>
	</simpleType>
	
	<simpleType name="sexType"> <!-- 定义sex的类型  -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<enumeration value="MAN" /> <!-- 定义sex为枚举,只可以选择其一 -->
			<enumeration value="WOMAN" /> 		
			<enumeration value="OTHER" /> 		
		</restriction>
	</simpleType>
	
	<complexType name="addressesType"> <!-- 定义addresses的类型 -->
		<sequence minOccurs="1" maxOccurs="4">
			<element name="address" type="crm:addressType"/> <!-- 引用address中的addressType -->
		</sequence>
	</complexType>
</schema>
        (3)使用Customer的Schema include_eqns_customer.xsd 生成xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<!-- 引入customer的Schema,并将其和address相同的命名空间作为默认空间 -->
<customer xmlns="http://www.xilen.com/crm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.xilen.com/crm include_eqns_customer.xsd"
	id="1">
	<name>Tom</name>
	<age>25</age>
	<sex>MAN</sex>
	<addresses>
		<address id="1">
			<code>100000</code>
			<info>BeiJing</info>
		</address>
		<address id="2">
			<code>510000</code>
			<info>GuangZhou</info>
		</address>
	</addresses>
</customer>
二、Import

        Import尽用于引入具有不同命名空间的Schema。

    1、定义Address的Schema(import_address.xsd)如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- xmlns:address="http://www.xilen.com/address":将当前命名空间别名为address -->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/address" xmlns:address="http://www.xilen.com/address">

	<element name="address" type="address:addressType"/> <!-- 定义根元素customer  -->
	
	<complexType name="addressType"> <!-- 定义address的数据类型  -->
		<sequence>
			<element name="code" type="address:codeType" /> <!-- 定义address的code子元素,数据类型引用codeType的定义 -->
			<element name="info" type="address:infoType"/> <!-- 定义address的info子元素,数据类型引用infoType的定义 -->
		</sequence>
		<attribute name="id" type="address:idType"/><!-- 定义address的id属性,数据类型引用idType的定义  -->
	</complexType>

	<simpleType name="idType">  <!-- 定义ID类型 -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型  -->
			<minInclusive value="1" /> <!-- 最小值 -->
			<maxInclusive value="999999999" /> <!-- 最大值  -->
		</restriction>
	</simpleType>
	
	<simpleType name="codeType"> <!-- 定义Code -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<pattern value="[0-9]*"/>
			<length value="6"/> <!-- 定义最小长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="infoType" > <!-- 定义Info的类型 -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<minLength value="0"/> <!-- 定义最小长度 -->
			<maxLength value="255"/> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
</schema>
    2、 定义Customer的Schema( import_customer.xsd ),引入Address,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	xmlns:customer="http://www.xilen.com/customer":将当前命名空间别名为customer
	xmlns:address="http://www.xilen.com/address" : 将引入的address的schema的命名空间别名为address
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/customer" xmlns:customer="http://www.xilen.com/customer"
	xmlns:address="http://www.xilen.com/address">
	
	<!-- 引入address的schema -->
	<import namespace="http://www.xilen.com/address" schemaLocation="import_address.xsd"/>
	
	 <!-- 定义当前命名空间的根元素customer  -->
	<element name="customer" type="customer:customerType"/>

	<complexType name="customerType"> <!-- 定义customer的数据类型  -->
		<sequence>
			<element name="name" type="customer:nameType"/> <!-- 定义customer的name子元素,数据类型引用nameType的定义 -->
			<element name="age" type="customer:ageType" /> <!-- 定义customer的age元素,的数据类型引用ageType的定义 -->
			<element name="sex" type="customer:sexType" /> <!-- 定义customer的sex子元素,的数据类型引用sexType的定义 -->			
			<element name="addresses" type="customer:addressesType"/>
		</sequence>
		<attribute name="id" type="address:idType"/> <!-- 定义customer的属性,数据类型重用address中的idType的定义 -->
	</complexType>
	
	<simpleType name="nameType"> <!-- 定义Name -->
		<restriction base="string">
			<pattern value="[a-zA-Z][_a-zA-Z0-9]*" /> <!-- 定义正则来规范名称 -->
			<minLength value="3" /> <!-- 定义最小长度 -->
			<maxLength value="18" /> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="ageType"> <!-- 定义age的类型  -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型 -->
			<minInclusive value="0" /><!-- 最小包含0 -->
			<maxExclusive value="150" /><!-- 最大不包含150 -->
		</restriction>
	</simpleType>
	
	<simpleType name="sexType"> <!-- 定义sex的类型  -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<enumeration value="MAN" /> <!-- 定义sex为枚举,只可以选择其一 -->
			<enumeration value="WOMAN" /> 		
			<enumeration value="OTHER" /> 		
		</restriction>
	</simpleType>
	
	<complexType name="addressesType"> <!-- 定义addresses的类型 -->
		<sequence minOccurs="1" maxOccurs="4">
			<element name="address" type="address:addressType"/> <!-- 引用address中的addressType -->
		</sequence>
	</complexType>
</schema>
    3、使用Customer的Schema(import_customer.xsd)生成xml如下:
<?xml version="1.0" encoding="UTF-8"?>

<!-- 
	xmlns="http://www.xilen.com/customer":将customer的命名空间作为默认的命名空间 
	xmlns:address="http://www.xilen.com/address":将address这个在customer中引用到的命名空间别名为address 
	xsi:schemaLocation="http://www.xilen.com/customer customer.xsd" 通过Schema构造器指定customer的路径 
-->
<customer xmlns="http://www.xilen.com/customer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:address="http://www.xilen.com/address" xsi:schemaLocation="http://www.xilen.com/customer customer.xsd"
	id="1">
	<name>Tom</name>
	<age>25</age>
	<sex>MAN</sex>
	<addresses>
		<address id="1">
			<address:code>100000</address:code>
			<address:info>BeiJing</address:info>
		</address>
		<address id="2">
			<address:code>510000</address:code>
			<address:info>GuangZhou</address:info>
		</address>
	</addresses>
</customer>
三、Redefine

        Redefine和Include类似,要求相同的命名空间。不同在于它可以对引入的Schema中的组件在这个元素里面进行重新定义(扩展或限制)。如果没有重定义元素,功能和Include等同。注意:可以进行重定义的元素尽简单类型、复杂类型、元素组和属性组。

    1、定义Address的Schema(redefine_address.xsd)如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	targetNamespace="http://www.xilen.com/crm":设置当前Schema的命名空间
	xmlns:crm="http://www.xilen.com/crm":将当前命名空间别名为crm 
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/crm" xmlns:crm="http://www.xilen.com/crm">

	<element name="address" type="crm:addressType"/> <!-- 定义根元素customer  -->
	
	<complexType name="addressType"> <!-- 定义address的数据类型  -->
		<sequence>
			<element name="code" type="crm:codeType" /> <!-- 定义address的code子元素,数据类型引用codeType的定义 -->
			<element name="info" type="crm:infoType"/> <!-- 定义address的info子元素,数据类型引用infoType的定义 -->
		</sequence>
		<attribute name="id" type="crm:idType"/><!-- 定义address的id属性,数据类型引用idType的定义  -->
	</complexType>
	
	<simpleType name="idType">  <!-- 定义ID类型 -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型  -->
			<minInclusive value="1" /> <!-- 最小值 -->
			<maxInclusive value="999999999" /> <!-- 最大值  -->
		</restriction>
	</simpleType>
	
	<simpleType name="codeType"> <!-- 定义Code -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<pattern value="[0-9]*"/>
			<length value="6"/> <!-- 定义最小长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="infoType" > <!-- 定义Info的类型 -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<minLength value="0"/> <!-- 定义最小长度 -->
			<maxLength value="255"/> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
</schema>

    2、定义Customer的Schema(redefine_customer.xsd),引入Address,如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	targetNamespace="http://www.xilen.com/crm":设置当前Schema的命名空间,和address相同 
	xmlns:crm="http://www.xilen.com/crm":将当前命名空间别名为crm
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"  
	targetNamespace="http://www.xilen.com/crm" xmlns:crm="http://www.xilen.com/crm">
	
	<!-- 通过redefine引入相同命名空间的address的schema -->	
	<redefine schemaLocation="redefine_address.xsd">
		<!-- 对address中的addressType进行扩展,添加node子元素 -->
		<complexType name="addressType">
			<complexContent>
				<extension base="crm:addressType">
					<sequence>
						<element name="note" type="string"/>
					</sequence>
				</extension>
			</complexContent>
		</complexType>
	</redefine>
	
	 <!-- 定义当前命名空间的根元素customer  -->
	<element name="customer" type="crm:customerType"/>

	<complexType name="customerType"> <!-- 定义customer的数据类型  -->
		<sequence>
			<element name="name" type="crm:nameType"/> <!-- 定义customer的name子元素,数据类型引用nameType的定义 -->
			<element name="age" type="crm:ageType" /> <!-- 定义customer的age元素,的数据类型引用ageType的定义 -->
			<element name="sex" type="crm:sexType" /> <!-- 定义customer的sex子元素,的数据类型引用sexType的定义 -->	
			<element name="addresses" type="crm:addressesType"/>
		</sequence>
		<attribute name="id" type="crm:idType"/> <!-- 定义customer的属性,数据类型重用address中的idType的定义 -->
	</complexType>
	
	<simpleType name="nameType"> <!-- 定义Customer的Name -->
		<restriction base="string">
			<pattern value="[a-zA-Z][_a-zA-Z0-9]*" /> <!-- 定义正则来规范名称 -->
			<minLength value="3" /> <!-- 定义最小长度 -->
			<maxLength value="18" /> <!-- 定义最大长度 -->
		</restriction>
	</simpleType>
	
	<simpleType name="ageType"> <!-- 定义age的类型  -->
		<restriction base="int"> <!-- 定义限定条件,基于int的类型 -->
			<minInclusive value="0" /><!-- 最小包含0 -->
			<maxExclusive value="150" /><!-- 最大不包含150 -->
		</restriction>
	</simpleType>
	
	<simpleType name="sexType"> <!-- 定义sex的类型  -->
		<restriction base="string"> <!-- 定义限定条件,基于string的类型  -->
			<enumeration value="MAN" /> <!-- 定义sex为枚举,只可以选择其一 -->
			<enumeration value="WOMAN" /> 		
			<enumeration value="OTHER" /> 		
		</restriction>
	</simpleType>
	
	<complexType name="addressesType"> <!-- 定义addresses的类型 -->
		<sequence minOccurs="1" maxOccurs="4">
			<element name="address" type="crm:addressType"/> <!-- 引用address中的addressType -->
		</sequence>
	</complexType>
</schema>

    3、使用Customer的Schema(redefine_customer.xsd)生成xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<!-- 引入customer的Schema,并将其和address相同的命名空间作为默认空间 -->
<customer xmlns="http://www.xilen.com/crm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.xilen.com/crm redefine_customer.xsd"
	id="1">
	<name>Tom</name>
	<age>25</age>
	<sex>MAN</sex>
	<addresses>
		<address id="1">
			<code>100000</code>
			<info>BeiJing</info>
			<note>Home Address</note>
		</address>
		<address id="2">
			<code>510000</code>
			<info>GuangZhou</info>
			<note>Office Address</note>
		</address>
	</addresses>
</customer>
 
 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值