PHP | DOMElement hasAttributeNS() Function Last Updated : 26 Feb, 2020 Comments Improve Suggest changes Like Article Like Report 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 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 TRUE on success or FALSE on failure. Below examples illustrate the DOMElement::hasAttributeNS() function in PHP: Example 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <div xmlns:x=\"my_namespace\"> <x:h1 x:style=\"color:red;\"> Hello, this is my red heading. </x:h1> <x:h1 x:style=\"color:green;\"> Hello, this is my green heading. </x:h1> <x:h1 x:style=\"color:blue;\"> Hello, this is my blue heading. </x:h1> </div> <div xmlns:y=\"another_namespace\"> <y:h1> Hello, this is my new red heading. </y:h1> <y:h1> Hello, this is my new green heading. </y:h1> <y:h1> Hello, this is my new blue heading. </y:h1> </div> </root>"); // Get the elements $nodeList = $dom->getElementsByTagName('h1'); $i = 0; foreach ($nodeList as $node) { if($node->hasAttributeNS('my_namespace', 'style')) { $i++; } } echo "Yes, there are total of $i style attributes inside my_namespace"; ?> Output: Yes, there are total of 3 style attributes inside my_namespace. Example 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <div xmlns:x=\"my_namespace\"> <x:h1 x:id=\"color:red;\"> 1 </x:h1> <x:h1 x:id=\"color:green;\"> 2 </x:h1> <x:h1 x:id=\"color:blue;\"> 3 </x:h1> </div> <div xmlns:y=\"another_namespace\"> <y:h1> 1 </y:h1> <y:h1> 2 </y:h1> <y:h1> 3 </y:h1> </div> </root>"); // Get the elements $nodeList = $dom->getElementsByTagName('h1'); $i = 0; foreach ($nodeList as $node) { if($node->hasAttributeNS('another_namespace', 'id')) { $i++; } } echo "There are total of $i id attributes inside another_namespace."; ?> Output: There are total of 0 id attributes inside another_namespace. Reference: https://www.php.net/manual/en/domelement.hasattributens.php Comment More infoAdvertise with us Next Article PHP | DOMElement hasAttributeNS() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement hasAttribute() Function The DOMElement::hasAttribute() function is an inbuilt function in PHP which is used to know whether attribute with a specific name exists as a member of the element. Syntax: bool DOMElement::hasAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name 1 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 getAttributeNodeNS() Function 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 2 min read 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 Like