drdawt9210 2009-08-07 09:07
浏览 65
已采纳

使用PHP从XML中删除命名空间

I have an XML document that looks like this:

<Data 
  xmlns="http://www.domain.com/schema/data" 
  xmlns:dmd="http://www.domain.com/schema/data-metadata"
>
  <Something>...</Something>
</Data>

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条回答 默认 最新

  • 普通网友 2009-08-07 09: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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部