使用 PHP DOMXpath 遍历子节点?
我无法理解 childNode 中到底存储了什么。理想情况下,我想对每个子节点执行另一个 xquery,但似乎无法直接解决。这是我的场景: 数据:
<div class="something">
<h3>
<a href="link1.html">Link text 1</a>
</h3>
<div class"somethingelse">Something else text 1</div>
</div>
<div class="something">
<h3>
<a href="link2.html">Link text 2</a>
</h3>
<div class"somethingelse">Something else text 2</div>
</div>
<div class="something">
<h3>
<a href="link3.html">Link text 3</a>
</h3>
<div class"somethingelse">Something else text 3</div>
</div>
和代码:
$html = new DOMDocument();
$html->loadHtmlFile($local_file);
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='something']");
foreach ($nodelist as $n) {
Can I run another query here? }
对于“某事”的每个元素(即 $n),我想访问两段文本和 href 的值。我尝试使用 childNode 和另一个 xquery 但无法让任何东西工作。任何帮助将不胜感激!
I'm having some trouble understanding what exactly is stored in childNodes. Ideally I'd like to do another xquery on each of the child nodes, but can't seem to get it straight. Here's my scenario:
Data:
<div class="something">
<h3>
<a href="link1.html">Link text 1</a>
</h3>
<div class"somethingelse">Something else text 1</div>
</div>
<div class="something">
<h3>
<a href="link2.html">Link text 2</a>
</h3>
<div class"somethingelse">Something else text 2</div>
</div>
<div class="something">
<h3>
<a href="link3.html">Link text 3</a>
</h3>
<div class"somethingelse">Something else text 3</div>
</div>
And the code:
$html = new DOMDocument();
$html->loadHtmlFile($local_file);
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//div[@class='something']");
foreach ($nodelist as $n) {
Can I run another query here? }
For each element of "something" (i.e., $n) I want to access the values of the two pieces of text and the href. I tried using childNode and another xquery but couldn't get anything to work. Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以运行另一个 xpath 查询,类似这样:
这将为您提供带有类 someelse 的内部 div,$xpath->query 方法的第二个参数告诉查询将此节点作为上下文,请参阅更多 https://www.php.net/manual/en/domxpath.query.php< /a>
Yes you can run another xpath query, something like that :
This will get you the inner div with the class somethingelse, the second argument of the $xpath->query method tells to query to take this node as context, see more https://www.php.net/manual/en/domxpath.query.php
Trexx 有,但他错过了问题的最后一句:
Trexx had it but he missed the last sentence of the question:
如果我正确理解你的问题,那么当我使用descendant:: 表达式时它就起作用了。尝试一下:
尽管有时使用 // 路径表达式组合查询就足以缩小搜索范围。 // 路径表达式从当前节点开始选择文档中与选择器匹配的节点。
然后循环遍历这些内容以找到您需要的内容。希望这有帮助。
If I understand your question correctly, it worked when I used the descendant:: expression. Try this:
Although sometimes it's just enough to combine queries using the // path expression for narrowing your search. The // path expression selects nodes in the document starting from the current node that match the selector.
Then loop through those for the stuff you need. Hope this helps.
下面是一个代码片段,允许您访问每个具有类属性“something”的节点中包含的信息:
Here is a code snippet that allows you to access the information contained within each of the nodes with class attribute "something":