XML 使用include元素实现简单模式重用

本文详细介绍了一种基于XML Schema的模式重用方法,通过创建和整合多个模式文件实现复杂数据类型的定义。从简单类型定义到复杂类型的组合,再到模式文件的引用,最后在XML文件中应用这些模式,全过程清晰明了。

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

1、新建一个模式firstschema.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="www.a/b">
	<xs:simpleType name="bid">
		<xs:restriction base="xs:string">
		    <xs:pattern value="[A]\d{4}"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

2、新建另一个模式secondschema.xsd文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="www.a/b">
	<xs:simpleType name="aid">
		<xs:restriction base="xs:string">
		    <xs:pattern value="[C]\d{4}"/>
		</xs:restriction>
	</xs:simpleType>
</xs:schema>

3、新建一个模式thirdschema.xsd,在此模式中使用上面两种模式

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prd="www.a/b" targetNamespace="www.a/b">
<xs:include schemaLocation="firstschema.xsd"/>
<xs:include schemaLocation="secondschema.xsd"/>
<xs:element name="bookinfo" type="prd:infotype"/>
<xs:complexType name="infotype">
    <xs:sequence>
        <xs:element name="book" type="prd:booktype"/>    
    </xs:sequence>
</xs:complexType>
<xs:complexType name="booktype">
    <xs:sequence>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="author" type="prd:atype"/>    
    </xs:sequence>
    <xs:attribute name="bookid" type="prd:bid"/>
</xs:complexType>
<xs:complexType name="atype">
    <xs:sequence>
        <xs:element name="xing" type="xs:string"/>
        <xs:element name="ming" type="xs:string"/>    
    </xs:sequence>
    <xs:attribute name="authorid" type="prd:aid"/>
</xs:complexType>
</xs:schema>

4、新建一个xml文件book.xml,在文件中导入第三个模式文件

<?xml version="1.0" encoding="UTF-8"?>
<prd:bookinfo xmlns:prd="www.a/b" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.a/b file:///C:/Documents%20and%20Settings/Administrator.EBD9FC1E47F5453/%e6%a1%8c%e9%9d%a2/thirdschema.xsd">
	<book bookid="A0001">
		<title>JAVA</title>
		<author authorid="C1234">
			<xing>李</xing>
			<ming>佳</ming>
		</author>
	</book>
</prd:bookinfo>

5、保存,查看机器指示

一个简单的基于include元素的模式重用就实现了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值