XML DOM getAttributeNodeNS() Method

Definition and Usage

The getAttributeNS() method retrieves the attribute node by namespace URI and name.

Syntax:

elementNode.getAttributeNodeNS(ns, name)
Parameters Description
ns Required. A unique identifier for the namespace of the attribute. If there is no namespace, this parameter is null.
name Required. Declare the identifier of the attribute name in the namespace.

Description

This attribute returns an Attr node, whose descendants represent the value of the specified attribute. If the element does not have such an attribute, null is returned.

This method is similar to getAttributeNode() methodSimilar, the attribute name is specified by the namespace URI and the local name defined in this namespace. Only XML documents that use the namespace use this method.

Example

In all examples, we will use the XML file books_ns.xml, and the JavaScript function loadXMLDoc().

The following code snippet retrieves the name and value of the "lang" attribute from the first <title> element in "books_ns.xml":

xmlDoc=loadXMLDoc("books_ns.xml");
x=xmlDoc.getElementsByTagName("title")[0];
ns="http://www.codew3c.com/children/";
y=x.getAttributeNodeNS(ns,"lang");
document.write(y.nodeName);
document.write(" = ");
document.write(y.nodeValue);

The output of the above code is:

c:lang = en

Try It Yourself

Get the value of the attribute
This example uses getAttributeNodeNS() to get the name and value of the "lang" attribute in "books_ns.xml".