DOMElement::setIdAttributeNS()函數是PHP中的內置函數,用於將給定本地名稱和名稱空間URI指定的屬性聲明為ID類型。
用法:
void DOMElement::setIdAttributeNS( string $namespaceURI, string $localName, bool $isId )
參數:此函數接受上述和以下所述的三個參數:
- $namespaceURI:它指定名稱空間URI。
 - $localName:它指定本地名稱。
 - $isId:它指定是否要讓名稱為ID類型。
 
返回值:此函數不返回任何內容。
異常:如果該節點是隻讀節點,則此函數將拋出DOM_NO_MODIFICATION_ALLOWED_ERR;如果name不是該元素的屬性,則此函數將拋出DOM_NOT_FOUND。
以下示例說明了PHP中的DOMElement::setIdAttributeNS()函數:
範例1:
<?php 
  
// Create a new DOMDocument 
$dom = new DOMDocument(); 
  
// Enable validate on parse 
$dom->validateOnParse = true; 
    
// Create an element 
$element = $dom->createElementNS("my_namespace", "x:p",  
                         'Hello, this is my paragraph.'); 
    
// Add the node to the dom 
$newnode = $dom->appendChild($element); 
    
// Set the attribute 
$newnode->setAttributeNS("my_namespace", "id", "my_value"); 
  
// Set that attribute as id 
$element->setIDAttributeNS("my_namespace", 'id', true); 
    
echo $dom->saveXML(); 
?>輸出:您可以按Ctrl + U查看DOM。

範例2:
<?php 
// Create a new DOMDocument 
$dom = new DOMDocument(); 
  
// Enable validate on parse 
$dom->validateOnParse = true; 
    
// Create an element 
$element = $dom->createElementNS("my_namespace", "x:p",  
                                       'GeeksforGeeks'); 
    
// Add the node to the dom 
$newnode = $dom->appendChild($element); 
    
// Set the attribute 
$newnode->setAttributeNS("my_namespace", "id", 
                                       "geeksforgeeks"); 
  
// Set that attribute as id 
$element->setIDAttributeNS("my_namespace", 'id', true); 
  
// Get the text of element with id='geeksforgeeks' 
// just to see if it works 
$value = $dom->getElementById('geeksforgeeks')->textContent; 
echo $value; 
?>輸出:
GeeksforGeeks
參考: https://www.php.net/manual/en/domelement.setidattributens.php
相關用法
- PHP DOMElement removeAttribute()用法及代碼示例
 - PHP DOMElement __construct()用法及代碼示例
 - PHP DOMElement getAttributeNodeNS()用法及代碼示例
 - PHP DOMElement getAttributeNS()用法及代碼示例
 - PHP DOMElement getElementsByTagName()用法及代碼示例
 - PHP DOMElement getAttributeNode()用法及代碼示例
 - PHP DOMElement removeAttributeNS()用法及代碼示例
 - PHP DOMElement getAttribute()用法及代碼示例
 - PHP DOMElement hasAttributeNS()用法及代碼示例
 - PHP cos( )用法及代碼示例
 - PHP Ds\Map get()用法及代碼示例
 - PHP pos()用法及代碼示例
 
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMElement setIdAttributeNS() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
