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
32543 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6921 POSTS0 COMMENTS
Nicole Veronica
12036 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7055 POSTS0 COMMENTS
Ted Musemwa
7293 POSTS0 COMMENTS
Thapelo Manthata
7014 POSTS0 COMMENTS
Umr Jansen
7000 POSTS0 COMMENTS