getNamespaces()

Namespace are used in XML document to avoid conflict of name used by different XML developers. For example name element can have name of the student or it can have name of the fruits in basket.

We will discuss more on how to get namespace details by using PHP. Here is the example to get namespace from our XML file. We have used one more sample file which used namespace in its elements.

xml-sample2.php

<?Php
$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<b:details xmlns:b="https://www.example.com/ns">
<name>Abcd</name>

<o:address xmlns:o="https://www.example.com/ns1">
  <o:door_no>234-ac</o:door_no>
  <o:street>Great Wall</o:street>
  <o:city>Alabama</o:city>
</o:address>


<r:address xmlns:r="https://www.example.com/ns2">
  <r:door_no>543-de</r:door_no>
  <r:street>Garden street</r:street>
  <r:city>New Work</r:city>
</r:address>


</b:details>
XML;
?>
We will use PHP script to get the namespaces from above XML data. Here it is
<?Php
require "xml-sample2.php";
$main1 = new SimpleXMLElement($str_xml);
$nm=$main1->getNamespaces(TRUE);
var_dump($nm);
?>
The output is
array(3) { ["b"]=> string(25) "https://www.example.com/ns" ["o"]=> string(26) "https://www.example.com/ns1" ["r"]=> string(26) "https://www.example.com/ns2" }
By using recursive to TRUE we will get name space of total documents including all child nodes. By making it FALSE it will return only root node namespaces.

Now change this line
$nm=$main1->getNamespaces(FALSE);
We will get only document root namespace
array(1) { ["b"]=> string(25) "https://www.example.com/ns" }

Example: Extracting Namespaces from Complex XML

$xml = <<<XML
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">
    <h:table>
        <h:tr>
            <h:td>Apples</h:td>
            <h:td>Bananas</h:td>
        </h:tr>
    </h:table>
    <f:table>
        <f:name>African Coffee Table</f:name>
        <f:width>80</f:width>
        <f:length>120</f:length>
    </f:table>
</root>
XML;

$xmlObject = simplexml_load_string($xml);
$namespaces = $xmlObject->getNamespaces(true);
print_r($namespaces);
Output
Array ( [h] => http://www.w3.org/TR/html4/ [f] => http://www.w3schools.com/furniture )

Example: Using Namespaces in XPath Queries

$xml = <<<XML
<root xmlns:h="http://www.w3.org/TR/html4/" xmlns:f="http://www.w3schools.com/furniture">
    <h:table>
        <h:tr>
            <h:td>Apples</h:td>
            <h:td>Bananas</h:td>
        </h:tr>
    </h:table>
    <f:table>
        <f:name>African Coffee Table</f:name>
        <f:width>80</f:width>
        <f:length>120</f:length>
    </f:table>
</root>
XML;

$xmlObject = simplexml_load_string($xml);

// Extracting namespaces
$namespaces = $xmlObject->getNamespaces(true);
print_r($namespaces);

// Register the HTML namespace for XPath queries
$xmlObject->registerXPathNamespace('h', 'http://www.w3.org/TR/html4/');

// Query for <h:td> elements
$result = $xmlObject->xpath('//h:td');

echo "HTML table data:n";
foreach ($result as $element) {
    echo $element . "n";  // Output: Apples, Bananas
}

Explanation:

  • Step 1: First, the XML data is loaded using simplexml_load_string(), and we extract the namespaces using getNamespaces(), which returns the defined namespaces with their respective prefixes.
  • Step 2: Using registerXPathNamespace(), we register the HTML namespace (`h`) to use it in our XPath query.
  • Step 3: We execute an XPath query using xpath() to find all `` elements inside the XML structure. The results are iterated and displayed, showing the values "Apples" and "Bananas".

Output:

Array
(
    [h] => http://www.w3.org/TR/html4/
    [f] => http://www.w3schools.com/furniture
)
HTML table data:
Apples
Bananas

XML XML Children

Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer