<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <!-- 内部DTD,指定了文档的根元素:书籍列表--> <!DOCTYPE 书籍列表[ <!ELEMENT 书籍列表 (计算机书)*> <!ELEMENT 计算机书 (书名,作者,价格,介绍)> <!ELEMENT 书名 (#PCDATA)> <!ELEMENT 作者 (#PCDATA)> <!ELEMENT 价格 (#PCDATA)> <!ELEMENT 介绍 (#PCDATA)> ]> <书籍列表> <计算机书> <书名>hadoop权威指南</书名> <作者>zpc</作者> <价格>22</价格> <介绍>该书介绍了Hadoop的技术</介绍> </计算机书> </书籍列表>8、外部DTD
<!DOCTYPE 根元素名 SYSTEM "外部DTD的URI" > 如: <!DOCTYPE 书籍列表 SYSTEM "http://www.haodianying.org/dtd/book.dtd" > <!DOCTYPE 书籍列表 SYSTEM "book.dtd" > 外部DTD例子: book.dtd文件 <?xml version="1.0" encoding="UTF-8"?> <!ELEMENT 书籍列表 (计算机书)*> <!ELEMENT 计算机书 (书名,作者,价格,介绍)> <!ELEMENT 书名 (#PCDATA)> <!ELEMENT 作者 (#PCDATA)> <!ELEMENT 价格 (#PCDATA)> <!ELEMENT 介绍 (#PCDATA)> outer.xml文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE 书籍列表 SYSTEM "C:\Documents and Settings\zhou\桌面\book.dtd"> <书籍列表> <计算机书> <书名>hadoop权威指南</书名> <作者>zpc</作者> <价格>22</价格> <介绍>该书介绍了Hadoop的技术</介绍> </计算机书> </书籍列表>
例子1:
order1.xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="order">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="orderItem" maxOccurs="10"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="orderItem">
<xsd:complexType>
<xsd:sequence>
<!--自定义的两个类型-->
<xsd:element name="id" type="idType"/>
<xsd:element name="quantity" type="quantityType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="idType">
<xsd:restriction base="xsd:string">
<!--枚举类型-->
<xsd:enumeration value="7-5058-3496-7"/>
<xsd:enumeration value="7-5005-6450-3"/>
<xsd:enumeration value="7-3020-6069-7"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="quantityType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1"/>
<xsd:maxInclusive value="10"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
order1.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<order xmlns:xi="http://www.w3.org/2001/XMLSchema-instance" xi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/order1.xsd">
<orderItem>
<id>7-5005-6450-3</id>
<quantity>5</quantity>
</orderItem>
</order>
注意:在XML Schema的根元素下定义的任何元素都可以作为XML文档的根元素使用,因此上面的order1.xml也可以写成下面的格式
<?xml version="1.0" encoding="UTF-8"?>
<orderItem xmlns:xi="http://www.w3.org/2001/XMLSchema-instance" xi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/order1.xsd">
<id>7-3020-6069-7</id>
<quantity>2</quantity>
</orderItem>
例子2:
age.xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" >
<!-- 定义新类型-->
<zpc:simpleType name="age_Type">
<zpc:restriction base="zpc:int">
<!--最大值100、最小值0-->
<zpc:maxInclusive value="100"/>
<zpc:minInclusive value="0"/>
</zpc:restriction>
</zpc:simpleType>
<!--定义age元素,它的类型是age_Type-->
<zpc:element name="age" type="age_Type"/>
</zpc:schema>
age.xml
<?xml version="1.0" encoding="UTF-8"?>
<age xmlns:niao="http://www.w3.org/2001/XMLSchema-instance" niao:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/test2.xsd">
<!--上述schema限制了age的取值范围-->
33
</age>
例子3:
price.xsd
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<zpc:simpleType name="price_Type">
<zpc:restriction base="zpc:decimal">
<zpc:maxInclusive value="100"/>
<zpc:minInclusive value="0"/>
<!-- 最多5位数,小数点最后最多2位-->
<zpc:fractionDigits value="2"/>
<zpc:totalDigits value="5"/>
</zpc:restriction>
</zpc:simpleType>
<zpc:element name="price" type="price_Type"/>
</zpc:schema>
price.xml
<?xml version="1.0" encoding="UTF-8"?>
<price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/test3.xsd">
23.34
</price>
例子4:
这次我们给出一个更加复杂一些的文档:
address.xml
---------------
<customer>
<name>Teiki</name>
<address>
<!-- address追加一个地址子元素 -->
<prefecture>Zhejiang</prefecture>
<city>Hangzhou</city>
<street>Xilu Road, No.121, 7F</street>
</address>
</customer>
为此,我们需要一个更加复杂一点的Schema文档:
address.xsd
-----------------
1: <?xml version="1.0"?>
2: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4: <xsd:element name="customer">
5: <xsd:complexType>
6: <xsd:sequence>
7: <xsd:element name="name" type="xsd:string"/>
8: <!-- 追加子元素address-->
9: <xsd:element name="address">
10: <xsd:complexType>
11: <xsd:sequence>
12: <xsd:element name="prefecture" type="xsd:string"/>
13: <xsd:element name="city" type="xsd:string" />
14: <xsd:element name="street" type="xsd:string" />
15: </xsd:sequence>
16: </xsd:complexType>
17: </xsd:element>
18: <!-- end -->
19: </xsd:sequence>
20: </xsd:complexType>
21: </xsd:element>
22:
23:</xsd:schema>
不过,我们还可以采用ref元素来重新编写这个Schema文档:
address2.xsd
----------------------
1: <?xml version="1.0"?>
2: <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
3:
4: <xsd:element name="customer">
5: <xsd:complexType>
6: <xsd:sequence>
7: <xsd:element name="name" type="xsd:string"/>
8: <xsd:element ref="address"/>
9: </xsd:sequence>
10: </xsd:complexType>
11: </xsd:element>
12:
13: <xsd:element name="address">
14: <xsd:complexType>
15: <xsd:sequence>
16: <xsd:element name="prefecture" type="xsd:string"/>
17: <xsd:element name="city" type="xsd:string" />
18: <xsd:element name="street" type="xsd:string" />
19: </xsd:sequence>
20: </xsd:complexType>
21: </xsd:element>
22:
23: </xsd:schema>
使用ref元素可以直接将其指向另一个模块,使文档更加具有可读性。
例子5:正则表达式约束
pattern.xsd
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" >
<!--定义一种新的数据类型-->
<zpc:simpleType name="price_Type">
<zpc:restriction base="zpc:decimal">
<!--必须匹配1XX.X的格式-->
<zpc:pattern value="1\d{2}\.\d"/>
</zpc:restriction>
</zpc:simpleType>
<!--定义元素-->
<zpc:element name="price" type="price_Type"/>
</zpc:schema>
price.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<price xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/pattern.xsd">
122.3
</price>
例子6:使用<list.../>派生列表类型
list.xsd文件
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<zpc:simpleType name="age_list_Type">
<zpc:list>
<zpc:simpleType>
<zpc:restriction base="zpc:int">
<zpc:maxInclusive value="100"/>
<zpc:minExclusive value="0"/>
</zpc:restriction>
</zpc:simpleType>
</zpc:list>
</zpc:simpleType>
<zpc:element name="age_list" type="age_list_Type"/>
</zpc:schema>
list.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<age_list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/list.xsd">
12
23
</age_list>14、合并多个schemaincluded.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" >
<zpc:simpleType name="age_Type">
<zpc:restriction base="zpc:int">
<zpc:maxExclusive value="100"/>
<zpc:minExclusive value="0"/>
</zpc:restriction>
</zpc:simpleType>
</zpc:schema>
include.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<zpc:schema xmlns:zpc="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.zpc.org" xmlns="http://www.zpc.org">
<zpc:include schemaLocation="included.xsd"/>
<zpc:element name="age" type="age_Type"/>
</zpc:schema>
在XML文件中引用include.xsd
<?xml version="1.0" encoding="UTF-8"?>
<age xmlns="http://www.zpc.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zpc.org file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/include.xsd">
23
</age>
15、可以使用redefine元素重定义被包含schema里的组件
16、使用include包含的schema要么没有目标命名空间,要么其目标命名空间与当前schema的目标命名空间相同
使用import包含的schema要么没有目标命名空间,要么其目标命名空间与当前schema的目标命名空间不同
例子:
imported.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://zpc.com" xmlns="http://zpc.com">
<xs:simpleType name="age_Type">
<xs:restriction base="xs:int">
<xs:maxExclusive value="100"/>
<xs:minExclusive value="0"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
import.xsd:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified" targetNamespace="http://www.zpc2.com" xmlns="http://www.zpc2.com" xmlns:zpc="http://zpc.com">
<!--导入名为imported.xsd的schema,被导入的schema的命名空间是http://zpc.com-->
<xs:import schemaLocation="imported.xsd" namespace="http://zpc.com"/>
<xs:element name="age" type="zpc:age_Type"/>
</xs:schema>例子1: namespace.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zpc.com/schema" xmlns="http://www.zpc.com/schema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <!--定义一个book-list元素--> <xs:element name="book-list"> <xs:complexType> <xs:sequence> <!--使用当前schema里的book元素,因为当前schema位于http://www.zpc.com/schema命名空间下,而且该命名空间没有指定对应限定短名,因此按如下格式使用book元素--> <xs:element ref="book" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <!--定义一个item_Type复杂类型--> <xs:complexType name="item_Type"> <xs:sequence> <xs:element name="book-name" type="xs:token"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> <!--这里使用http://www.zpc.com/schema下的item_Type类型时无需指定命名空间前缀--> <xs:element name="book" type="item_Type"/> </xs:schema> 对应的合格的xml文件: <?xml version="1.0" encoding="UTF-8"?> <book-list xmlns="http://www.zpc.com/schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.zpc.com/schema file:///C:/Documents%20and%20Settings/zhou/%e6%a1%8c%e9%9d%a2/namespace.xsd"> <book> <book-name/> <price>1</price> </book> <book> <book-name></book-name> <price>3</price> </book> </book-list> 例子2:增加了命名空间前缀 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zpc.com/schema" xmlns:zpc="http://www.zpc.com/schema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <!--定义一个book-list元素--> <xs:element name="book-list"> <xs:complexType> <xs:sequence> <!--使用当前schema里的book元素,因为当前schema位于http://www.zpc.com/schema命名空间下,而且该命名空间指定了对应限定短名zpc,因此按如下格式使用book元素--> <xs:element ref="zpc:book" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <!--定义一个item_Type复杂类型--> <xs:complexType name="item_Type"> <xs:sequence> <xs:element name="book-name" type="xs:token"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> <!--使用http://www.zpc.com/schema下的item_Type类型时必须指定命名空间前缀--> <xs:element name="book" type="zpc:item_Type"/> </xs:schema> 例子3:XML允许为任何元素指定xmlns:prefix属性 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.zpc.com/schema" xmlns:zpc="http://www.zpc.com/schema" elementFormDefault="qualified" attributeFormDefault="unqualified"> <!--定义一个book-list元素--> <xs:element name="book-list"> <xs:complexType> <xs:sequence xmlns:java="http://www.zpc.com/schema"> <!--使用当前schema里的book元素,因为当前schema位于http://www.zpc.com/schema命名空间下,而且该命名空间也对应限定短名java,因此这里的限定名可以使用两种--> <xs:element ref="java:book" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> <!--定义一个item_Type复杂类型--> <xs:complexType name="item_Type"> <xs:sequence> <xs:element name="book-name" type="xs:token"/> <xs:element name="price" type="xs:decimal"/> </xs:sequence> </xs:complexType> <!--限定名java只能作用在上面的位置,因为为某个元素指定了xmlns:prefix属性之后,该属性所引入的命名空间只对该元素及其子元素有效--> <xs:element name="book" type="zpc:item_Type"/> </xs:schema>在模式文档中,我们很容易就能区分出不同名称空间中的元素和类型,带有xs前缀的元素和类型属于http://www.w3.org/2001/XMLSchema名称空间,而其他的元素和类型则属于目标名称空间。
《语义网基础教程》学习笔记(一),布布扣,bubuko.com
原文:http://blog.csdn.net/hellozpc/article/details/38349077