DOMElement::removeAttributeNS()函數是PHP中的內置函數,用於從元素中刪除特定命名空間中具有特定本地名稱的屬性。
用法:
bool DOMElement::removeAttributeNS( string $namespaceURI, string $localName )
參數:該函數接受上述和以下描述的兩個參數:
- $namespaceURI:它指定名稱空間URI。
- $localName:它指定本地名稱。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
異常:當節點為隻讀時,此函數將拋出DOM_NO_MODIFICATION_ALLOWED_ERR。
以下示例說明了PHP中的DOMElement::removeAttributeNS()函數:
範例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\"> Geeksforgeeks </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>';
}
?>
輸出:
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:
<?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\">
Geeksforgeeks
</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;
?>
輸出:
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
相關用法
- PHP DOMElement getAttributeNodeNS()用法及代碼示例
- PHP DOMElement getAttributeNS()用法及代碼示例
- PHP DOMElement getAttributeNode()用法及代碼示例
- PHP DOMElement hasAttributeNS()用法及代碼示例
- PHP DOMElement getAttribute()用法及代碼示例
- PHP DOMElement __construct()用法及代碼示例
- PHP DOMElement getElementsByTagName()用法及代碼示例
- p5.js value()用法及代碼示例
- PHP Ds\Map get()用法及代碼示例
- PHP cos( )用法及代碼示例
- p5.js sq()用法及代碼示例
- CSS var()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMElement removeAttributeNS() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。