由于默认名称空间,XML简单的XML解析器和Xpath问题

时间:2019-07-18 09:41:43

标签: php xpath namespaces xml-namespaces

我尝试使用xpath在XML字符串(PHP simplexml)中选择与给定名称匹配的节点。但是,由于默认的名称空间,它不起作用。假设我正在使用从Internet下载的XML文件并且无法更改,那么该怎么做?

我已经查看了有关该问题的所有非常详细的论坛帖子(包括这篇Reference - how do I handle namespaces (tags and attributes with colon in) in SimpleXML?),但找不到我的问题的答案。

XML字符串:

<scrutin xmlns="http://url_here.com/x" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <id>1</id>
  <numero>1</numero>
  <individuals>
    <group>
      <resultFor>
        <individual>345</individual>
        <individual>568</individual>
       </resultFor>
       <resultAgainst>
         <individual>567</individual>
         <individual>687</individual>
       </resultAgainst>
     </group>
   </individuals>
</scrutin>

我的简单PHP代码,带有xpath:

 foreach($xml->xpath('individual') as $mp){
  //code...
}

如上所述,我想访问XML字符串中的所有individual。目前,由于默认的命名空间,我无法访问这些节点。唯一有效的xpath是xpath('.')

非常感谢。

0 个答案:

没有答案
相关问题