微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml语义约束:DTD和Schema

XML不像HTML那样有一套预置的标签,但是XML有严格的语义约束,主要有两种模式:DTD和Schema

DTD

DTD有三种引用方式:

1.内部引用,DTD只能供一个XML文档使用。

2.外部(SYstem)引用,DTD是一个单独的文件,可以供多个XML文档使用。

3.公共(PUBLIC)引用,DTD是一个URL,可以供多个XML使用。

注:一个XML一般只能引入一个DTD。

Schema

按Schema是否指定命名空间,Schema的引用方式分为两种:

1.无命名空间引用。

2.有命名空间引用。

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2013 (http://www.altova.com) by  () -->
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:noNamespaceSchemaLocation="file:///E:/XMLSpy/Users/zzj/book.xsd"
	xsi:schemaLocation="www.so.com file:///E:/XMLSpy/Users/zzj/movie.xsd 
						www.baidu.com file:///E:/XMLSpy/Users/zzj/game.xsd" 
	xmlns:b="www.so.com" xmlns:g="www.baidu.com">
	<book>
		<name>疯狂XML讲义</name>
		<author>李刚</author>
	</book>
	<book>
		<name>疯狂Java讲义</name>
		<author>李刚</author>
	</book>
	<b:movie>
		<b:name>卧虎藏龙</b:name>
		<b:author>李安</b:author>
	</b:movie>
	<b:movie>
		<b:name>英雄</b:name>
		<b:author>张艺谋</b:author>
	</b:movie>
	<g:game>
		<g:name>qq部落</g:name>
		<g:author>腾讯</g:author>
	</g:game>
	<g:game>
		<g:name>帝国文明</g:name>
		<g:author>腾讯</g:author>
	</g:game>
</books>
book.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!--W3C Schema generated by XMLSpy v2013 (http://www.altova.com)-->
<!-- 根元素未指定targetNamespace属性,XML使用无命名空间方式引入 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="name">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="books">
		<!-- books下面除了可以有book元素,还可以有其他元素,这样可以让books元素引入其他schema定义的元素 -->
		<xs:complexType>
			<xs:sequence>
 				<xs:choice minOccurs="0" maxOccurs="unbounded">
				  <xs:element ref="book" /> 
				  <xs:any namespace="##other" /> 
				</xs:choice>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="book">
		<!-- book下面只能有name和author元素 -->
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="name"/>
				<xs:element ref="author"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="author">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
</xs:schema>
movie.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- 根元素指定了targetNamespace属性,XML使用有命名空间方式引入 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns="www.so.com" 
	targetNamespace="www.so.com">
	<xs:element name="name">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="author">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="movie">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="name"/>
				<xs:element ref="author"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
game.xsd
<?xml version="1.0" encoding="UTF-8"?>
<!-- 根元素指定了targetNamespace属性,XML使用有命名空间方式引入 -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
	xmlns="www.baidu.com" 
	targetNamespace="www.baidu.com">
	<xs:element name="name">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="author">
		<xs:simpleType>
			<xs:restriction base="xs:string"/>
		</xs:simpleType>
	</xs:element>
	<xs:element name="game">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="name"/>
				<xs:element ref="author"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>
一个XSD文件的目标命名空间(targetNamespace)通常都会指定为一个URL(但并不是必须的),而这个URL通常又会指向这个XSD文件,因为URL是唯一的,这样就保证了XML文档元素和属性的唯一。

注:一个XML文档可以引入多个Schema,但只能引入一个无命名空间的Schema。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类