Does any one know what is the exact usage of xmlns
in HTML, XML files?
Edit: Why do we need this namespace? What is its usage?
The xmlns
attribute declares an XML Namespace. The Namespaces in XML standard discusses this element in depth.
Namespaces are used primarily to avoid conflicts between element names when mixing XML languages. If you have a particular application that you have questions about, perhaps you could post an example.
Namespaces let you reduce ambiguity when there are duplicates. You could have a <title>
tag that refers to authors and <title>
tag that refers to a salutation, like Mr., Mrs. etc. To differentiate, you could assign them to different namespaces.
You can also use namespaces when validating documents for conformance to a particular standard/restrictions, where the namespace would indicate to what "Schema" that the document is belonging to.
In HTML, xmlns is just a talisman to make moving from and to XHTML easier. It doesn't do anything at all.
The xmlns
attribute has special handling, that allows the declaration of a namespace.
All names, such as tag names, in a document belong to a namespace. In the absence of the xmlns
attribute all the names belong to the "no name" namespace. Hence:-
<root><item /></root>
In the above example both root
and item
are names in the "no name" namespace. Whereas:-
<root xmlns="urn:mydomain.com:mystuff"><item /></root>
Now root
and item
exist in the "urn:mydomain.com:mystuff" namespace.
The xmlns
can further define additional namespaces elements of which can be distinguished from others by using an alias prefix:-
<root xmlns="urn:mydomain.com:mystuff" xmlns:a="urn:otherdomain.com:other">
<item>
<a:supplement />
</item>
</root>
In this case root
and item
continue to be in the "urn:mydomain.com:mystuff" namespace but a:supplement
indicates that the name supplement
is in the "urn:otherdomain.com:other" namespace.
What does this acheive?
The X in XML stands for eXtensible. One goal is to allow additional information to layer onto an existing document, i.e., the ability to extend the document. Consider:-
Party A create a document:-
<root>
<item />
<root>
Party B extends the document by including additional information:-
<root>
<item />
<supplement />
</root>
Later Party A adds new info to their original form of the document and just so happen to also use the name supplement
in their original. We could end up with something like:-
<root>
<item />
<supplement />
<supplement />
</root>
Which supplement element belongs to which party? By using namespaces the document would look like this:-
<root xmlns="urn:mydomain.com:mystuff" xmlns:a="urn:otherdomain.com:other">
<item />
<supplement />
<a:supplement />
</root>
Now when it comes to parsing and querying the XML its clear which element belongs to whom. Namespaces elimnate the collision between what would otherwise be a global set of simple names.
XML namespaces help contextualize elements an attributes, among other things. It also offers a precise identification for a particular element or attribute.
For instance, the <html>
element can be defined by anyone and have any meaning. However, the <html>
element within the http://www.w3.org/1999/xhtml namespace is unique and refers to the XHTML.
Namespaces also prove useful when dealing with homographs, when using multiple XML languages in a single file.