XML学习之Xml Schema:十一、引入其他的模式文档

在编写文档时,需要导入其他的模式文档,XML Schema提供了3种机制,方便我们导入。分别是包含(include)、重定义(redefine)、导入(import)。

 

        包含Include

        要包含一个模式文档,可以使用xs:include元素,他有个一个必须的属性schemaLocation,用于指出要包含的模式文档的位置。xs:include元素要作为xs':schema元素的子元素使用,且必须出现在模式文档的开始处。(之前只能出现xs:redefine、xs:import)。

        使用包含一个很重要的条件就是:被包含组件的命名空间必须和包含方的目标名称空间一样。被包含的模式文档也可以没有命名空间,这样的话,将使用包含方的目标命名空间。如果包含方也没有目标名称空间,那么就和没有使用名称空间一样的。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.jwt.com/Employee" targetNamespace="http://www.jwt.com/Employee">
	<xs:complexType name="addressType">
		<xs:sequence>
			<xs:element name="city" type="xs:token"/>
			<xs:element name="street" type="xs:string"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

XS_Include_AddressType.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Employee" xmlns="http://www.jwt.com/Employee">
	<xs:include schemaLocation="XS_Include_AddressType.xsd"/>
	<xs:element name="employee" type="employeeType"/>
	<xs:complexType name="employeeType">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="homeAddr" type="addressType"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

XS_Include_Employee.xml

        重定义redefine

        重定义与包含类似,都可以用于引入其他的模式,且都要求被引入的模式文档与包含文档具有相同的名称空间(或者没有目标名称空间),不同的是,重定义可以对引入的模式文档中的组件进行重新定义。要注意的是:1、只有某些特定的类型的模式组件可以被重定义,他们是:简单类型、复杂类型、元素组、属性组。2、是有需要修改的组件才应该出现在xs:redefine元素中,其他的组件将被之间包含在新的模式中。3、xs:redefine元素并不要求有任何的子元素,这样的情况下,他仅仅起到xs:include元素的作用。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Employee" xmlns="http://www.jwt.com/Employee">
	<xs:redefine schemaLocation="XS_Include_AddressType.xsd">
		<xs:complexType name="addressType">
			<xs:complexContent>
				<xs:extension base="addressType">
					<xs:sequence>
						<xs:element name="zipCode" type="xs:string"/>
					</xs:sequence>
				</xs:extension>
			</xs:complexContent>
		</xs:complexType>
	</xs:redefine>
	
	<xs:element name="employee" type="employeeType"/>
	<xs:complexType name="employeeType">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="homeAddr" type="addressType"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>


 导入Import

导入用于在一个模式文档中引入其他名称空间中的组件。导入使用xs:import元素来表示,有两个属性,namespace和schemaLocation。namespace属性指定要导入的名称空间的URI,shemaLocation属性是必需的,用于指出模式文档的位置。同样的,xs:import元素只能作为xs:schema的子元素使用,且必须出现在模式文档的开始处。

<?xml version="1.0" encoding="UTF-8"?>  
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.jwt.com/Address" targetNamespace="http://www.jwt.com/Address">  
    <xs:complexType name="addressType">  
        <xs:sequence>  
            <xs:element name="city" type="xs:token"/>  
            <xs:element name="street" type="xs:string"/>  
        </xs:sequence>  
    </xs:complexType>  
</xs:schema>  
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jwt.com/Emp" xmlns:emp="http://www.jwt.com/Emp" 
xmlns:addr="http://www.jwt.com/Address">
	<xs:import namespace="http://www.jwt.com/Address" schemaLocation="address.xsd" />
	<xs:element name="employee" type="empType"/>
	<xs:complexType name="empType">
		<xs:sequence>
			<xs:element name="name" type="xs:string"/>
			<xs:element name="homeAddr" type="addr:addressType"/>
		</xs:sequence>
	</xs:complexType>
</xs:schema>



 



 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值