您的位置:首页 > 其它

关于xml的命名空间(转自国外一blog)

2006-08-11 19:50 218 查看

A Short Explanation of XML Namespaces

I wrote this up in response to a question on the Axis User discussion list, but thought others might appreciate it.

The purpose of a namespace qualification is to disambiguate two
components of the same name. For example, if you have multiple schemas
that each defines an element called "foo", how do you tell them apart?

[code]
    <s:schema
          xmlns:s="http://www.w3.org/2001/XMLSchema"
          targetNamespace="urn:example:foo:1">
       <s:element name="foo" type=x:string"/>
    </s:schema>   

    <s:schema
          xmlns:s="http://www.w3.org/2001/XMLSchema"
          targetNamespace="urn:example:foo:2">
       <s:element name="foo" type=x:int"/>
    </s:schema>

    <s:schema s="http://www.w3.org/2001/XMLSchema"
          xmlns:ns1="urn:example:foo:1"
          xmlns:ns2="urn:example:foo:2"
          targetNamespace="urn:example:foo:0">
      <s:import namespace="urn:example:foo:1"/>
      <s:import namespace="urn:example:foo:2"/>
      <s:element name="foo">
         <s:complexType>
             <s:sequence>
                <s:element ref="ns1:foo"/>
                <s:element ref="ns2:foo"/>
             </s:sequence>
         </s:complexType>
      </s:element>
    </s:schema>


An instance document of this element could look like this:

[code]
    <ns0:foo
        xmlns:ns0="urn:example:foo:0"
        xmlns:ns1="urn:example:foo:1"
        xmlns:ns2="urn:example:foo:2">
          <ns1:foo>some string</ns1:foo>
          <ns2:foo>12345</ns2:foo>
     </ns0:foo>


This instance is equally valid (but less readable):

[code]
    <tns:foo xmlns:tns="urn:example:foo:0">
       <tns:foo xmlns:tns="urn:example:foo:1">
          some string</tns:foo>
       <tns:foo xmlns:tns="urn:example:foo:2">
          12345</tns:foo>
    </tns:foo>


In other words, the string used for the prefix is irrelevant -- what
matters is the namespace it's been assigned to and the scope of the
namespace declaration. A namespace declaration applies to the element
it's defined in and all that element's children, but you can always
reassign the prefix to a different namespace in a child element.

The default namespace declaration (e.g., xmlns="urn:example:foo:0")
says that all non-explicitly qualified elements belong to the default
namespace. I generally recommend avoiding use of the default namespace
-- especially if you have unqualified elements -- because that forces
you to override the default namespace (e.g., xmlns="") on all
unqualified elements.

So for example, let's say I have this schema:

[code]
   <s:schema
        xmlns:s="http://www.w3.org/2001/XMLSchema"
        targetNamespace="urn:example:foobar">
      <s:element name="foobar">
         <s:complexType>
            <s:sequence>
               <s:element name="foo" type="s:string"/>
               <s:element name="bar" type="s:string"/>
            </s:sequence>
          </s:complexType>
      </s:element>
   </s:schema>


Because the schema does not specify elementFormDefault="qualified",
all local elements ("foo" and "bar") are unqualified. A valid instance
of this schema is:

[code]
  <tns:foobar xmlns:tns="urn:example:foobar">
     <foo>some string</foo>
     <bar>another string</bar>
  </tns:foobar>


But the following instance is not valid because "foo" and "bar" must be unqualified:

[code]
  <foobar xmlns="urn:example:foobar">
     <foo>some string</foo>
     <bar>another string</bar>
  </foobar>


This instance is valid:

[code]
  <foobar xmlns="urn:example:foobar">
     <foo xmlns="">some string</foo>
     <bar xmlns="">another string</bar>
  </foobar>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: