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

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?

<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:

<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):

<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:

<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:

<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:

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


This instance is valid:

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

你可能感兴趣的:(xml,Blog,UP)