XML Namespaces
You've probably heard of XML or know basically how an XML document is structured. XML is an important technology.
The basics of XML are actually quite simple, but because of all the additional technologies like DTD, XML schema, XPath, XQuery, XSL, XSLT and so on, XML quickly becomes an overwhelming technology.
The markup language for webpages, HTML is much easier to understand.
In this article I explain what XML namespaces are and try to keep things as simple as possible.
This is an example of a simple XML document, which is easy to understand:
<persons>
<person gender="male">
<firstname>John</firstname>
<lastname>Smith</lastname>
<haircolor>Brown</haircolor>
<length>1.82</length>
</person>
<person gender="female">
<firstname>Mary</firstname>
<lastname>Young</lastname>
<haircolor>Blond</haircolor>
<length>1.74</length>
</person>
</persons>In XML the hierarchy or nesting of elements and attributes is important. In the above example person, firstname, lastname, haircolor, length are all XML elements. Gender is an attribute of the person element. A person is part of (all) persons and a person contains the following information: firstname, lastname, haircolor and length. It's not always necessary for a person to contain all sub-elements, but that's another story of document validation (DTD and XML schema). In this article I only want to explain about XML namespaces.
You might have heard of XML namespaces. So what are XML namespaces actually? Well, XML namespaces make it possible to distinguish between the owners of elements and attributes in an XML document. With an owner I mean, the person or organisation that defined a certain element or attribute in an XML document.
With XML namespaces it's possible to make it clear who defined a certain element or attribute in an XML document. This distinction is important, because different persons or organizations give different meaning to a certain element or attribute name. XML parsers can make use of this information in order to process the XML document. An XML parser is a program that read an XML document and processes the information, for example store the information in your database.
For illustration purposes let's just say that I defined above XML document, and I want to state clearly that each element and attribute in above XML document is defined by me. I do that by introducing an XML namespace with the name 'mine'. Also I need to provide an URI (uniform resource identifier) for my namespace, so I just choose my domain http://www.software-secrets.com. By the way an URI identifies an internet resource. I just simply choose an URL. It's also possible to use a universal resource name (URN), but let's keep things as simple as possible.
So, let's modify above XML document to include my namespace:
<mine:persons
xmlns:mine='http://www.software-secrets.com'>
<mine:person mine:gender="male">
<mine:firstname>John</mine:firstname>
<mine:lastname>Smith</mine:lastname>
<mine:haircolor>Brown</mine:haircolor>
<mine:length>1.82</mine:length>
</mine:person>
<mine:person mine:gender="female">
<mine:firstname>Mary</mine:firstname>
<mine:lastname>Young</mine:lastname>
<mine:haircolor>Blond</mine:haircolor>
<mine:length>1.74</mine:length>
</mine:person>
</mine:persons>Now, when you want to use above XML document and add your own element or attribute, you can define your own namespace. Let's just say you name your namespace 'yours'. By adding your namespace to my XML document, you can safely add your own elements or attributes to my XML document.
For example:
<mine:persons
xmlns:mine='http://www.software-secrets.com'
xmlns:yours='http://www.yourdomain.com'>
<mine:person mine:gender="male">
<mine:firstname>John</mine:firstname>
<mine:lastname>Smith</mine:lastname>
<mine:haircolor>Brown</mine:haircolor>
<mine:length>1.82</mine:length>
<yours:age>21</yours:age>
</mine:person>
<mine:person mine:gender="female">
<mine:firstname>Mary</mine:firstname>
<mine:lastname>Young</mine:lastname>
<mine:haircolor>Blond</mine:haircolor>
<mine:length>1.74</mine:length>
<yours:age>17</yours:age>
</mine:person>
</mine:persons> Actually, the reason for XML namespaces is to avoid naming conflicts. Both you and me could have defined elements or attributes with the same name, but with different meaning. An XML parser can make use of the namespace information in order to make a clear distinction of this difference in meaning.
Hopefully this explanation of XML namespaces make this important technology much clearer for you.