在 XML Schema 中使用名称空间

在这个协作的世界中,一个人可能处理来自多个其他团体的文档,而不同的团体可能希望以不同的方式表示他们的数据元素。此外,他们还可能在一个文档中引用不同团体创建的同名元素。如何区分相同名字的不同定义呢?XML Schema 使用名称空间区分这些定义。

 

清单1:

Xml代码
  1. < element   name = 'InvoiceNo'   type = 'positive-integer' />   
  2. < element   name = 'ProductID'   type = 'ProductCode' />   
  3.   
  4. < simpleType   name = 'ProductCode'   base = 'string' >   
  5.   < pattern   value = '[A-Z]{1}d{6}' />   
  6. </ simpleType >   
<element name='InvoiceNo' type='positive-integer'/>
<element name='ProductID' type='ProductCode'/>

<simpleType name='ProductCode' base='string'>
  <pattern value='[A-Z]{1}d{6}'/>
</simpleType>

    一个给定的 XML Schema 定义了一组新名字,如元素名、类型名、属性名、属性组名,这些名字的定义和声明都写在模式中。 清单1 定义的名字包括 InvoiceNo 、 ProductID 和 ProductCode 。

 

1、目标名称空间
    我们说模式中定义的名字属于它的 目标名称空间。名称空间本身有一个固定但没有限制的名字,必须符合 URL 语法。比如,对于 清单1 中模式片段,您可以把名称空间的名字设为: http://www.SampleStore.com/Account

    名称空间的名字语法容易让人混淆,尽管以 http:// 开始,那个 URL 并不指向一个包含模式定义的文件。事实上,这个 URL http://www.SampleStore.com/Account 根本没有指向任何文件,只是一个分配的名字。

 

2、源名称空间
    模式中的定义和声明可能引用属于其他名称空间的名字。在本文中,我们称这些名称空间为 源名称空间。每个模式都有一个目标名称空间,但可能有多个源名称空间。名称空间的名字可能相当长,但在 XML 文档中通过 xmlns 声明可使用简写形式。

 

清单 2:目标名称空间和源名称空间

Xml代码
  1. <!--XML Schema fragment in file schema1.xsd -->    
  2. < xsd :schema   targetNamespace = 'http://www.SampleStore.com/Account'   
  3.             xmlns:xsd = 'http://www.w3.org/1999/XMLSchema'   
  4.             xmlns:ACC'http://www.SampleStore.com/Account' >   
  5.   < xsd :element   name = 'InvoiceNo'   type = 'xsd :positive-integer' />   
  6.   < xsd :element   name = 'ProductID'   type = 'ACC:ProductCode' />   
  7.   
  8.   < xsd :simpleType   name = 'ProductCode'   base = 'xsd :string' >   
  9.     < xsd :pattern   value = '[A-Z]{1}d{6}' />   
  10.   </ xsd :simpleType >    
  11. </ xsd :schema >   
<!--XML Schema fragment in file schema1.xsd

--> 
<xsd

:schema targetNamespace='http://www.SampleStore.com/Account'
            xmlns:xsd

='http://www.w3.org/1999/XMLSchema'
            xmlns:ACC= 'http://www.SampleStore.com/Account'>
  <xsd

:element name='InvoiceNo' type='xsd

:positive-integer'/>
  <xsd

:element name='ProductID' type='ACC:ProductCode'/>

  <xsd

:simpleType name='ProductCode' base='xsd

:string'>
    <xsd

:pattern value='[A-Z]{1}d{6}'/>
  </xsd

:simpleType> 
</xsd

:schema>

    目标名称空间 targetNamespace 的名字是 http://www.SampleStore.com/Account ,其中包含的名字有 InvoiceNo 、ProductID 和 ProductCode 。
    schema 、 element 、 simpleType 、 pattern 、 string 和 positive-integer这些名字属于源名称空间http://www.w3.org/1999/XMLSchema ,通过 xmlns 声明缩写为 xsd
    别名 xsd 没有任何特殊的地方,我们可以选择任何其他的名字。在本文后面的部分为了方便和简化起见,我们使用 xsd 代表名称空间 http://www.w3.org/1999/XMLSchema ,在一些代码片段中省略了限定符 xsd
    在这个例子中, targetNamespace 偶尔也作为一个源名称空间,因为要使用名字 ProductCode 定义其他的名字。

 

3、模式的位置和使用默认名称空间
    对于整个“模式的模式”, http://www.w3.org/1999/XMLSchema ,不需要指定位置,因为它的位置是人所共知的。
    对于源名称空间 http://www.SampleStore.com/Account ,也不需要指定位置,因为它恰好是该文件中定义的目标名称空间。

    为了更好地理解如何指定模式的位置和使用默认名称空间,看一看 清单 3中扩展的例子。

 

清单 3:多个源名称空间,导入一个名称空间

Xml代码
  1. <!--XML Schema fragment in file schema1.xsd -->   
  2. < schema   targetNamespace = 'http://www.SampleStore.com/Account'   
  3.         xmlns = 'http://www.w3.org/1999/XMLSchema'   
  4.         xmlns:ACC'http://www.SampleStore.com/Account'   
  5.         xmlns:PART'http://www.PartnerStore.com/PartsCatalog' >   
  6.   < import   namespace = 'http://www.PartnerStore.com/PartsCatalog'   schemaLocation = 'http://www.ProductStandards.org/repository/alpha.xsd ' />   
  7.   
  8.   < element   name = 'InvoiceNo'   type = 'positive-integer' />   
  9.   < element   name = 'ProductID'   type = 'ACC:ProductCode' />   
  10.   
  11.   < simpleType   name = 'ProductCode'   base = 'string' >   
  12.     < pattern   value = '[A-Z]{1}d{6}' />   
  13.   </ simpleType >   
  14.   
  15.   < element   name = 'stickyGlue'   type = 'PART:SuperGlueType' />    
  16. </ schema >   
<!--XML Schema fragment in file schema1.xsd

-->
<schema targetNamespace='http://www.SampleStore.com/Account'
        xmlns='http://www.w3.org/1999/XMLSchema'
        xmlns:ACC= 'http://www.SampleStore.com/Account'
        xmlns:PART= 'http://www.PartnerStore.com/PartsCatalog'>
  <import namespace='http://www.PartnerStore.com/PartsCatalog' schemaLocation='http://www.ProductStandards.org/repository/alpha.xsd

'/>

  <element name='InvoiceNo' type='positive-integer'/>
  <element name='ProductID' type='ACC:ProductCode'/>

  <simpleType name='ProductCode' base='string'>
    <pattern value='[A-Z]{1}d{6}'/>
  </simpleType>

  <element name='stickyGlue' type='PART:SuperGlueType'/> 
</schema>

    清单 3中多了一个名称空间引用: http://www.PartnerStore.com/PartsCatalog 。这个名称空间不同于 targetNamespace 和标准名称空间。因此必须使用 import 声明元素引入,该元素的 schemaLocation 属性指明包含模式的文件位置。
    默认的名称空间是 http://www.w3.org/1999/XMLSchema ,它的 xmlns 声明没有名字。每个非限定的名字如 schema 和 element ,都属于默认名称空间 http://www.w3.org/1999/XMLSchema 。如果模式从一个名称空间中引用了多个名字,将其指定为默认名字空间更方便。

    一个 XML 实例文档可能引用多个名称空间的元素名,这些名称空间定义在不同模式中。为了引用和简化名称空间的名字,同样要使用 xmlns 声明。我们使用 XML Schema 实例名称空间的 schemaLocation 属性指定文件的位置。要注意,该属性不同于上一个例子中 xsd 名称空间的同名属性 schemaLocation 。

 

清单 4:使用来自多个模式的多个名称空间的名字

Xml代码
  1. <? xml   version = "1.0" ?>   
  2. < ACC:rootElement    xmlns:ACC = 'http://www.SampleStore.com/Account'   
  3.                  xmlns:PART = 'http://www.PartnerStore.com/PartsCatalog'   
  4.                  xmlns:xsi = 'http://www.w3.org/1999/XMLSchema-instance'   
  5.                  xsi:schemaLocation ='http://www.PartnerStore.com/PartsCatalog http://www.ProductStandards.org/repository/alpha.xsd   
  6.                                      http://www.SampleStore.com/Account http://www.SampleStore.com/repository/schema1.xsd '>   
  7.   < ACC:InvoiceNo > 123456789 </ ACC:InvoiceNo >    
  8. </ ACC:rootElement >  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值