Método XML DOM setAttributeNS()
Definición y uso
setAttributeNS()
Método para agregar un nuevo atributo (con espacio de nombres).
Si ya existe un atributo con ese nombre o espacio de nombres en el elemento, su valor se cambiará a value Parámetros
Sintaxis
elementNode.setAttributeNS(ns,name,value,
) | Descripción |
---|---|
ns | Requerido. Especifica el URI del espacio de nombres del atributo a establecer. |
name | Requerido. Especifica el nombre del atributo a establecer. |
value | Requerido. Especifica el valor del atributo a establecer. |
Ejemplo
Ejemplo 1
El siguiente código carga "books_ns.xml" en xmlDoc y agrega el atributo "edition" al primer elemento <book>:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books_ns.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("book")[0]; var ns = "https://www.codew3c.com/edition/"; x.setAttributeNS(ns, "edition", "first"); document.getElementById("demo").innerHTML = x.getAttributeNS(ns,"edition"); }
Ejemplo 2
El siguiente código carga "books_ns.xml" en xmlDoc y cambia el valor de "lang" del primer elemento <title>:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4 && xhttp.status == 200) { myFunction(xhttp); } }; xhttp.open("GET", "books_ns.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("title")[0]; var ns = "https://www.codew3c.com/edition/"; x.setAttributeNS(ns, "c:lang", "italian"); document.getElementById("demo").innerHTML = x.getAttributeNS(ns, "lang"); }