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:
<table>
    <tr>
        <td>Patrick</td>
        <td>#3, park lane</td>
    </tr>    
</table>

Fragment 2:
<orders>
    <order-date>
        <td>02/02/2013</td>
        <td>01/11/2014</td>
        <td>09/01/2015</td>
    </order-date>
</orders>

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:tr>
        <xhtml:td>Patrick</xhtml:td>
        <xhtml:td>#3, park lane</xhtml:td>
        <xhtml:td>
            <o:orders xmlns:o="http://www.orderfactory.com/orders">
                <o:order-date>
                    <o:td>02/02/2013</o:td>
                    <o:td>01/11/2014</o:td>
                    <o:td>09/01/2015</o:td>
                </o:order-date>
            </o:orders>
        </xhtml:td>
    </xhtml:tr>    
</xhtml:table>

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.

localName

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

namespaceURI

Fetches namespace URI of a node.   

prefix

Fetches namespace prefix of this node.

lookupNamespaceURI(prefix)

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

lookupPrefix(namespaceURI)                      

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 ››