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