XML Namespaces provide a way to avoid name space conflicts within DOM elements. This way elements from different XML based languages can be mixed together to form a single document without the fear of element name space clashes. Namespaces are specified using the xmlns attribute

Example of XML namespaces and xmls attribute

Consider the following two fragments, the first one shows a regular html table and the second one shows user defined XML. Combining these two XML documents will cause a namespace conflicts with <td> element. A DOM parser will not know how to handle these differences.

Fragment 1:
        <td>#3, park lane</td>

Fragment 2:

By using the XML Namespaces with xmlns attribute in the start tag of an element this conflict issue between two different XML's may be solved. The illustration of using XML namespace attribute with the combined XML is given below. Here elements are segregated by two separate namespaces one from XHTML and one from user defined 'orders'.

<xhtml:table xmlns:xhtml=”http://www.w3.org/1999/xhtml">
        <xhtml:td>#3, park lane</xhtml:td>
            <o:orders xmlns:o="http://www.orderfactory.com/orders">

Changes to Interface Element

The interface Element represents an element in an HTML or XML document. Elements may have attributes associated with them, as we are aware, Dom Level 1 provided methods like getAttribute, setAttribute, removeAttribute, etc to fetch, set and remove attributes of an element. With the introduction of XML Namespaces in DOM Level 2 and 3 specifications, few more convenience methods are added which are namespace aware.

getAttributeNS(namespaceURI, qualifiedName)

Retrieves an attribute value by local name and namespace URI.           

setAttributeNS(namespaceURI, qualifiedName, value)

Adds a new attribute in the given namespace URI, local name with the value specified.         

removeAttributeNS(namespaceURI, localName)

Removes an attribute by local name and namespace URI.           

getAttributeNodeNS(namespaceURI, localName)   

Fetches an Attribute node by local name and namespace URI.           

getElementsByTagNameNS(namespaceURI, localName)

Returns a NodeList of all the descendant Elements with a given local name and namespace URI in document order.  

hasAttributeNS(namespaceURI, localName)

Verifies if a given attribute with a local name and namespace URI is specified on the element.

Changes to Interface Document

The interface Document represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data. XML Namespaces aware methods to create element and fetch elements are provided in this interface.

createElementNS(namespaceURI, qualifiedName)

Creates an element of the given qualified name and namepspace URI.

createAttributeNS(namespaceURI, qualifiedName)

Creates an attribute of the given qualified name and namespace URI.

getElementsByTagNameNS(namespaceURI, localName)

Returns a NodeList of all the Elements with a given local name and namespace URI in document order.

Changes to Interface Node

The interface Node is the primary datatype for the entire Document Object Model. It represents a single node in the document tree.

Namespace specific attributes added in DOM Level 2 specs are as follows.


Fetches the local part of the qualified name of this node.   


Fetches namespace URI of a node.   


Fetches namespace prefix of this node.


Fetches the namespace URI associated to the given prefix, starting from this node.


Fetches the prefix associated to the given namespace URI, starting from this node.

isDefaultNamespace (namespaceURI)

This method checks if the specified namespace URI is the default namespace or not.


›› go to examples ››