使用PHP从XML删除名称空间 [英] Remove namespace from XML using PHP

查看:72
本文介绍了使用PHP从XML删除名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的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>

我正在使用PHP中的SimpleXML解析信息.我正在处理数组,但名称空间似乎有问题.

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.

我的问题是:如何删除那些名称空间?我从XML文件读取数据.

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

谢谢!

推荐答案

如果您使用的是XPath,那么这是XPath的局限性,不是 PHP请在

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.

更具体地说,它是导致问题的根节点中的xmlns=""属性.这意味着您需要注册名称空间,然后使用 QName 来引用元素.

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/...");

重要:registerXPathNamespace调用中使用的URI必须与实际XML文件中使用的URI相同.

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

这篇关于使用PHP从XML删除名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆