user5539357
user5539357

Reputation: 1066

XML namespaces conflicts

I was wondering why do we have to define our namespaces with an URI that is unique for our organization (just like packages in Java). That would obviously make sense and let us avoid name conflicts. However, this page says:

It is also possible (though not recommended) for the same prefix refer to different namespaces, depending on their context

So basically if I have my own XML document with my own namespace prefix myNamespace defined as http://hisdomain.com/test, somebody could redefine the 'myNamespace' prefix to point to his own namespace http://hisdomain.com/test, even by mistake (I think there is no alert if I define a namespace prefix that has been already defined in the document).

Even with URIs, other people need to make sure they don't use my prefix!

Then using URIs and having to provide definitions to namespaces becomes useless, the namespace system would work equally well if we simply used the prefixes only without having to provide definitions using xlns attribute.


If I understand correctly, this is not allowed:

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">
...
</root>

but this one is okay - you could redefine the abc prefix with your domain.

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>

Upvotes: 1

Views: 1833

Answers (2)

Cosyn
Cosyn

Reputation: 5017

The point is every namespace has its own scope. A descendant namespace declaration can override the same prefix that is defined in an ancestor.

For example, the other party has defined a namespace prefix abc with http://hisdomain.com/test. Then you insert your content in their XML file defining the same prefix abc with your domain http://yourdomain.com/test, getting something like this:

<abc:root xmlns:abc="http://hisdomain.com/test">
    <abc:width>100</abc:width>
    <abc:width xmlns:abc="http://yourdomain.com/test">200</abc:width>
</abc:root>

The scope of http://hisdomain.com/test is root and 100 width, while the scope of http://yourdomain.com/test is 200 width. This is totally fine as long as both you and the other party are using query APIs that specify the full URI of the namespace. That way both you and they can find the correct element.

Reference

https://www.w3.org/TR/xml-names/#scoping

Upvotes: 0

kjhughes
kjhughes

Reputation: 111786

Even with URIs, other people need to make sure they don't use my prefix!

Correct, within an XML document, namespace prefix declarations should not conflict.

The key, however, is that the namespace prefix itself is insignificant. It is syntactic sugar for the full namespace name. Conformant XML processors will not care what you or other parties use for namespace prefixes. Namespace prefixes are just shorthand conveniences; namespace names are what matter.

Then using URIs and having to provide definitions to namespaces becomes useless, the namespace system would work equally well if we simply used the prefixes only without having to provide definitions using xlns attribute.

Not true.

The namespace system would not work equally well if component names were burdened with having to be long enough to support the uniqueness requirements of namespace names. We wouldn't like the extra verbosity.


Update for new addition to question:

Correct, this is not allowed (similar to how two attributes could not have the same name for a given element):

<root xmlns:abc="mydomain.com/test" xmlns:abc="yourdomain.com/test">

But this is allowed (but better avoided):

<root xmlns:abc="mydomain.com/test">
    <body xmlns:abc="yourdomain.com/test">
    ...//to use my namespace here, I'd need to redefine it again to mydomain.com/test

    </body>
</root>

Upvotes: 5

Related Questions