Namespace for XML (transferred from a foreign blog)

Source: Internet
Author: User
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: int32 = "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 cocould look like this:

 

<Ns0: foo
Xmlns: ns0 = "urn: Example: FOO: 0"
Xmlns: NS1 = "urn: Example: FOO: 1"
Xmlns: int32 = "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
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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.