PHP | DOMDocument getElementsByTagnameNS() Function Last Updated : 07 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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 two parameters as mentioned above and described below: $namespaceURI: It specifies the namespace URI of the elements to match on and * matches all namespaces.$localName : It specifies the local name of the elements to match on and * matches all local names. Return Value: This function returns a DOMNodeList of all elements with a given local name and a namespace URI. Below given programs illustrate the DOMDocument::getElementsByTagNameNS() function in PHP: Program 1: In this example, we will get localname and prefix of elements with a specific namespace. PHP <?php // Create an XML $xml = <<<EOD <?xml version="1.0" ?> <!-- this is the namespace --> <chapter xmlns:xi="my_namespace"> <title>Books of the other guy..</title> <para> <xi:include> <xi:fallback> </xi:fallback> </xi:include> </para> </chapter> EOD; $dom = new DOMDocument; // Load the XML string defined above $dom->loadXML($xml); // Use getElementsByTagName to get // the elements from xml foreach ($dom->getElementsByTagNameNS( 'my_namespace', '*') as $element) { echo '<b>Local name:</b> ', $element->localName, ', <b>Prefix: </b>', $element->prefix, "<br>"; } ?> Output: Local name: include, Prefix: xi Local name: fallback, Prefix: xi Program 2: In this example, we will get the number of elements of a certain namespace. PHP <?php // Create an XML $xml = <<<EOD <?xml version="1.0" ?> <!-- this is the namespace --> <chapter xmlns:xi="my_namespace"> <title>Books of the other guy..</title> <para> <xi:include> <!-- 1st --> <xi:fallback> <!-- 2nd --> </xi:fallback> </xi:include> <xi:include> <!-- 3rd --> <xi:fallback> <!-- 4th --> </xi:fallback> </xi:include> </para> </chapter> EOD; $dom = new DOMDocument(); // Load the XML string defined above $dom->loadXML($xml); // It will count all occurrence of // xi inside my_namespace $elements = $dom->getElementsByTagNameNS( 'my_namespace', '*'); print_r($elements->length); ?> Output: 4 Reference: https://www.php.net/manual/en/domdocument.getelementsbytagnamens.php Comment More infoAdvertise with us Next Article PHP | DOMDocument getElementsByTagnameNS() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads 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 getElementsByTagNameNS() Function 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 2 min read 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 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 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 Like