XML Namespace

Namespace is set of unique names. It is used to avoid element name conflict in XML document. A name space is identified by URI (Uniform Resource Identifiers).

Namespace Declaration

Namespace is declared using reserved attributes. Attribute name must be started with xmlns:
<element xmlns:name="URL">
Syntax
  • Namespace must be starts with the keyword xmlns.
  • The word name is the Namespace prefix.
  • The URL in the Namespace identifier.
Example
<?xml version="1.0"?>  
<cont:contact xmlns:cont="www.tutorialandexample.com/javascript-tutorial">  
    <cont:name>Ajay singh</cont:name>  
    <cont:company> SSSIT.Pvt.Ltd</cont:company>  
    <cont:phone>0120- 4256464, 9990449935</cont:phone>  
</cont:contact>
In above Example prefix is cont, and Namespace identifier as www.tutorialandexample.com/javascript-tutorial

Local Namespace

When we define the namespace, we defined it against the root element. It means that the namespace was to be used for the whole document, when we prefixed all child elements with the same namespace.

Example of Local Namespace

<tutorials>  
    <tutorial>  
        <tt: title xmlns:tt="https://www.tutorialandexample.com/javascript-tutorial">  
            The Dream saga  
        </tt : title>  
        <owner>Sonoo Jaiswal</owner>  
    </tutorial>  
    ............   
</tutorials>

Default Namespace

A default namespace is one where prefix is not apply. We can also define namespace against child node. We use multiple namespaces within the same document if required.

Example of Default Namespace

<tutorials xmlns="https://www.tutorialandexample.com/javascript-tutorial">  
    <tutorial>  
        <title>JavaScript</title>  
        <owner>Sonoo jaiswal</owner>          
    <tutorial>  
    ............  
</tutorials>