xpath 查询到 xpath 查询结果 [英] xpath query to xpath result of query

查看:42
本文介绍了xpath 查询到 xpath 查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如我们有这个xml:

for example we have this xml:

<body>
        <a>
            <b>
                <c>hello</c>
                <c>world</c>
            </b>
        </a>
        <a>
            <b>
                <c>another hello</c>
                <c>world</c>
            </b>
        </a>
</body>

通过 Xpath 查询,我们可以找到所有B"标签.但是我们需要在每个找到的B"标签中找到所有的C"标签.我写了这段代码:

by Xpath query we can find all "B"-tags. But then we need to find all "C"-tags in every found "B"-tag. I wrote this code:

$dom = new DOMDocument();
$dom->loadXML($xml);

$xpath = new DOMXPath($dom);
$btags = $xpath->query("//b");

foreach ($btags as $b)
{
  $ctags = $xpath->query("/b/c", $b);
      foreach ($ctags as $c) {
        echo $c->nodeValue;
      }

}

但它不起作用.可以用 XPath 查询来做到这一点吗?

But it doesn't work. It possible to do this with XPath query's?

推荐答案

对于你的第二个 XPath,试试这个:$ctags = $xpath->query("c", $b);

For your second XPath, try this instead: $ctags = $xpath->query("c", $b);

第二个 XPath 已经相对于 'b' 节点...如果我没记错的话,PHP XPath 语句中的相对路径要求您省略前导的 '/'.

This second XPath is already relative to the 'b' node...if I'm not mistaken, relative pathing in PHP XPath statements requires that you omit the leading '/'.

这篇关于xpath 查询到 xpath 查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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