后续 Xpath 查询无法正常工作
我正在尝试跨多个 Xpath 查询访问多个项目,同时使用第一个查询作为基础。
我尝试过:
$playerinfo = $xpath->query('//*[@class="PlayerHeader"]');
$playername = $xpath->query('/h3/a', $playerinfo);
echo $playername->item(0)->nodeValue;
什么都没有返回。如果我这样做,
$playerinfo = $xpath->query('//*[@class="PlayerHeader"]/h3/a');
echo $playerinfo->item(0)->nodeValue;
效果很好。任何帮助表示赞赏。
I'm trying to access the multiple items across multiple Xpath queries, while using the first query as a base.
I tried:
$playerinfo = $xpath->query('//*[@class="PlayerHeader"]');
$playername = $xpath->query('/h3/a', $playerinfo);
echo $playername->item(0)->nodeValue;
Nothing is returned. If I do
$playerinfo = $xpath->query('//*[@class="PlayerHeader"]/h3/a');
echo $playerinfo->item(0)->nodeValue;
It works fine. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试针对节点计算绝对 XPath 表达式——这是没有意义的。
绝对表达式总是以文档节点作为上下文节点来计算。
没有
/h3
节点(顶部元素可能是html
,而不是h3
,因此,没有/ h3/a
节点也)。解决方案:使用相对表达式:
h3/a
。建议:阅读“相对和绝对 XPath 表达式”并了解该主题。
You are trying to evaluate an absolute XPath expression against a node -- that isn't meaningful.
An absolute expression is always evaluated having the document node as context node.
there is no
/h3
node (the top element is probablyhtml
, noth3
, and due to this, there is no/h3/a
node also).Solution: Use a relative expression:
h3/a
.Recommendation: Read about "Relative and Absolute XPath expressions" and understand this topic.
$playerinfo 是匹配节点的列表 - 当您将其用作后续搜索的上下文时,上下文只能是单个节点,而不是节点列表:
$playerinfo is a list of matching nodes - when you use it as the context for a subsequent search, the context can only be a SINGLE node, not a node list: