xsd的import和include,模式重用

本文详细介绍了如何在XML Schema定义中使用import和include进行模式重用。通过具体实例,展示了不同命名空间下如何引用已定义的类型和元素,以及何时使用import与include的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xsd的import和include,模式重用

编写A.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="www.a.com/stu" xmlns:Aid="www.a.com/stu" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:element name="id" type="Aid:idt"/>
	<xs:simpleType name="idt">
		<xs:restriction base="xs:string">
			<xs:length value="7"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

设置targetNamespace为www.a.com/stu,因为后面需要引用,所以此处需要设置xmlns:Aid=“www.a.com/stu”

编写B.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prd="www.a.com/stu" targetNamespace="www.a.com/stu" >
	<xs:include schemaLocation="A.xsd"/>
	
	<xs:element name="stu" type="prd:stutype"/>
	
	<xs:complexType name="stutype">
		<xs:sequence>
			<xs:element name="id" type="prd:idt"></xs:element>
			<xs:element name="name" type="xs:string"></xs:element>
			<xs:element name="age" type="xs:integer"></xs:element>
		</xs:sequence>
	</xs:complexType>
</xs:schema>

这里的代码段使用了include,由于A和B在同一个targetNamespace下面,所以可以使用include,<xs:include schemaLocation=“A.xsd”/>
<xs:element name=“id” type=“prd:idt”></xs:element>为对A的引用,和A出现的情况一样,由于想在此xsd中写element,所以需要给此文件起别名

编写C.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="www.a.com/student" xmlns:prd="www.a.com/stu" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
	<xs:import namespace="www.a.com/stu" schemaLocation="B.xsd"/>
	
	<xs:element name="student">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="stu" type="prd:stutype" maxOccurs="1000"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

这里的代码段使用了import,由于C和B在不在同一个targetNamespace下面,所以可以使用import,
**<xs:import namespace=“www.a.com/stu” schemaLocation=“b.xsd”/>**对B的引用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值