douan2478 2011-10-02 02:50
浏览 47
已采纳

XPath选择父母兄弟的后代

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?

  • 写回答

1条回答 默认 最新

  • doudong3570 2011-10-02 03:04
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛