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。