Xpath反向搜索

问题描述 投票:0回答:1

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

我正在做一个网站的一些工作。 (链接如下)。 http://www.sturmfh.com/obit-display.jhtml?DB=update/obits/dbase&DO=display&ID=1189477693_24578

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

php web-scraping dom xpath domdocument
1个回答
2
投票

使用

(//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:[email protected]?Subject=For%20the%20Family%20of%20Lyle%20Meier">Click</a>
   <a href="mailto:[email protected]?Subject=For%20the%20Family%20of%20Lyle%20Meier">
      <img src="/images/email_condol.gif" alt="Logo" border="0" align="middle"/>
   </a>
   <a href="mailto:[email protected]?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>
© www.soinside.com 2019 - 2024. All rights reserved.