Understanding XML Namespaces

Unique Names

The names payrollData and HRData are descriptive but they are also very common and two different organizations could potentially use the name payrollData for two different namespaces. This might become a problem if you start exchanging XML documents with other organizations. For example, you might want to send the payroll information to the IRS in XML. So you’d send them an XML document with a bunch of elements that belong to the payrollData namespace. If for some reason the IRS decides to process your payroll data along with payroll data from other companies, its likely another company will also have used the payrollData namespace because that name is just not unique. Its also likely that this company’s payroll document structure is different from yours. Bottom line: You’d have a situation where an application needs to deal with two different namespaces that have the same name.

To prevent such situations, namespace names must be globally unique. Technically, a namespace must be a Uniform Resource Identifer as defined in IETF RFC 2396. Basically, a URI is a unique string that can be classified as a locator (a Uniform Resource Locator or URL) or a name (a Uniform Resource Name or URN) or both. We all know URLs and use them regularly to access Web pages and other Internet resources. For example, http://www.developerfusion.com/ is a URL. A URN is globally unique resource name that sticks around forever and never goes away even if the resource itself no longer exists. For example, you could use a Universally Unique Identifier (UUID) as a namespace like this:

urn:uuid:EB3859CB-0B67-4053-9C40-CF9DED28C33F

This is considered a URN and is therefore a valid namespace name. So if you pick that as your namespace name for the payroll data (instead of payrollData), the XML document would become:

<hr:employees xmlns:hr="HRData"
xmlns:py=" urn:uuid:EB3859CB-0B67-4053-9C40-CF9DED28C33F">
<hr:employee>
    <hr:id>49982</hr:id>
    <hr:name>Bart Simpson</hr:name>
    <hr:hireDate>2000-07-04</hr:hireDate>
    <py:salary>4000765.00</py:salary>
    <py:taxes>3980765.27</py:taxes>
</hr:employee>
<hr:employee>
    <hr:id>12345</hr:id>
    <hr:name>Hugo Simpson</hr:name>
    <hr:hireDate>2000-05-29</hr:hireDate>
    <py:salary>82000.00</py:salary>
    <py:taxes>16567.87</py:taxes>    
</hr:employee>
</hr:employees>

All I did was change the namespace prefix mapping for the py prefix to point to the new namespace name. You can also use a URL as your namespace name, for example, http://www.developerfusion.com/schemas/payroll/ for the payroll data and http://www.developerfuison.com/schemas/hr/ for the HR data:

<hr:employees xmlns:hr="http://www.developerfusion.com/schemas/hr/"
xmlns:py="http://www.developerfusion.com/schemas/payroll/">
<hr:employee>
    <hr:id>49982</hr:id>
    <hr:name>Bart Simpson</hr:name>
    <hr:hireDate>2000-07-04</hr:hireDate>
    <py:salary>4000765.00</py:salary>
    <py:taxes>3980765.27</py:taxes>
</hr:employee>
<hr:employee>
    <hr:id>12345</hr:id>
    <hr:name>Hugo Simpson</hr:name>
    <hr:hireDate>2000-05-29</hr:hireDate>
    <py:salary>82000.00</py:salary>
    <py:taxes>16567.87</py:taxes>    
</hr:employee>
</hr:employees>

Note that the URL need not point to anything on the Internet. It is just used as a unique string. This is a source of confusion for most people who tend to think that there’s some document located at this URL. Just keep in mind a namespace name is simply a unique string, nothing more nothing less.

You might also like...

Comments

About the author

Yasser Shohoud United States

Yasser started programming at the age of 12 when he wrote his first text-based game on a Commodore PET. He's since moved to IBM mainframes then to Microsoft technologies and has worked as System...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold