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 // Store the XML document to the source $source = <<<XML <?xml version="1.0" encoding="UTF-8"?> <root><contact><email>abc@geeksforgeeks.org</email> <mobile>+91-987654321</mobile></contact></root> XML; // Create a new document $domDocument = new DOMDocument( '1.0' ); // Load the XML file $domDocument->loadXML( $source ); // Use createAttributeNS() function to create new attribute // node with an associated namespace $attrNS = $domDocument->createAttributeNS( '{namespace}', 'info:cont_info' ); // Create XML document and display it echo $domDocument->saveXML() . "\n"; // Assign value to the createAttributeNS $domDocument->getElementsByTagName( 'contact' ) ->item(0)->appendChild( $attrNS ); // Create XML document and display it print $domDocument->saveXML() . "\n"; ?> |
<?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
Recommended Posts:
- PHP | DOMDocument xinclude() Function
- PHP | DOMDocument createCDATASection() Function
- PHP | DOMDocument createComment() Function
- PHP | DOMDocument createElement() Function
- PHP | DOMDocument createAttribute() Function
- PHP | DOMDocument __construct() Function
- PHP | DOMDocument getElementsByTagName() Function
- PHP | DOMDocument importNode() Function
- PHP | DOMDocument saveXML() Function
- PHP | DOMDocument createTextNode() Function
- PHP | DOMDocument createProcessingInstruction() Function
- PHP | DOMDocument createEntityReference() Function
- PHP | DOMDocument saveHTMLFile() Function
- PHP | DOMDocument saveHTML() Function
- PHP | DOMDocument save() Function
- PHP | DOMDocument loadXML() Function
- PHP | DOMDocument normalizeDocument() Function
- PHP | DOMDocument loadHTMLFile() Function
- PHP | DOMDocument loadHTML() Function
- PHP | DOMDocument load() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

