PHP | DOMElement removeAttributeNS()函数
DOMElement::removeAttributeNS()函数是PHP中的一个内置函数,用于从元素中删除特定命名空间中具有特定本地名称的属性。
句法:
bool DOMElement::removeAttributeNS( string $namespaceURI, string $localName )
参数:该函数接受上面提到的两个参数,如下所述:
- $namespaceURI:它指定命名空间 URI。
- $localName:它指定本地名称。
返回值:此函数在成功时返回 TRUE,在失败时返回 FALSE。
例外:当节点为只读时,此函数会抛出 DOM_NO_MODIFICATION_ALLOWED_ERR。
下面的示例说明了PHP中的DOMElement::removeAttributeNS()函数:
示例 1:
loadXML("
Geeksforgeeks
Second heading
Random text
Another heading
");
// Get the elements
$nodes = $dom->getElementsByTagName('h1');
echo "Before the removal of attributes:
";
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 . '
';
// Remove the id attribute
$node->removeAttributeNS('my_namespace', 'id');
}
echo "
After the removal of attributes:
";
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 . '
';
}
?>
输出:
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
示例 2:
loadXML("
Geeksforgeeks
Second heading
Random text
Another heading
");
// Get the elements
$node = $dom->getElementsByTagName('h1')[1];
echo "Before the removal of attributes:
";
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
// Remove the id attribute
$node->removeAttributeNS('another_namespace', 'id');
echo "
After the removal of attributes:
";
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
?>
输出:
Before the removal of attributes:
No of attributes => 2
After the removal of attributes:
No of attributes => 1
参考: https://www. PHP.net/manual/en/domelement.removeattributens。 PHP