XML DOM setAttributeNS() 메서드

정의와 사용법

setAttributeNS() 이름 공간을 가진 새로운 속성 추가 메서드.

요소에 해당 이름이나 이름 공간을 가진 속성이 이미 존재하면 그 값이 변경됩니다. value 파라미터.

문법

elementNode.setAttributeNS(ns,name,value,
) 설명
ns 필수. 설정할 속성의 이름 공간 URI를 지정합니다.
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");
}

직접 테스트해 보세요