PHP | DOMNode lookupNamespaceUri() Function

The DOMNode::lookupNamespaceUri() function is an inbuilt function in PHP which is used to get the namespace URI of the node based on the prefix.

Syntax:

string DOMNode::lookupNamespaceUri( string $prefix )

Parameters: This function accepts a single parameter $prefix which holds the prefix.



Return Value: This function returns namespace URI of the node.

Below examples illustrate the DOMNode::lookupNamespaceUri() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a XML variable with no namespace
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<root>
    <h1>GeeksforGeeks</h1>
</root>
XML;
  
// Load the XML
$document->loadXML($xml);
  
// Get the default namespace URI
$uri = $document->documentElement->lookupnamespaceURI(null);
echo $uri;
?>

chevron_right


Output:

// Empty string which means no namespace is there.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Load the XML with a namespace with prefix x
$document->loadXML("<?xml version=\"1.0\"?>
    <div xmlns:x=\"my_namespace\">
        <x:h1 x:style=\"color:red;\"> GeeksforGeeks </x:h1>
    </div>
");
  
// Get the URI with prefix x
$uri = $document->documentElement->lookupnamespaceURI('x');
echo $uri;
?>

chevron_right


Output:

my_namespace

Reference: https://www.php.net/manual/en/domnode.lookupnamespaceuri.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.


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.