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 this namespace. Only XML documents using namespaces use this method.

Ejemplo

En todos los ejemplos, utilizaremos archivos XML books_ns.xml, y las funciones de JavaScript loadXMLDoc().

El siguiente fragmento de código extrae el nombre y el valor del atributo "lang" del primer elemento <title> en "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);

La salida del código anterior es:

c:lang = en

TIY

Obtener el valor del atributo
Este ejemplo utiliza getAttributeNodeNS() para obtener el nombre y el valor del atributo "lang" en "books_ns.xml".