XML DOM getAttributeNS() メソッド

定義と使用法

getAttributeNS() メソッドは、命名空間URIと名前を指定して属性値を取得します。

構文

elementNode.getAttributeNS(ns,name)
引数 説明
ns 必須。属性値を取得する命名空間URIを指定します。
name 必須。属性値を取得する属性を指定します。

以下のコードは、"books_ns.xml"をxmlDocにロードし、最初の<title>要素から"lang"属性値を取得します:

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("title")[0];
    var ns = "https://www.codew3c.com/meishi/";
    document.getElementById("demo").innerHTML =
    x.getAttributeNS(ns, "lang");
}

自分で試してみる