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