I am trying to access a list of nodes without namespace declaration within nodes with namespace declaration. My XML file has a main node with namespace ehd
with two subnodes header, body within the same namespace. However, all subnodes within the body node have no further namespace declaration. I am struggling with accessing these nodes with SimpleXML.
Excerpt from the xml file:
<?xml version="1.0" encoding="ISO-8859-15"?>
<ehd:ehd ehd_version="1.40" xmlns:ehd="urn:ehd/001" xmlns="urn:ehd/go/001">
<ehd:header>
</ehd:header>
<ehd:body>
<gnr_liste>
<gnr V="01100"></gnr>
<gnr V="01101"></gnr>
<gnr V="01102"></gnr>
</gnr_liste>
</ehd:body>
</ehd:ehd>
My code is as follows:
$xml = simplexml_load_file($file) or die("Failed to load");
$ehd = $xml->children('ehd', true)->body;
simplexml_dump($ehd);
$gnr_liste = $ehd->children('gnr_liste')->children('gnr');
simplexml_dump($gnr_liste);
The output is:
SimpleXML object (1 item)
[
Element {
Namespace: 'urn:ehd/001'
Namespace Alias: 'ehd'
Name: 'ehd'
String Content: ''
Content in Namespace ehd
Namespace URI: 'urn:ehd/001'
Children: 2 - 1 'body', 1 'header'
Attributes: 0
Content in Default Namespace
Children: 0
Attributes: 1 - 'ehd_version'
}
]
SimpleXML object (1 item)
[
Element {
Namespace: 'urn:ehd/001'
Namespace Alias: 'ehd'
Name: 'body'
String Content: ''
Content in Default Namespace
Namespace URI: 'urn:ehd/go/001'
Children: 1 - 1 'gnr_liste'
Attributes: 0
}
]
How do I access all gnr
items from the gnr_liste
node?
Note: I am using simplexml_dump for debugging