XPath 选择父母兄弟姐妹的后代

发布于 2024-12-07 18:11:50 字数 1043 浏览 1 评论 0原文

此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

征﹌骨岁月お 2024-12-14 18:11:50

您的错误在于

...font[...]/following-sibling::tr/...

很容易看出,在提供的 XML 片段中, 元素没有同级元素。

这是一个正确的 XPath 表达式

  tr[td[contains(., 'Refine by Vehicle Types')]]
      /following-sibling::tr
        /td/div/table
                 /tr/td/font
                         /ul/li/a

根据以下 XML 文档进行评估时(您提供的片段由 包装):

<table>
    <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>
</table>

>选择以下元素

<a class="padded2"> Cars</a>
<a class="padded2">Marine Engines</a>
<a class="padded2">Trucks</a>

基于XSLT的验证

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:copy-of select=
  "tr[td[contains(., 'Refine by Vehicle Types')]]
      /following-sibling::tr
        /td/div/table
                 /tr/td/font
                         /ul/li/a
  "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于上面的XML文档时,将输出所选元素

<a class="padded2"> Cars</a>
<a class="padded2">Marine Engines</a>
<a class="padded2">Trucks</a>

我建议使用 XPath Visualizer 快速编写正确且优雅的 XPath 表达式。

Your error is in this:

...font[...]/following-sibling::tr/...

It is easy to see that in the provided XML fragment, the <font> element has no sibling elements.

Here is one correct XPath expression:

  tr[td[contains(., 'Refine by Vehicle Types')]]
      /following-sibling::tr
        /td/div/table
                 /tr/td/font
                         /ul/li/a

When evaluated against the following XML document (your provided fragment wrapped by a <table>):

<table>
    <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>
</table>

the following elements are selected:

<a class="padded2"> Cars</a>
<a class="padded2">Marine Engines</a>
<a class="padded2">Trucks</a>

XSLT - based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/*">
  <xsl:copy-of select=
  "tr[td[contains(., 'Refine by Vehicle Types')]]
      /following-sibling::tr
        /td/div/table
                 /tr/td/font
                         /ul/li/a
  "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the XML document above, the selected elements are output:

<a class="padded2"> Cars</a>
<a class="padded2">Marine Engines</a>
<a class="padded2">Trucks</a>

I would recommend using an XPath Visualizer to get quickly up with writing correct and elegant XPath expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文