PHP | DOMElement setAttributeNodeNS() Function Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report The DOMElement::setAttributeNodeNS() function is an inbuilt function in PHP which is used to add a new attribute node to element. This is just an alternative for setAttributeNode() function. Syntax: DOMAttr DOMElement::setAttributeNodeNS( DOMAttr $attr ) Parameters: This function accepts a single parameter $attr which holds the attribute to be added. Return Value: This function returns the old node if the attribute has been replaced. Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly. Below examples illustrate the DOMElement::setAttributeNodeNS() function in PHP: Example 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Create a element with a namespace $root = $dom->createElementNS("my_namepsace", "root"); // Create an element $node = $dom->createElement("div", "GeeksforGeeks"); // Append the child $dom->appendChild($root); // Add the node to the dom $newnode = $dom->appendChild($node); // Create a DOMAttr instance $attr = new DOMAttr('style', 'color: green; font-size: 100px;'); // Set the attribute $newnode->setAttributeNodeNS($attr); echo $dom->saveXML(); ?> Output: Example 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <html> <h1 id=\"my_id\"> Geeksforgeeks </h1> <h2> Second heading </h2> </html> </root>"); // Get the elements $node = $dom->getElementsByTagName('h1')[0]; echo "Before the addition of attributes: <br>"; // Get the attribute count $attributeCount = $node->attributes->count(); echo 'No of attributes => ' . $attributeCount; // Create a DOMAttr instance $attr = new DOMAttr('class', 'value'); // Add the new attribute $node->setAttributeNodeNS($attr); echo "<br>After the addition of attributes: <br>"; // Get the attribute count $attributeCount = $node->attributes->count(); echo 'No of attributes => ' . $attributeCount; ?> Output: Before the addition of attributes: No of attributes => 1 After the addition of attributes: No of attributes => 2 Reference: https://www.php.net/manual/en/domelement.setattributenodens.php Comment More infoAdvertise with us Next Article PHP | DOMElement setAttributeNodeNS() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement setAttributeNode() Function The DOMElement::setAttributeNode() function is an inbuilt function in PHP which is used to add a new attribute node to element. Syntax: DOMAttr DOMElement::setAttributeNode( DOMAttr $attr ) Parameters: This function accepts a single parameter $attr which holds the attribute node to be added. Return 2 min read PHP | DOMElement setAttributeNS() Function The DOMElement::setAttributeNS() function is an inbuilt function in PHP which is used to set an attribute with given namespace and name to the given value. If the attribute does not exist, it will be created. Syntax: void DOMElement::setAttributeNS( string $namespaceURI, string $qualifiedName, strin 2 min read PHP | DOMElement setIdAttributeNode() Function The DOMElement::setIdAttributeNode() function is an inbuilt function in PHP which is used to declare the attribute specified by DOMAttr instance to be of type ID. Syntax: void DOMElement::setIdAttributeNode( DOMAttr $attr, bool $isId ) Parameters: This function accepts two parameters as mentioned ab 2 min read PHP | DOMElement setIdAttributeNS() Function The DOMElement::setIdAttributeNS() function is an inbuilt function in PHP which is used to declare the attribute specified by given local name and namespace URI to be of type ID. Syntax: void DOMElement::setIdAttributeNS( string $namespaceURI, string $localName, bool $isId ) Parameters: This functio 2 min read PHP | DOMElement setAttribute() Function The DOMElement::setAttribute() function is an inbuilt function in PHP which is used to set an attribute with given name to the given value. If the attribute does not exist, it will be created. Syntax: DOMAttr DOMElement::setAttribute( string $name, string $value ) Parameters: This function accepts t 2 min read Like