XPath 选择父母兄弟姐妹的后代
此 html 位于我的页面内:
<tr>
<td class="padded2" bgcolor="#103A74"><font color="White">Refine by Vehicle Types</font></td>
</tr><tr>
<td class="padded2" bgcolor="White"><div>
<table border="0">
<tr>
<td class="padded2"><font color="#103A74"><ul><li><a class="padded2"> Cars</a></li><li><a class="padded2">Marine Engines</a></li><li><a class="padded2">Trucks</a></li></ul></font></td>
</tr>
</table>
</div></td>
</tr>
我想根据“汽车”和“卡车”位于“按车辆类型优化”之后的事实来抓取“汽车”和“卡车”。 我尝试了很多不同的方法,这是我能得到的最接近的方法,但返回 NULL。
$Nodes = $xPath->query("//tr/td/font[text()[contains(., 'Refine by Vehicle Type')]]/following-sibling::tr/td/div/table/tr/td/font/ul/li/a")->item(0)->nodeValue;
我缺少什么?
This html is within my page:
<tr>
<td class="padded2" bgcolor="#103A74"><font color="White">Refine by Vehicle Types</font></td>
</tr><tr>
<td class="padded2" bgcolor="White"><div>
<table border="0">
<tr>
<td class="padded2"><font color="#103A74"><ul><li><a class="padded2"> Cars</a></li><li><a class="padded2">Marine Engines</a></li><li><a class="padded2">Trucks</a></li></ul></font></td>
</tr>
</table>
</div></td>
</tr>
I'm wanting to scrape "Cars" and "Trucks" based on the fact that they are after "Refine by Vehicle Type".
I've tried many diferent ways and this is as close as I can get, but returns NULL.
$Nodes = $xPath->query("//tr/td/font[text()[contains(., 'Refine by Vehicle Type')]]/following-sibling::tr/td/div/table/tr/td/font/ul/li/a")->item(0)->nodeValue;
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误在于:
很容易看出,在提供的 XML 片段中,
元素没有同级元素。
这是一个正确的 XPath 表达式:
根据以下 XML 文档进行评估时(您提供的片段由
包装):
>选择以下元素:
基于XSLT的验证:
当此转换应用于上面的XML文档时,将输出所选元素:
我建议使用 XPath Visualizer 快速编写正确且优雅的 XPath 表达式。
Your error is in this:
It is easy to see that in the provided XML fragment, the
<font>
element has no sibling elements.Here is one correct XPath expression:
When evaluated against the following XML document (your provided fragment wrapped by a
<table>
):the following elements are selected:
XSLT - based verification:
when this transformation is applied on the XML document above, the selected elements are output:
I would recommend using an XPath Visualizer to get quickly up with writing correct and elegant XPath expressions.