XML DOM setAttributeNS()
Method
Definition and Usage
The setAttributeNS()
method adds a new attribute (with a namespace).
If an attribute with the specified name or namespace already exists on the element, its value is changed to the value of the prefix and value parameters.
Syntax
Parameter | Description |
---|---|
ns | Required. Specifies the namespace URI of the attribute to be set. |
name | Required. Specifies the name of the attribute to be set. |
value | Required. Specifies the value of the attribute to be set. |
Example 1
The following code snippet uses loadXMLDoc()
to load "books_ns.xml" into xmlDoc
and adds an "edition" attribute to the first <book>
element:
xmlDoc = loadXMLDoc("books_ns.xml");
x = xmlDoc.getElementsByTagName("book")[0];
ns = "http://www.tutorialpro.org/w3cnote/";
x.setAttributeNS(ns, "edition", "first");
document.write(x.getAttributeNS(ns, "edition"));
Output:
first
Example 2
The following code snippet uses loadXMLDoc()
to load "books_ns.xml" into xmlDoc
and changes the "lang" value of the first <title>
element:
xmlDoc = loadXMLDoc("books_ns.xml");
x = xmlDoc.getElementsByTagName("title")[0];
ns = "http://www.tutorialpro.org/w3cnote/";
x.setAttributeNS(ns, "c:lang", "italian");
document.write(x.getAttributeNS(ns, "lang"));
Output:
italian