2009-08-07 17:07
浏览 64


I have an XML document that looks like this:


I am parsing the information using SimpleXML in PHP. I am dealing with arrays and I seem to be having a problem with the namespace.

My question is: How do I remove those namespaces? I read the data from an XML file.

Thank you!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dqyanc7264 2009-08-07 17:31

    If you're using XPath then it's a limitation with XPath and not PHP look at this explanation on xpath and default namespaces for more info.

    More specifically its the xmlns="" attribute in the root node which is causing the problem. This means that you'll need to register the namespace then use a QName thereafter to refer to elements.

    $feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf');
    $feed->registerXPathNamespace("a", "http://www.domain.com/schema/data");
    $result = $feed->xpath("a:Data/a:Something/...");

    Important: The URI used in the registerXPathNamespace call must be identical to the one that is used in the actual XML file.

    解决 无用
    打赏 举报
  • doucezhu3570 2009-08-07 17:48

    To remove the namespace completely, you'll need to use Regular Expressions (RegEx). For example:

    $feed = file_get_contents("http://www.sitepoint.com/recent.rdf");
    $feed = preg_replace("/<.*(xmlns *= *[\"'].[^\"']*[\"']).[^>]*>/i", "", $feed); // This removes ALL default namespaces.
    $xml_feed = simplexml_load_string($feed);

    Then you've stripped any xml namespaces before you load the XML (be careful with the regex through, because if you have any fields with something like:

    <![CDATA[ <Transfer xmlns="http://redeux.example.com">cool.</Transfer> ]]>

    Then it will strip the xmlns from inside the CDATA which may lead to unexpected results.

    解决 无用
    打赏 举报
  • douer9399 2011-10-03 23:03

    I found the answer above to be helpful, but it didn't quite work for me. This ended up working better:

    // Gets rid of all namespace definitions 
    $xml_string = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $xml_string);
    // Gets rid of all namespace references
    $xml_string = preg_replace('/[a-zA-Z]+:([a-zA-Z]+[=>])/', '$1', $xml_string);
    解决 无用
    打赏 举报
  • douchui7332 2012-10-30 15:45

    The following PHP code automatically detects the default namespace specified in the XML file under the alias "default". No all xpath queries have to be updated to include the prefix default:

    So if you want to read XML files rather they contain an default NS definition or they don't and you want to query all Something elements, you could use the following code:

    $xml = simplexml_load_file($name);
    $namespaces = $xml->getDocNamespaces();
    if (isset($namespaces[''])) {
        $defaultNamespaceUrl = $namespaces[''];
        $xml->registerXPathNamespace('default', $defaultNamespaceUrl);
        $nsprefix = 'default:';
    } else {
        $nsprefix = '';
    $somethings = $xml->xpath('//'.$nsprefix.'Something');
    echo count($somethings).' times found';
    解决 无用
    打赏 举报

相关推荐 更多相似问题