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, string $value )

Parameters: This function accepts three parameters as mentioned above and described below:



  • $namespaceURI: It specifies the namespace URI.
  • $qualifiedName: It specifies the name of attribute.
  • $value: It specifies the value of attribute.

Return Value: This function returns nothing.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly or DOM_NAMESPACE_ERR, if $qualifiedName is a malformed qualified name, or if $qualifiedName has a prefix and namespaceURI is NULL.

Below examples illustrate the DOMElement::setAttributeNS() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
   
// Create an element
$node = $dom->createElementNS("my_namespace", "x:p"
                     'Hello, this is my paragraph.');
   
// Add the node to the dom
$newnode = $dom->appendChild($node);
   
// Set the attribute
$newnode->setAttributeNS("my_namespace", "id", "my_value");
  
echo $dom->saveXML();
?>

chevron_right


Output: You can press Ctrl + U to see the DOM.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
   
// Create an element
$node = $dom->createElementNS("my_namespace", "x:p",
                     'Hello, this is my paragraph.');
   
// Add the node to the dom
$newnode = $dom->appendChild($node);
  
echo "Before the addition of attributes: <br>";
   
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
   
// Set the attribute with a namespace
$newnode->setAttributeNS("my_namespace", "style", "color:blue");
  
echo "<br>After the addition of attributes: <br>";
    
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
?>

chevron_right


Output:

Before the addition of attributes:
No of attributes => 0
After the addition of attributes:
No of attributes => 1

Reference: https://www.php.net/manual/en/domelement.setattributens.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.