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. The URI of the namespace that uniquely identifies 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, it returns null.

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

Exemplo

Em todos os exemplos, usaremos o arquivo XML books_ns.xml, e a função JavaScript loadXMLDoc().

O código a seguir obtém o nome e o valor do atributo "lang" do primeiro elemento <title> no "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);

A saída do código acima é:

c:lang = en

TIY

Obter o valor do atributo
Este exemplo usa getAttributeNodeNS() para obter o nome e o valor do atributo "lang" no "books_ns.xml".