PHP | DOMElement getElementsByTagNameNS() Function Last Updated : 05 Mar, 2020 Comments Improve Suggest changes Like Article Like Report The DOMElement::getElementsByTagNameNS() function is an inbuilt function in PHP which is used to get all the descendant elements with a given localName and namespaceURI. Syntax: DOMNodeList DOMElement::getElementsByTagNameNS( string $namespaceURI, string $localName ) Parameters: This function accept 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 a DOMNodeList value containing all matched elements in the order in which they are encountered in a preorder traversal of this element tree. Below given programs illustrate the DOMElement::getElementsByTagNameNS() function in PHP: Program 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 y:style=\"color:red;\"> Hello, this is my new red heading. </y:h1> <y:h1 y:style=\"color:green;\"> Hello, this is my new green heading. </y:h1> <y:h1 y:style=\"color:blue;\"> Hello, this is my new blue heading. </y:h1> </div> </root>"); // Get the elements with h1 tag from // specific namespace $nodeList = $dom->getElementsByTagNameNS( 'my_namespace', 'h1'); foreach ($nodeList as $node) { echo $node->getAttribute('x:style') . '<br>'; } ?> Output: color:red;color:green;color:blue; Program 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:p>HELLO.</x:p> <x:p>NEW.</x:p> <x:p>WORLD.</x:p> </div> <div xmlns:y=\"g4g_namespace\"> <y:p>GEEKS</y:p> <y:p>FOR</y:p> <y:p>GEEKS</y:p> </div> </root>"); // Get the elements $nodeList = $dom->getElementsByTagNameNS( 'g4g_namespace', 'p'); foreach ($nodeList as $node) { echo $node->textContent . '<br>'; } ?> Output: GEEKSFORGEEKS Reference: https://www.php.net/manual/en/domelement.getelementsbytagnamens.php Comment More infoAdvertise with us Next Article PHP | DOMElement getElementsByTagNameNS() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement getElementsByTagName() Function The DOMElement::getElementsByTagName() function is an inbuilt function in PHP which is used to get the elements by tagname. Syntax:Â DOMNodeList DOMElement::getElementsByTagName( string $name ) Parameters: This function accepts a single parameter $name which holds the tag name or use * for getting a 2 min read PHP | DOMDocument getElementsByTagnameNS() Function The DOMDocument::getElementsByTagNameNS() function is an inbuilt function in PHP which is used to search for all elements with given tag name in specified namespace. Syntax: DOMNodeList DOMDocument::getElementsByTagNameNS( string $namespaceURI, string $localName ) Parameters: This function accepts t 2 min read PHP | DOMDocument getElementsByTagName() Function The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name. Syntax: DOMNodeList DOMDocument::getElementsByTagName( string $name ) Parameters: This function accepts single 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 | DOMDocument getElementById() Function The DOMDocument::getElementById() function is an inbuilt function in PHP which is used to search for an element with a certain id. Syntax: DOMElement DOMDocument::getElementById( string $elementId ) Parameters:This function accepts a single parameter $elementId which holds the id to search for. Retu 2 min read Like