Friday, November 21, 2025
HomeLanguagesPHP | DOMNode lookupNamespaceUri() Function

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:




<?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>neveropen</h1>
</root>
XML;
  
// Load the XML
$document->loadXML($xml);
  
// Get the default namespace URI
$uri = $document->documentElement->lookupnamespaceURI(null);
echo $uri;
?>


Output:

// Empty string which means no namespace is there.

Example 2:




<?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;\"> neveropen </x:h1>
    </div>
");
  
// Get the URI with prefix x
$uri = $document->documentElement->lookupnamespaceURI('x');
echo $uri;
?>


Output:

my_namespace

Reference: https://www.php.net/manual/en/domnode.lookupnamespaceuri.php

RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11997 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7166 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS