PHP | DOMDocument createAttributeNS() Function
Last Updated :
29 Aug, 2019
The DOMDocument::createAttributeNS() function is an inbuilt function in PHP which is used to create a new attribute node with an associated namespace.
Syntax:
DOMAttr DOMDocument::createAttributeNS( string $namespaceURI, string $qualifiedName )
Parameters: This function accepts two parameters as mentioned above and described below:
- $namespaceURI: This parameter holds the URI of the namespace.
- $qualifiedName This parameter holds the tag name and prefix of the attribute. For example: prefix:tagname.
Return Value: This function returns a new DOMAttr object on success or FALSE on failure.
Below program illustrates the DOMDocument::createAttributeNS() function in PHP:
Program:
<?php
$source = <<<XML
<?xml version= "1.0" encoding= "UTF-8" ?>
<root><contact><email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile></contact></root>
XML;
$domDocument = new DOMDocument( '1.0' );
$domDocument ->loadXML( $source );
$attrNS = $domDocument ->createAttributeNS(
'{namespace}' , 'info:cont_info' );
echo $domDocument ->saveXML() . "\n" ;
$domDocument ->getElementsByTagName( 'contact' )
->item(0)->appendChild( $attrNS );
print $domDocument ->saveXML() . "\n" ;
?>
|
Output:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:info="{namespace}">
<contact>
<email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile>
</contact>
</root>
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:info="{namespace}">
<contact info:cont_info=
"https://www.geeksforgeeks.org/about/contact-us/">
<email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile>
</contact>
</root>
Reference: https://www.php.net/manual/en/domdocument.createattributens.php
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...