PHP | DOMElement getAttributeNodeNS() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMElement::getAttributeNodeNS() function is an inbuilt function in PHP which is used to get the attribute node in specific namespace with local name for the current node. Syntax: DOMAttr DOMElement::getAttributeNodeNS( string $namespaceURI, string $localName ) Parameters: This function accepts two parameters as mentioned above and described below: $namespaceURI: It specifies the namespace URI. $localName: It specifies the local name. Return Value: This function returns the attribute value containing the attribute node. Below given programs illustrate the DOMElement::getAttributeNodeNS() function in PHP: Program 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <body xmlns:x=\"my_namespace\"> <x:div x:attr=\"value\" > DIV 1 </x:div> </body>"); // Get the elements by tagname $elements = $dom->getElementsByTagName('div'); // Get the attribute node $node = $elements[0]->getAttributeNodeNS('my_namespace', 'attr'); // Extract name $name = $node->name; // Extract value $value = $node->value; echo $name . " => " . $value . "<br>"; ?> Output: attr => value Program 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <body xmlns:x=\"my_namespace1\"> <x:div x:id=\"my_id1\" > DIV 1 </x:div> <x:div x:id=\"my_id2\" > DIV 1 </x:div> </body> <body xmlns:xi=\"my_namespace2\"> <xi:div xi:id=\"new\" > DIV 1 </xi:div> </body> </root>"); // Get the elements by tagname $elements = $dom->getElementsByTagName('div'); foreach ($elements as $element) { $node = $element->getAttributeNodeNS('my_namespace1', 'id'); if ($node) { // Extract name $name = $node->name; // Extract value $value = $node->value; echo $name . " => " . $value . "<br>"; } } ?> Output: id => my_id1 id => my_id2 Reference: https://www.php.net/manual/en/domelement.getattributenodens.php Comment More infoAdvertise with us Next Article PHP | DOMElement getAttributeNodeNS() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement getAttributeNode() Function The DOMElement::getAttributeNode() function is an inbuilt function in PHP which is used to get the attribute node with name, for the current element. Syntax: DOMAttr DOMElement::getAttributeNode( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the at 2 min read PHP | DOMElement getAttributeNS() Function The DOMElement::getAttributeNS() function is an inbuilt function in PHP which is used to get the value of the attribute in a specific namespace with local name for the current node. Syntax: string DOMElement::getAttributeNS( string $namespaceURI, string $localName ) Parameters: This function accepts 2 min read PHP | DOMElement getAttribute() Function The DOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node. Syntax: string DOMElement::getAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribu 1 min read PHP | DOMElement hasAttributeNS() Function The DOMElement::hasAttributeNS() function is an inbuilt function in PHP which is used to know whether attribute in specific namespace named localName exists as a member of the element or not. Syntax: bool DOMElement::hasAttributeNS( string $namespaceURI, string $localName ) Parameters: This function 2 min read PHP | DOMElement setAttributeNodeNS() Function The DOMElement::setAttributeNodeNS() function is an inbuilt function in PHP which is used to add a new attribute node to element. This is just an alternative for setAttributeNode() function. Syntax: DOMAttr DOMElement::setAttributeNodeNS( DOMAttr $attr ) Parameters: This function accepts a single p 2 min read Like