Search code examples
xmlxml-namespaces

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>

Solution

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