Search code examples
phpnamespacessimplexml

SimpleXML load fails to get namespaces


When I try to get namespaces I've got a wrong result, SimpleXML extension was loaded no errors thrown.

NOTE: On the local machine, I also cannot reproduce, but in shared host it always reproducible, is there any reasons why?

Snippet:

<?php
$xmlString = <<<DATA
<?xml  version="1.0" ?>
<some version="2.0" xmlns:a="test0" xmlns:b="test1" xmlns:c="test2"></some>
DATA;
$xml = simplexml_load_string( $xmlString );
var_dump($xml->getDocNamespaces());

Result:

array(1) {
  [""]=>
  string(5) "test0"
}

Expected: get

a=>test0,
b=>test1,
c=>test2

Is there any options or reasons why simplexml doesn't fetch that namespaces?

P.S. I have tried to use simplexml_load_file, simplexml_import_dom(new DomDocument -> loadXML..), all of them return same result

P.S.2.

PHP Version 5.6.29
SimpleXML
Simplexml support   enabled
Revision    $Id: d7077fc935154236afb4fe70814ba358efdbdca4 $
Schema support  enabled

Solution

  • Try this: http://codepad.org/GOggdpkJ

    I have added true to getDocNamespaces(TRUE)

    <?php
    
    $xml = <<<XML
    <?xml version="1.0" standalone="yes"?>
    <some version="2.0" xmlns:a="test0" xmlns:b="test1" xmlns:c="test2"></some>
    XML;
    
    $sxe = new SimpleXMLElement($xml);
    
    $namespaces = $sxe->getDocNamespaces(TRUE);
    var_dump($namespaces);
    
    ?>