Met Element Setattributens
# XML DOM: setAttributeNS() Method
The `setAttributeNS()` method adds a new attribute with a specified namespace to an element. If an attribute with the same local name and namespace URI already exists on the element, its value is updated to the new value provided.
This method is essential when working with XML documents that utilize multiple namespaces (such as SVG, MathML, or custom XML schemas) to prevent naming conflicts.
---
## Syntax
```javascript
elementNode.setAttributeNS(namespaceURI, qualifiedName, value);
```
### Parameter Values
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `namespaceURI` | String | **Required.** The namespace URI of the attribute to create or alter. |
| `qualifiedName` | String | **Required.** The qualified name of the attribute to set (can include a namespace prefix, e.g., `prefix:name`). |
| `value` | String | **Required.** The value to assign to the attribute. |
---
## Code Examples
### Example 1: Adding a New Namespace Attribute
The following code loads an XML file (`books_ns.xml`) and adds a new attribute named `edition` with a specific namespace to the first `` element.
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books_ns.xml");
// Select the first element
var bookElement = xmlDoc.getElementsByTagName("book");
var nsURI = "http://www.youtip.co/schema/books/";
// Set the new attribute with a namespace
bookElement.setAttributeNS(nsURI, "edition", "first");
// Retrieve and display the newly set attribute
document.write(bookElement.getAttributeNS(nsURI, "edition"));
```
**Output:**
```text
first
```
---
### Example 2: Modifying an Existing Namespace Attribute
The following code loads `books_ns.xml` and updates the value of an existing namespaced attribute (`c:lang`) on the first `` element.
```javascript
// Load the XML document
var xmlDoc = loadXMLDoc("books_ns.xml");
// Select the first element
var titleElement = xmlDoc.getElementsByTagName("title");
var nsURI = "http://www.youtip.co/schema/books/";
// Update the attribute value using its prefix and local name
titleElement.setAttributeNS(nsURI, "c:lang", "italian");
// Retrieve and display the updated attribute value
document.write(titleElement.getAttributeNS(nsURI, "lang"));
```
**Output:**
```text
italian
```
---
## Important Considerations
1. **Namespace URI vs. Prefix**: The `namespaceURI` is the unique identifier (usually a URL) that defines the namespace, whereas the prefix (like `c:` in `c:lang`) is just a shorthand alias. When retrieving the attribute later using `getAttributeNS()`, you must use the `namespaceURI` and the local name (without the prefix).
2. **HTML5 vs. XML**: In standard HTML5 documents, namespaces are rarely needed unless you are embedding inline SVG or MathML. `setAttributeNS()` is primarily used in XML DOM manipulation.
3. **Null Namespaces**: If you want to set an attribute without a namespace using this method, you should pass `null` as the first argument. However, for standard non-namespaced attributes, it is recommended to use the simpler `setAttribute()` method instead.
YouTip