PHP | DOMElement setIdAttributeNS() Function
Last Updated :
26 Feb, 2020
The DOMElement::setIdAttributeNS() function is an inbuilt function in PHP which is used to declare the attribute specified by given local name and namespace URI to be of type ID.
Syntax:
void DOMElement::setIdAttributeNS( string $namespaceURI,
string $localName, bool $isId )
Parameters: This function accept three parameters as mentioned above and described below:
- $namespaceURI: It specifies the namespace URI.
- $localName: It specifies the local name.
- $isId: It specifies whether if you want name to be of type ID.
Return Value: This function returns nothing.
Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly or DOM_NOT_FOUND, if name is not an attribute of this element.
Below examples illustrate the DOMElement::setIdAttributeNS() function in PHP:
Example 1:
<?php
$dom = new DOMDocument();
$dom ->validateOnParse = true;
$element = $dom ->createElementNS( "my_namespace" , "x:p" ,
'Hello, this is my paragraph.' );
$newnode = $dom ->appendChild( $element );
$newnode ->setAttributeNS( "my_namespace" , "id" , "my_value" );
$element ->setIDAttributeNS( "my_namespace" , 'id' , true);
echo $dom ->saveXML();
?>
|
Output: You can press Ctrl+U to see the DOM.
Example 2:
<?php
$dom = new DOMDocument();
$dom ->validateOnParse = true;
$element = $dom ->createElementNS( "my_namespace" , "x:p" ,
'GeeksforGeeks' );
$newnode = $dom ->appendChild( $element );
$newnode ->setAttributeNS( "my_namespace" , "id" ,
"geeksforgeeks" );
$element ->setIDAttributeNS( "my_namespace" , 'id' , true);
$value = $dom ->getElementById( 'geeksforgeeks' )->textContent;
echo $value ;
?>
|
Output:
GeeksforGeeks
Reference: https://www.php.net/manual/en/domelement.setidattributens.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...