Search code examples
xmlxsdurixml-namespacesxml-validation

Why can't we use random strings instead of URI's for XML namespaces?


I'm trying to understand the reasons of the XML-namespaces convention (using URI for namespaces).

I've read this thread on StackOverflow. I understand that namespace's fullname should be unique. But I still don't understand why can't we just use random strings for this?

For example, we're trying to distinguish the real cars from the toy cars in our file:

<?xml version="1.0" encoding="UTF-8"?>
<carstore xmlns:real="http://cxf.apache.org/jaxws/cars/real" xmlns:toy="http://cxf.apache.org/jaxws/cars/toy">
<real:car category="truck">
    <model lang="en">Scania R 770</model>
    <year>2005</year>
    <price currency="US dollar">200000.00</price>
</real:car>
<toy:car category="sedan">
    <title lang="en">Ford Focus</title>
    <year>2012</year>
    <price currency="US dollar">100.00</price>
</toy:car>
</carstore>


<?xml version="1.0" encoding="UTF-8"?>
<carstore xmlns:real="heroigairehgoiaer7272" xmlns:toy="289ry89fhfhbvnsdkljnv">
<real:car category="truck">
<model lang="en">Scania R 770</model>
<year>2005</year>
<price currency="US dollar">200000.00</price>
</real:car>
<toy:car category="sedan">
<title lang="en">Ford Focus</title>
<year>2012</year>
<price currency="US dollar">100.00</price>
</toy:car>
</carstore>

I'll be grateful if someone will explain why this option

xmlns:real="http://cxf.apache.org/jaxws/cars/real"

is better than this one:

xmlns:real="heroigairehgoiaer7272"

What negative consequences can we face using the second one? Probably there is any real-life examples?


Solution

  • Random, unique, strings could be used for namespace values, however...

    URI-based namespaces offer advantages over random strings:

    1. Leveraging the in-place ownership mechanisms of domain names avoids having to replicate that functionality for control of namespaces.
    2. Domain names communicate ownership, which is useful for identifying a party or project responsible for a namespace.
    3. Although namespace URIs do not have to be retrievable, they can be and often are, thus serving as a convenient means of retrieval of governing schemas or documentation.
    4. Hierarchical nature of URI's can mirror the natural hierarchical organization of name collections.