简体   繁体   English

如何选择孙子-使用php的xml中的子项

[英]How to select grandchild - child in xml using php

I have a xml file and i need to select grandchild or grandchild's child using xpath. 我有一个xml文件,我需要使用xpath选择孙子或孙子。

Here is xml file link - XML file I do that with this code: 这是xml文件链接-XML文件我使用以下代码进行操作:

<?php
        $dom = new DOMDocument();
        $dom->validateOnParse = TRUE;
        $dom->load('file.xml')

        $xpath = new DomXPath($dom);

        $nodes = $xpath->query('//question[@id = "1"]/answer[@id = "1"]/subquestion[@id = "1-1"]/answer[@id = "1-1"]');
?>

Above code work fine but i need shorten query as i don't know the dept of subquestions. 上面的代码工作正常,但我需要缩短查询时间,因为我不知道子问题的部门。

Thanks 谢谢

You can use the // at any point in the query, so 您可以在查询的任何位置使用//,因此

//question[@id = "1"]//subquestion[@id = "1-1"]/answer[@id = "1-1"]

can find it at any level below the one your after. 可以在您追求的水平以下的任何水平找到它。

You can always play with these things in something like https://www.freeformatter.com/xpath-tester.html which allows you to test your expressions. 您始终可以使用https://www.freeformatter.com/xpath-tester.html之类的东西来玩这些东西,它可以测试您的表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM