Namespaces
Namespaces are used to prevent name conflicts or name collisions by containing the names under another names. This another name, i.e. name of the abstract container, is called namespace.
Example:
- farming:crop
- image_editing:crop
Here crop identifier has more than one meaning. Usage of this identifier without namespaces is unambiguous. By using proper namespaces or containing them in separate namespaces (or categories), farming and image_editing gives unique names and hence prevents the naming collisions.
More Examples:
- computer_keyboard:key
- cryptography:key
- lock:key
- surface:flat
- real_estate:flat
In Programming Languages
Namespaces exist in programming languages also. Wherein these are used to prevent collisions between the identifiers such as variables, classes, functions etc.
For example, two libraries lib1 and lib2, might use the same name for a function which returns version of the library say getVersion().
A call to this function will create ambiguity in an environment where both the libraries are used. This can be prevented by containing these functions in separate namespaces.
namespace lib1 // defines the function in the namespace lib1
{
string getVersion();
}
namespace lib2 // defines the function in the namespace lib2
{
string getVersion();
}
Now users can make following calls without any disambiguation.
//
// unambiguous calls by using qualified names
//
string lib1Version = lib1::getVersion(); // points to lib1
string lib2Version = lib2::getVersion(); // points to lib2
XML Namespaces
Coming back to the topic of XML Namespaces. Identifiers in XML such as elements and attributes can also face name conflicts. In the following example the first element <key> represents key-press on a keyboard. Whereas the second <key> element is a representation of key in the context of cryptography.
<key>
<name>D</name>
</key>
<key>
<private>AdfgkloPjad</private>
<public>hpkrpioHml</public>
</key>
Declaration
A namespace is declared by using the XML reserved attribute xmlns . The declaration can be done on any element of the XML document. Two syntaxes are available to define the attributes:
- xmlns:prefix=URI
- xmlns=URI
The first one associates the prefix with URI and prefix: can be prefixed to the elements or attributes in the scope of declaration of the namespace. The second method declares a default namespace and all the elements and attributes in the scope of declaration are contained in the given URI. Lets look at both the methods with examples.
xmlns:prefix=URI, Defining Prefix based Namespaces
XML attribute
xmlns:lock=”http://www.dahiya.co.in/lock-key”
defines a namespace lock and associates it with the URI=”http://www.dahiya.co.in/lock-key”. The prefix lock: can now be used in the current and its child elements.
<lock:key xmlns:lock="http://www.dahiya.co.in/lock-key">
<lock:name>D</lock:name>
</lock:key>
<crypto:key xmlns:crypto="http://www.dahiya.co.in/crypto-key">
<crypto:private>AdfgkloPjad</crypto:private>
<crypto:public>hpkrpioHml</crypto:public>
</crypto:key>
The declaration can be done in the parent elements as well because the declaration has a scope of current elements and children.
<root xmlns:lock="http://www.dahiya.co.in/lock-key" xmlns:crypto="http://www.dahiya.co.in/crypto-key">
<lock:key>
<lock:name>D</lock:name>
</lock:key>
<crypto:key>
<crypto:private>ASKDIMBJ</crypto:private>
<crypto:public>ASKDIMBJ</crypto:public>
</crypto:key>
</root>
xmlns=URI, Default Namespaces
XML attribute
xmlns=”http://www.dahiya.co.in/lock”
defines the default namespace for the elements and attributes that lack any namespace prefixes. This default namespace has a scope of current and child elements.
<key xmlns="http://www.dahiya.co.in/lock-key">
<name>D</name>
</key>
<key xmlns="http://www.dahiya.co.in/crypto-key">
<private>ASKDIMBJ</private>
<public>ASKDIMBJ</public>
</key>
Redefining Namespaces
Namespace can be redefined by using the same syntaxes with new URI.
- xmlns=New-URI
- xmlns:prefix=New-URI
Unsetting Namespaces
Unsetting of the namespaces can be done by using empty URIs.
Note that after unsetting the prefix based namespace, the prefix should not be used.
Namespace Names
The XML specifications doesn’t say that the namespace names must be URIs. But the use of URLs (type of URI) in the form of http:// is quite common. It is not required that the given URL should host anything. The whole idea is to have a unique namespace. Domain name is a unique ID, namespaces created using domain name will come out unique. In the above examples I have used my domain to create namespaces.
- http://www.dahiya.co.in/lock-key
- http://www.dahiya.co.in/crypto-key
A little off the topic but worth mentioning, Java has a convention of using reversed-domain-name to name the packages. The motive is to have a unique name for every package. This prevents packages name conflicts if everyone follows the convention.
- com.sun.team1.network
- com.sun.team2.network
- com.apple.quicktime.v2
- edu.cmu.cs.bovik.cheese
XML Namespaces in Real Use
XMPP uses XML namespaces. Following IQ stanzas use default namespace to differentiate various type of IQ stanzas.
<iq type='get' from='romeo@montague.net/orchard' to='plays.shakespeare.lit' id='info1'>
<query xmlns='http://jabber.org/protocol/disco#info'/>
</iq>
<iq from='romeo@montague.net/orchard' id='last1' to='juliet@capulet.com' type='get'>
<query xmlns='jabber:iq:last'/>
</iq>