Tuesday, June 16, 2026
HomeLanguagesPHP | DOMElement removeAttributeNS() Function

PHP | DOMElement removeAttributeNS() Function

The DOMElement::removeAttributeNS() function is an inbuilt function in PHP which is used to remove the attribute with a specific local name in a specific namespace from the element.

Syntax:

bool DOMElement::removeAttributeNS( string $namespaceURI, string $localName )

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

  • $namespaceURI: It specifies the namespace URI.
  • $localName: It specifies the local name.

Return Value: This function returns TRUE on success or FALSE on failure.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR when the node is read-only.

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

Example 1:




<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html xmlns:x=\"my_namespace\">
        <x:h1 x:id=\"my_id\"> Geeksforneveropen </x:h1>
        <x:h2> Second heading </x:h2>
    </html>
    <html xmlns:y=\"another_namespace\">
        <y:h1 id=\"my_new_id\"> Random text </y:h1>
        <y:h2> Another heading </y:h2>
    </html>
</root>");
  
// Get the elements
$nodes = $dom->getElementsByTagName('h1');
  
echo "Before the removal of attributes: <br>";
  
foreach ($nodes as $node) {
  
    // Get the attribute name and value
    $attribute = $node->attributes->item(0);
    $attribute_name = $attribute->name;
    $attribute_value = $attribute->value;
  
    echo $attribute_name . ' => ';
    echo $attribute_value . '<br>';
  
    // Remove the id attribute
    $node->removeAttributeNS('my_namespace', 'id');
}
  
echo "<br>After the removal of attributes: <br>";
  
foreach ($nodes as $node) {
    // Get the attribute name and value
    $attribute = $node->attributes->item(0);
    $attribute_name = $attribute->name;
    $attribute_value = $attribute->value;
    echo $attribute_name . ' => ';
    echo $attribute_value . '<br>';
}
?>


Output:

Before the removal of attributes:
id => my_id
id => my_new_id

After the removal of attributes:
=>  // Empty string means attribute is removed
id => my_new_id 

Example 2:




<?php
   
// Create a new DOMDocument
$dom = new DOMDocument();
   
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html xmlns:x=\"my_namespace\">
        <x:h1 x:id=\"my_id\"> 
        Geeksforneveropen 
        </x:h1>
        <x:h2> Second heading </x:h2>
    </html>
    <html xmlns:y=\"another_namespace\">
        <y:h1 y:id=\"my_new_id\" 
         y:style=\"color: blue;\">
         Random text 
         </y:h1>
        <y:h2> Another heading </y:h2>
    </html>
</root>");
   
// Get the elements
$node = $dom->getElementsByTagName('h1')[1];
    
echo "Before the removal of attributes: <br>";
   
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
    
// Remove the id attribute
$node->removeAttributeNS('another_namespace', 'id');
    
echo "<br>After the removal of attributes: <br>";
    
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
?>


Output:

Before the removal of attributes:
No of attributes => 2
After the removal of attributes:
No of attributes => 1

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

RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS