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 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 specific namespace.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 elments from xml
foreach ($dom->getElementsByTagNameNS(
        'my_namespace', '*') as $element) {
    echo '<b>Local name:</b> '
        $element->localName,
        ', <b>Prefix: </b>'
        $element->prefix, "<br>";
}
  
?>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


Output:

4

Reference: https://www.php.net/manual/en/domdocument.getelementsbytagnamens.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Akanksha_Rai

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.