XML DOM getAttributeNS() method

Definition and usage

The getAttributeNS() method retrieves the attribute value through the namespace URI and name.

Syntax:

elementNode.getAttributeNS(ns,name)
Parameters Description
ns Required. Specifies the namespace URI from which the attribute value is obtained.
name Required. Specifies the attribute from which the attribute value is obtained.

Example

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

The following code snippet retrieves the 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/";
document.write(x.getAttributeNS(ns,"lang");

The output of the above code is:

en