级别: 初级 Brett McLaughlin (brett@oreilly.com), 作家兼编辑, O'Reilly and Associates
2002 年 12 月 01 日 本技巧文章将说明 XML Schema 规范如何要求每个名称空间有一个 XML Schema,并将演示如何在一个文档中包括多个模式以及如何从一个模式来引用另一个模式。
随着 XML 名称空间的出现,XML 文档带有一个、两个甚至更多 XML 名称空间的现象变得很普遍。内容作者使用多个名称空间分隔内容,并且根据一组约束来验证文档的特定部分,而根据另一组约束来验证文档的另一部分。然而,随着 XML 名称空间的出现,XML Schema 规范给复杂的事物又增加了一些额外的复杂性。尽管 XML 模式允许一个更丰富的模型用于详述元素或属性的内容,但是在同一个文档内处理多个名称空间和多个模式容易出错。
CD 数据库:一个示例
在讨论细节以前,让我们研究一个简单的 XML 文档,我将用这个文档作为示例来说明这些概念。
清单 1中的文档,尽管没有多大价值,但它的确使用了两个不同的名称空间。
清单 1. 带两个名称空间的 XML
<?xml version="1.0"?>
<database xmlns="http://www.newInstance.com/cd-database"
xmlns:sh="http://www.sugarhillrecords.com"
>
<cd title="Nickel Creek">
<artist>Nickel Creek</artist>
<sh:album><
<sh:description>Bluegrass Revivalists, Acoustic Innovators,
Youthgrass are just some of the terms that have been
used to describe Nickel Creek over the past year -- perhaps
producer Alison Krauss says it best:It's just Nickel Creek
music.</sh:description>
<sh:producer>Alison Krauss</sh:producer>
</sh:album>
</cd>
<cd title="Ice Caps: Peaks of Telluride">
<artist>Sam Bush</artist>
<sh:album>
<sh:description>Sam Bush's amazing mandolin and fiddle
playing, singing and outrageous stage presence have
been captivating audiences since the late 1960s. So what
could be better-or more fun-than a live Sam Bush album
recorded at the Telluride Bluegrass Festival, where he
has performed for 26 years?
</sh:description>
<sh:producer>Sam Bush</sh:producer>
</sh:album>
</cd>
</database>
|
对于任何不了解允许该文档包含什么信息的人来说,这个文档不是很有用;出于这个原因,您需要将模式分配给文档中使用的每个名称空间。尽管这里只有两个名称空间,但您可以很容易地把所介绍的这种思想推广到三个或更多的名称空间。首先,您要使用
schemaLocation 属性,用正确的名称空间来匹配每个名称空间。那还需要增加另一个名称空间;
清单 2演示了做出这两项更改后的结果。
清单 2. 用 XML 模式匹配名称空间
<?xml version="1.0"?>
<database xmlns="http://www.newInstance.com/cd-database"
xmlns:sh="http://www.sugarhillrecords.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.newInstance.com/cd-database cd-database.xsd
http://www.sugarhillrecords.com sugarHill.xsd"
>
<cd title="Nickel Creek">
<artist>Nickel Creek</artist>
<sh:album><
<sh:description>Bluegrass Revivalists, Acoustic Innovators,
Youthgrass are just some of the terms that have been used
to describe Nickel Creek over the past year -- perhaps
producer Alison Krauss says it best:It's just Nickel Creek
music.</sh:description>
<sh:producer>Alison Krauss</sh:producer>
</sh:album>
</cd>
<cd title="Ice Caps: Peaks of Telluride">
<artist>Sam Bush</artist>
<sh:album>
<sh:description>Sam Bush's amazing mandolin and fiddle playing,
singing and outrageous stage presence have been captivating
audiences since the late 1960s. So what could be better-or
more fun-than a live Sam Bush album recorded at the Telluride
Bluegrass Festival, where he has performed for 26 years?
</sh:description>
<sh:producer>Sam Bush</sh:producer>
</sh:album>
</cd>
</database>
|
到目前为止,一切都还不太困难。名称空间 URI 与用于该 URI 的 XML 模式结成一对,然后对下一个名称空间/模式对重复同样的事情。然而,当您开始使用 XML 模式本身时,事情就变得棘手了。
CD 数据库模式
首先,让我们从即将被引用的模式着手;该模式将用于文档的 Sugar Hill 部分,模式的名称为
sugarHill.xsd。这是最好的起点,因为它
不必引用其它模式(反过来就不正确了)。
清单 3说明了这个模式。
清单 3. Sugar Hill 模式
<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.sugarhillrecords.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
>
<xs:element name="album">
<xs:complexType>
<xs:sequence>
<xs:element name="description" type="xs:string" />
<xs:element name="producer" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
|
这里没有什么太奇怪的事,对吗?只是您的普通 XML 模式。然而,看看
清单 4,它演示了一个 CD 数据库模式的正在开发的版本,该模式用于
http://www.newInstance.com/cd-database名称空间。
清单 4. CD 数据库模式
<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.newInstance.com/cd-database"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
>
<xs:element name="cd">
<xs:complexType>
<xs:sequence>
<xs:element name="artist" type="xs:string" />
<xs:element ref="sh:album" />
</xs:sequence>
<xs:attribute name="title" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
|
这个模式看起来没问题,但它是不完善的。尽管正确地定义了结构,但您的 XML 解析器在试图解析和计算 CD 数据库模式时会报错。那是因为它不知道如何处理
sh:album 。名称空间前缀
sh 没有被分配给名称空间 URI,而且它不知道定义
album 元素的模式在哪里。实际上,修正这个问题比您想象的要容易,接下来,我将处理这个问题。
导入模式
首先,您需要定义 Sugar Hill 名称空间 URI;这很简单,您在最初的 XML 文档中就已经这样做过了。然后,也是较难被理解的,您需要导入另一个您正在使用的 XML 模式。
清单 5 演示了
import 元素(来自 XML Schema 定义名称空间)的使用方法,以及它如何将两个 XML 模式文档链接在一起。
清单 5. CD 数据库模式
<?xml version="1.0"?>
<xs:schema targetNamespace="http://www.newInstance.com/cd-database"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sh="http://www.sugarhillrecords.com"
elementFormDefault="qualified"
>
<xs:import namespace="http://www.sugarhillrecords.com" schemaLocation="sugarHill.xsd"/>
<xs:element name="cd">
<xs:complexType>
<xs:sequence>
<xs:element name="artist" type="xs:string" />
<xs:element ref="sh:album" />
</xs:sequence>
<xs:attribute name="title" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
|
增加了这两项之后,现在一切都应工作正常。您的模式现在引用 Sugar Hill 模式,并且可以解析和使用
album 元素定义。此外,您的 XML 文档已经引用了这两个模式,因此在解析文档时不会有任何问题。
结束语
理论上,本技巧文章填补了有关使用多个模式的一些空白。当我在内容创作中第一次碰到这个问题时,我花了很长时间辛苦地通读规范以寻找答案。希望本文能使您避免做同样的事情。希望对您有用,期待在网上遇见您!
参考资料
关于作者  | 
|  |
Brett McLaughlin自从流行 Logo 语言的时代开始就一直从事计算机行业(还记得小三角形吗?)。目前,他专门从事用 Java 相关的技术构建应用程序的基础结构。最近几年,他在 Nextel Communications 和 Allegiance Telecom, Inc. 从事这些基础结构的实现。Brett 是 Java Apache Turbine 项目的共同创始人之一,该项目通过使用 Java servlet 为 Web 应用程序开发构建可重用组件体系结构。他还是 EJBoss 项目(一种开放源码 EJB 应用程序服务器)和 Cocoon(一种开放源码 XML Web 发布引擎)的志愿开发人员之一。
|
对本文的评价
|