2

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

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

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

4

1 回答 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: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>
于 2011-12-21T20:22:25.627 回答