XML DOM setAttributeNS() विधि

विन्यास और उपयोग

setAttributeNS() नामस्पेक्ट के साथ नए अट्रिब्यूट जोड़ने के लिए विधि

यदि एलीमेंट में इस नाम या नामस्पेक्ट के साथ अट्रिब्यूट पहले से ही मौजूद है, तो उसका मान बदल जाएगा value पारामीटर

व्याकरण

elementNode.setAttributeNS(ns,name,value,
) वर्णन
ns आवश्यक। निर्धारित अट्रिब्यूट के नामस्पेक्ट यूआरआई को निर्धारित करता है。
name आवश्यक। निर्धारित अट्रिब्यूट का नाम निर्धारित करता है。
value आवश्यक। निर्धारित अट्रिब्यूट के मान को निर्धारित करता है。

उदाहरण

उदाहरण 1

इस कोड के द्वारा "books_ns.xml" को xmlDoc में लोड किया जाएगा और पहले <book> एलीमेंट में "edition" अट्रिब्यूट जोड़ा जाएगा:

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");
}

स्वयं प्रयोग कीजिए

उदाहरण 2

इस कोड के द्वारा "books_ns.xml" को xmlDoc में लोड किया जाएगा और पहले <title> एलीमेंट का "lang" मान बदला जाएगा:

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");
}

स्वयं प्रयोग कीजिए