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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40788630/article/details/83303651

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元素的模式重用就实现了

猜你喜欢

转载自blog.csdn.net/qq_40788630/article/details/83303651