Wednesday, October 8, 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
32342 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6712 POSTS0 COMMENTS
Nicole Veronica
11875 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6833 POSTS0 COMMENTS
Ted Musemwa
7092 POSTS0 COMMENTS
Thapelo Manthata
6786 POSTS0 COMMENTS
Umr Jansen
6789 POSTS0 COMMENTS