TargetNameSpace和DefaultNameSpace之间的区别

本文解析了XML Schema中TargetNameSpace与DefaultNameSpace的区别及作用。TargetNameSpace用于标识Schema定义元素的命名空间,而DefaultNameSpace则为Schema定义过程中的默认命名空间。通过实例说明了如何使用这两种命名空间。

N久没有看XML SCHAME的规范了,在项目中一直搞不清楚TargetNameSpace和DefaultNameSpace之间的区别。今天去查了哈文档。

根据我的理解TargetNameSpace只在定义XML SCHAME的时候才有用,它的主要作用是标明Schame定义的元素的命名空间,而DefaultNameSpace则只是标明在定义这个Schame中用的默认命名空间比如:http://www.w3.org/2001/XMLSchema就是在定义一个XSD的时候的默认命名空间,而定义出来的Schame的命名空间则是根据需要指定。

下面是W3C(http://www.w3.org/TR/2004/PER-xmlschema-0-20040318/#NS)上关于这个问题的解释:

A schema can be viewed as a collection (vocabulary) of type definitions and element declarations whose names belong to a particular namespace called a target namespace. Target namespaces enable us to distinguish between definitions and declarations from different vocabularies. For example, target namespaces would enable us to distinguish between the declaration for element in the XML Schema language vocabulary, and a declaration for element in a hypothetical chemistry language vocabulary. The former is part of the http://www.w3.org/2001/XMLSchema target namespace, and the latter is part of another target namespace.

When we want to check that an instance document conforms to one or more schemas (through a process called schema validation), we need to identify which element and attribute declarations and type definitions in the schemas should be used to check which elements and attributes in the instance document. The target namespace plays an important role in the identification process. We examine the role of the target namespace in the next section.

The schema author also has several options that affect how the identities of elements and attributes are represented in instance documents. More specifically, the author can decide whether or not the appearance of locally declared elements and attributes in an instance must be qualified by a namespace, using either an explicit prefix or implicitly by default. The schema author's choice regarding qualification of local elements and attributes has a number of implications regarding the structures of schemas and instance documents, and we examine some of these implications in the following sections

例如:

<description></description>

  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <description    
  3.     xmlns="http://www.w3.org/ns/wsdl"  
  4.     targetNamespace"http://greath.example.com/2004/wsdl/resSvc"    
  5.     xmlns:tns"http://greath.example.com/2004/wsdl/resSvc"  
  6.     xmlns:ghns = "http://greath.example.com/2004/schemas/resSvc"  
  7.     xmlns:wsoap"http://www.w3.org/ns/wsdl/soap"  
  8.     xmlns:soap="http://www.w3.org/2003/05/soap-envelope"  
  9.     xmlns:wsdlx"http://www.w3.org/ns/wsdl-extensions">  
  10.   
  11.   <documentation>  
  12.     This document describes the GreatH Web service.  Additional    
  13.     application-level requirements for use of this service --    
  14.     beyond what WSDL 2.0 is able to describe -- are available    
  15.     at http://greath.example.com/2004/reservation-documentation.html   
  16.   </documentation>  
  17.   
  18.   <types>  
  19.     <xs:schema    
  20.         xmlns:xs="http://www.w3.org/2001/XMLSchema"  
  21.         targetNamespace="http://greath.example.com/2004/schemas/resSvc"  
  22.         xmlns="http://greath.example.com/2004/schemas/resSvc">  
  23.   
  24.       <xs:element name="checkAvailability" type="tCheckAvailability"/>       
  25.       <xs:complexType name="tCheckAvailability">        
  26.         <xs:sequence>         
  27.           <xs:element  name="checkInDate" type="xs:date"/>         
  28.           <xs:element  name="checkOutDate" type="xs:date"/>         
  29.           <xs:element  name="roomType" type="xs:string"/>         
  30.         </xs:sequence>        
  31.       </xs:complexType>      
  32.                
  33.       <xs:element name="checkAvailabilityResponse" type="xs:double"/>       
  34.        
  35.       <xs:element name="invalidDataError" type="xs:string"/>       
  36.   
  37.     </xs:schema>       
  38.   </types>  
xml 代码

在这个WSDL中,默认的命名空间是http://www.w3.org/ns/wsdl所以TYPES的命名空间就是它,而TargetNameSpace是http://greath.example.com/2004/schemas/resSvc 所以定义的checkAvailability这个ELEMENT的命名空间就是这个咯:)

总结下面代码 xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.w3.org/2003/05/soap-envelope" targetNamespace="http://www.w3.org/2003/05/soap-envelope" elementFormDefault="qualified"> <xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="http://www.w3.org/2001/xml.xsd"/> <!-- Envelope, header and body --> <xs:element name="Envelope" type="tns:Envelope"/> <xs:complexType name="Envelope"> <xs:sequence> <xs:element ref="tns:Header" minOccurs="0"/> <xs:element ref="tns:Body" minOccurs="1"/> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType> <xs:element name="Header" type="tns:Header"/> <xs:complexType name="Header"> <xs:annotation> <xs:documentation> Elements replacing the wildcard MUST be namespace qualified, but can be in the targetNamespace </xs:documentation> </xs:annotation> <xs:sequence> <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType> <xs:element name="Body" type="tns:Body"/> <xs:complexType name="Body"> <xs:sequence> <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType> <!-- Global Attributes. The following attributes are intended to be usable via qualified attribute names on any complex type referencing them. --> <xs:attribute name="mustUnderstand" type="xs:boolean" default="0"/> <xs:attribute name="relay" type="xs:boolean" default="0"/> <xs:attribute name="role" type="xs:anyURI"/> <!-- 'encodingStyle' indicates any canonicalization conventions followed in the contents of the containing element. For example, the value 'http://www.w3.org/2003/05/soap-encoding' indicates the pattern described in the SOAP Version 1.2 Part 2: Adjuncts Recommendation --> <xs:attribute name="encodingStyle" type="xs:anyURI"/> <xs:element name="Fault" type="tns:Fault"/> <xs:complexType name="Fault" final="extension"> <xs:annotation> <xs:documentation> Fault reporting structure </xs:documentation> </xs:annotation> <xs:sequence> <xs:element name="Code" type="tns:faultcode"/> <xs:element name="Reason" type="tns:faultreason"/> <xs:element name="Node" type="xs:anyURI" minOccurs="0"/> <xs:element name="Role" type="xs:anyURI" minOccurs="0"/> <xs:element name="Detail" type="tns:detail" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="faultreason"> <xs:sequence> <xs:element name="Text" type="tns:reasontext" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="reasontext"> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute ref="xml:lang" use="required"/> </xs:extension> </xs:simpleContent> </xs:complexType> <xs:complexType name="faultcode"> <xs:sequence> <xs:element name="Value" type="tns:faultcodeEnum"/> <xs:element name="Subcode" type="tns:subcode" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:simpleType name="faultcodeEnum"> <xs:restriction base="xs:QName"> <xs:enumeration value="tns:DataEncodingUnknown"/> <xs:enumeration value="tns:MustUnderstand"/> <xs:enumeration value="tns:Receiver"/> <xs:enumeration value="tns:Sender"/> <xs:enumeration value="tns:VersionMismatch"/> </xs:restriction> </xs:simpleType> <xs:complexType name="subcode"> <xs:sequence> <xs:element name="Value" type="xs:QName"/> <xs:element name="Subcode" type="tns:subcode" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="detail"> <xs:sequence> <xs:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/> </xs:sequence> <xs:anyAttribute namespace="##other" processContents="lax"/> </xs:complexType> <!-- Global element declaration and complex type definition for header entry returned due to a mustUnderstand fault --> <xs:element name="NotUnderstood" type="tns:NotUnderstoodType"/> <xs:complexType name="NotUnderstoodType"> <xs:attribute name="qname" type="xs:QName" use="required"/> </xs:complexType> <!-- Global element and associated types for managing version transition as described in Appendix A of the SOAP Version 1.2 Part 1 Recommendation --> <xs:complexType name="SupportedEnvType"> <xs:attribute name="qname" type="xs:QName" use="required"/> </xs:complexType> <xs:element name="Upgrade" type="tns:UpgradeType"/> <xs:complexType name="UpgradeType"> <xs:sequence> <xs:element name="SupportedEnvelope" type="tns:SupportedEnvType" minOccurs="1" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:schema>
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值