Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php xpath中的遍历_Php_Xml_Xpath - Fatal编程技术网

Php xpath中的遍历

Php xpath中的遍历,php,xml,xpath,Php,Xml,Xpath,我以前从来没有使用过xpath,这让我很抓狂。我有以下XML: <entry> <updated>2018-02-20</updated> <api:related> <api:object> <api:ever-approved>true</api:ever-approved> <api:reporting-date-1>2017-12-31</api:report

我以前从来没有使用过xpath,这让我很抓狂。我有以下XML:

<entry>
 <updated>2018-02-20</updated>
  <api:related>
   <api:object>
    <api:ever-approved>true</api:ever-approved>
    <api:reporting-date-1>2017-12-31</api:reporting-date-1>
    <api:repository-items>
     <api:repository-item repository-id="1">
      <api:public-url>http://url.com</api:public-url>
      <api:content-file-count>1</api:content-file-count>
      <api:licence-file-count>1</api:licence-file-count>
      <api:status>accepted</api:status>
     </api:repository-item>
    </api:repository-items>
    <api:all-labels type="keyword-list">
     <api:keywords>
      <api:keyword>Africa</api:keyword>
      <api:keyword>Economy</api:keyword>
     </api:keywords>
    </api:all-labels>
  </api:object>
</api:related>
这很好。但是,我无法遍历到存储库项目中的任何内容-我只得到“nada”

我试过:

  • 'api:相关/api:对象/api:存储库项目/api:存储库项目/api:状态'
  • 'api:相关/api:对象/api:存储库项目/api:存储库项目/api:内容文件计数'
  • 'api:相关/api:对象/api:存储库项目/api:存储库项目'
  • “api:相关/api:对象/api:存储库项”
  • 'api:related/api:object//api:status'

我做错了什么?还有其他建议吗?!谢谢

如果我正确理解了XML,这就是正确的XPATH:

'//entry//api:repository-items'
//entry
搜索XML中名为
entry
的节点的相对路径,而
//api:repository items
在名为
api:repository items
的节点上搜索相对路径,这些节点嵌套在
//entry


我强烈建议你阅读这本书。这是一个很好的指南,包含了您需要的关于XPath的所有信息以及一些图形示例,而且不太长。

谢谢-但您稍微忽略了一点-我已经在$this中说明了入口节点:正如我所说的,路径“api:related/api:object/api:all labels/api:keywords/api:keyword[1]”非常有效。我试图理解为什么会这样,但“api:related/api:object/api:repository items/api:repository items/api:content file count”和“//api:repository items”不会。(哦——我确实已经读过W3XPath指南了——很好,但在这方面对我没有帮助!)我确实误解了这个问题,对不起。尽管如此,xpath并没有错,如果您的XML结构在整个文档中都是这样,那么您在“我尝试了什么”一节中提供的xpath也应该可以正常工作。我想问题在于如何在PHP代码上使用xpath结果?不幸的是,我现在不能帮你,我稍后会尝试编辑这个答案。谢谢卢卡斯-这让人放心。。。我认为实际的问题在于我得到的XML。您还没有显示“api”前缀的任何名称空间绑定。由于不正确的命名空间绑定是XPath问题的常见来源,这意味着我们无法验证您是否正确。
'//entry//api:repository-items'