I am dealing with huge XML files (hundreds of MBs). I found perfect solution that does work for me: How to use XMLReader in PHP? (accepted answer).
Now, in my code I need to know the path of currently processed node. I am using DOM function getNodePath for that purpose. Everything looks OK unless XML document has declared default NS.
Here is my code:
$doc = new DOMDocument();
$z = new XMLReader;
$z->open($_SERVER['argv'][1]);
while ($z->read() && $z->name !== 'header');
$onix = simplexml_import_dom($doc->importNode($z->expand(), true));
$xpath = dom_import_simplexml($onix)->getNodePath();
echo "#".$xpath."#".PHP_EOL;
And here go the sample files. First - working - without NS declared, and the 2nd is namespaced.
<ONIXmessage>
<header>
<m174>blah</m174>
</header>
</ONIXmessage>
<ONIXmessage xmlns="http://www.editeur.org/onix/2.1/short">
<header>
<m174>blah</m174>
</header>
</ONIXmessage>
I need to get "/header" in my code. But I get "/*" if I parse the 2nd file. Removing xmlns attribute using string functions works, but it is not acceptable for me.