【问题标题】:Xpath reverse searchingXpath 反向搜索
【发布时间】:2012-01-25 13:33:43
【问题描述】:

有没有办法,当使用 DOM_Document Xpath 反向搜索时(从页面末尾向上移动而不是从上向下移动?) 如果是这样,我该怎么做?

我正在浏览一个网站。 (下面链接)。 http://www.sturmfh.com/obit-display.jhtml?DB=update/obits/dbase&DO=display&ID=1189477693_24578

我只想刮掉 3 段讣告。所以我认为从最后开始向上移动是最容易的。

【问题讨论】:

  • 解释你为什么要这样做。

标签: php dom xpath domdocument scrape


【解决方案1】:

使用

(//p)[position() > count(//p) - 3]

这将选择 XML 文档中的最后一个(最多三个)p 元素。

基于 XSLT 的验证

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

 <xsl:template match="node()|@*">
     <xsl:copy-of select="(//p)[position() > count(//p) - 3]"/>
 </xsl:template>
</xsl:stylesheet>

当应用于问题中引用的文档时,此转换评估 XPath 表达式并输出选定的 p 元素。

结果是

<p>
                If you would like to share your thoughts and memories,<br/> we will deliver your message to the family.<br/>
   <a href="mailto:staff@sturmfh.com?Subject=For%20the%20Family%20of%20Lyle%20Meier">Click</a>
   <a href="mailto:staff@sturmfh.com?Subject=For%20the%20Family%20of%20Lyle%20Meier">
      <img src="/images/email_condol.gif" alt="Logo" border="0" align="middle"/>
   </a>
   <a href="mailto:staff@sturmfh.com?Subject=For%20the%20Family%20of%20Lyle%20Meier">here</a>.
        </p>
<p>To Request a Tribute Folder
                <br/>
   <a href="./obit-foldreq.jhtml?fname=Lyle&amp;lname=Meier">Click</a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&amp;lname=Meier">
      <img src="/images/email_condol.gif" border="0" alt="View" align="top"/>
   </a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&amp;lname=Meier">here</a>
</p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-31
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多