信息解析系统复习笔记,week5

什么是XML的名称空间?

XML名称空间是一组元素类型和属性名字的集合。它定义了区别同名元素和属性的的方法。

例如如下两段代码
<? xml version=“1.0” ?>
< address >
< street > Dandenong Rd  </ street >
< city > Caulfield East </ city >
< state > Victoria </ state >
< country > Australia </ country >
< postcode > 3145 </ postcode >
</ address >
<? xml version=“1.0” ?>
< server >
< name > aWebServer </ name >
< address > 130.194.225.1 </ address >
< server >

其中,address使我们通常意义上地址,而server元素下的address元素则是服务器的ip地址,二者意义不同但是名称相同,如果混合在一个xml文件中使用,则会产生误会。但是考察如下代码

<? xml version=“1.0” ?>
< department >
< name > Computer Science </ name >
< addr:address  xmlns:addr =“http://www.csse.monash.edu.au/xml/address”>
<addr:street > Dandenong Rd  </ addr:street >
< addr:city > Caulfield East </ addr:city >
< addr:state > Victoria </ addr:state >
< addr:country > Australia </ addr:country >
< addr:postcode > 3145 </ addr:postcode >
</ addr:address >
< serv:server  xmlns:serv =“http://www.csse.monash.edu.au/xml/servers”>
<serv:name > aWebServer </ serv:name >
< serv:address > 130.194.225.1 </ serv:address >
</ serv:server >
</ department >

这段代码使用xml的名称空间,所以即使address元素重名,也不会产生问题。看不懂不要紧,稍后我们会详细考虑。

为什么使用名称空间


因为我们要把来自不同文档中的代码片段混合使用而不产生名称冲突。同时,通过名称空间,我们可以产生可以重用的代码,这样就保证了特定的元素可以在多个文档中被调用,形成模块。

声明名称空间


我们可以通过使用 xmlns属性来为一个元素声明名称空间。属性的值就是要声明的名称空间。例如
< addr:address  xmlns:addr =“http://www.csse.monash.edu.au/xml/address”>

默认情况下,名称空间的格式是:
xmlns=URI or URL

而通常我们要指定一个详细的名称空间,称之为目标名称空间(target namespace),格式是这样的:
xmlns:prefix=URI or URL

上面那个addr就是使用这种方式来处理的。名称空间可以在xml文档实例、DTD、Schema和XSLT中使用。

在schema中:
< schema  xmlns =“http://www.w3.org/2001/XMLSchema”
xlmns:addr =“http://www.csse.monash.edu.au/xml/address”
targetNamespace: =“http://www.csse.monash.edu.au/xml/address”
elementFormDefault =“unqualified”
attributeFormDefault =“unqualified”>

在XML实例中
<? xml version="1.0" ?>
< adept:department  xsi:schemaLocation ="http://www.csse.monash.edu.au/xml/address
namespaceUnqualified.xsd"
 
xmlns:adept
="http://www.csse.monash.edu.au/xml/address"  
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" >

在已验证的XML 元素和属性中有
< schema  xmlns =“http://www.w3.org/2001/XMLSchema”
xlmns:addr =“http://www.csse.monash.edu.au/xml/address”
targetNamespace: =“http://www.csse.monash.edu.au/xml/address”
elementFormDefault =“qualified”
attributeFormDefault =“unqualified”>
<complexType name =“addressType”>
<sequence >
< element  name =”street”  type =“string”/>
<element name =“city”  type =“string”/>
<element name =“state”  type =“string”/>
<element name =“country”  type =“string”/>
</sequence >
</ complexType >
< element  name =“department”>
<complexType >
< sequence >
< element  name =“name”  type =“string”/>
<element name =“address”  type =“addr:addressType”/>
</sequence >
</ complexType >
</ element >
</ schema >

对于已验证的XML文档实例,使用名称空间的时候分为显式和隐式两种使用方法。对于显式,需要在每个元素前面加上名称空间,而隐式则无需这样做。

多重Schema

有些时候schema文件非常臃肿,需要将其分割成相对较小的schema文件。这样做可以帮助提升维护性、可读性和访问控制性。

在多个schema文件的情况下,使用名称空间有如下几种方法:

Include

对于两个或者多个来自于相同名称空间的文件,可使用include声明。
< xs:include  schemaLocation =address  of the schema >

所有include在内的文件必须,使用相同的targetNamespace。

参看如下四个例子
<? xml version="1.0" ?>
< xsd:schema  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"
    xmlns:addr
="http://www.csse.monash.edu.au/xml/address"
    targetNamespace
="http://www.csse.monash.edu.au/xml/address"
    elementFormDefault
="qualified"
    attributeFormDefault
="qualified" >

    
< xsd:complexType  name ="deptType" >
      
< xsd:sequence >
        
< xsd:element  name ="street"  type ="xsd:string" />
        
< xsd:element  name ="city"  type ="xsd:string" />
        
< xsd:element  name ="state"  type ="xsd:string" />
        
< xsd:element  name ="country"  type ="xsd:string" />
      
</ xsd:sequence >
     
</ xsd:complexType >
     
     
< xsd:complexType  name ="serverType" >
         
< xsd:sequence >
             
< xsd:element  name ="IP"  type ="xsd:string" />
             
< xsd:element  name ="domainName"  type ="xsd:string" />
             
< xsd:element  name ="alias"  type ="xsd:string"  minOccurs ="0"  maxOccurs ="unbounded" />
         
</ xsd:sequence >
     
</ xsd:complexType >
</ xsd:schema >
<? xml version="1.0" ?>
< xsd:schema  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"
        targetNamespace
="http://www.csse.monash.edu.au/xml/address"
        xmlns:addr
="http://www.csse.monash.edu.au/xml/address"
        elementFormDefault
="unqualified"
        attributeFormDefault
="unqualified" >

< xsd:include  schemaLocation ="department.xsd" />
< xsd:include  schemaLocation ="server.xsd" />

< xsd:element  name ="faculty" >
    
< xsd:complexType >
        
< xsd:sequence >
            
< xsd:element  name ="name"  type ="xsd:string" />
            
< xsd:element  name ="organisationalUnit"  maxOccurs ="unbounded" >
                
< xsd:complexType >
                    
< xsd:sequence >
                        
< xsd:element  ref ="addr:department" />
                        
< xsd:element  ref ="addr:servers" />
                    
</ xsd:sequence >
                
</ xsd:complexType >  
            
</ xsd:element >
        
</ xsd:sequence >
    
</ xsd:complexType >

</ xsd:element >
</ xsd:schema >
<? xml version="1.0" ?>
< xsd:schema  targetNamespace ="http://www.csse.monash.edu.au/xml/address"
 xmlns:addr
="http://www.csse.monash.edu.au/xml/address"
  xmlns:xsd
="http://www.w3.org/2001/XMLSchema"  
  elementFormDefault
="qualified"  
  attributeFormDefault
="qualified" >
    
< xsd:include  schemaLocation ="bottom.xsd" />
    
< xsd:element  name ="servers" >
        
< xsd:complexType >
            
< xsd:sequence >
                
< xsd:element  name ="server"  type ="addr:serverType"  maxOccurs ="unbounded" />
            
</ xsd:sequence >
            
< xsd:attribute  name ="organisation"  type ="xsd:string" />
        
</ xsd:complexType >
    
</ xsd:element >
</ xsd:schema >
<? xml version="1.0" ?>
< xsd:schema  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"
        targetNamespace
="http://www.csse.monash.edu.au/xml/address"
        xmlns:addr
="http://www.csse.monash.edu.au/xml/address"
        elementFormDefault
="unqualified"
        attributeFormDefault
="qualified" >

 
< xsd:include  schemaLocation ="bottom.xsd" />
 
 
< xsd:element  name ="department" >
      
< xsd:complexType >
        
< xsd:sequence >
          
< xsd:element  name ="name"  type ="xsd:string" />
          
< xsd:element  name ="address"  type ="addr:deptType" />
          
< xsd:element  name ="webServer"  type ="addr:serverType" />
        
</ xsd:sequence >
      
</ xsd:complexType >
    
</ xsd:element >
</ xsd:schema >

Import

Import声明结合两个或多个来自不同名称空间的文档到一起。但是,只有全局元素和有名字的类型才能被导入。
< import  namespace =namespace  to be imported schemaLocation =“schema  to be imported” >

例如:
<? xml version="1.0" ?>
< xsd:schema  xmlns:xsd ="http://www.w3.org/2001/XMLSchema"
        targetNamespace
="http://www.monash.edu.au/xml/uni"
        xmlns:uni
="http://www.monash.edu.au/xml/uni"
        xmlns:xaddr
="http://www.csse.monash.edu.au/xml/address"
        elementFormDefault
="qualified" >

< xsd:import  namespace ="http://www.csse.monash.edu.au/xml/address"  schemaLocation ="faculty.xsd" />

< xsd:element  name ="university" >
    
< xsd:complexType >
        
< xsd:sequence >
            
< xsd:element  ref ="xaddr:faculty" />
            
< xsd:element  name ="dean"  type ="xsd:string" />
        
</ xsd:sequence >
    
</ xsd:complexType >
</ xsd:element >

</ xsd:schema >

多重Schema名称空间的设计

同源名称空间
– 所有的名称空间使用相同的名称
– 使用<include> 元素来组合多个XML Schemas

异源名称空间
– 每个schema使用不同的名称空间
– 使用 <import> 元素来组合多个XML Schemas

不定式名称空间
– 主XML Schema有target Namespace
– 其他的要包含到主 XML Schemas中的schema没有自己的target namespace

不定式名称空间的问题


假设有三个schema,A、B和C,要组装成3个大的schema文件1、2和3,其中1含有AB,2含有AC,3要含有1和2。这样问题就来了,如果命名类型A在1和2中不是不同的类型就会导致冲突。为了避免这种情况的发生,要使用代理模式下的schema。

1-proxy.xsd
– targetNamespace="http://www.1-proxy.org"
– <xsd:include schemaLocation="1.xsd"/>
2-proxy.xsd
– targetNamespace="http://www.2-proxy.org"
– <xsd:include schemaLocation="2.xsd"/>
main.xsd
– targetNamespace="http://www.main.org"
– <xsd:import namespace="http://www.1-proxy.org" schemaLocation="1-proxy.xsd"/>
– <xsd:import namespace="http://www.2-proxy.org" schemaLocation="2-proxy.xsd"/>
实际上是为1和2每个都多写了一个xsd。代理schema允许设计和分配不同的名称空间来供应用程序使用。

什么时候使用同源设计


当你所有的xml都是概念上相关的时候,就要使用同源设计。
当不需要线性或者从文档实例中继承虚拟标识符的时候,例如”元素A不是来自于schemaX“
当无需给元素分类的时候

什么时候使用异源设计

当多个元素使用相同的名字时候,为了避免名称冲突
当需要线性或者从文档实例中继承虚拟标识符的时候,例如”元素A来自于schemaX“

什么时候使用不定式设计

当元素使用的组件组合而成,而组成其主题的元素本身与主元素之间没有语义上继承关系的时候
当你要对模式进行硬编码,而非使用include来提供其自身应用特征的时候
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值