XML DOM hasAttributeNS() メソッド

定義と使用方法

現在の要素ノードが指定された命名空間と名前を持つ属性を持っている場合、 hasAttributeNS() メソッドはtrueを返し、それ以外の場合はfalseを返します。

構文

hasAttributeNS(ns,name)
引数 説明
ns 必須。検索する属性の命名空間を指定します。
name 必須。検索する属性の名前を指定します。

以下のコードは、"books_ns.xml"をxmlDocにロードし、最初の<title>要素が指定された命名空間と名前を持つ属性を持っているかを確認します:

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.hasAttributeNS(ns,"lang");
{}

実際に試してみる