Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the exact usage of xmlns in xml, and html

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?

like image 255
coderex Avatar asked Sep 03 '09 08:09

coderex


2 Answers

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.

like image 125
AnthonyWJones Avatar answered Oct 13 '22 00:10

AnthonyWJones


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.

like image 32
Greg Hewgill Avatar answered Oct 12 '22 23:10

Greg Hewgill