TargetNameSpace和DefaultNameSpace之间的区别

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

例如:

  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的命名空间就是这个咯:)

你可能感兴趣的:(XML)