Xpath反向搜索

发布于 2024-12-22 14:50:40 字数 383 浏览 0 评论 0原文

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

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

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

Is there a way, when using DOM_Document Xpath to search in reverse (from the end of the page moving up instead of from the top down?)
If so, how would I do this?

I am doind a scrape of a web site. (linked below).
http://www.sturmfh.com/obit-display.jhtml?DB=update/obits/dbase&DO=display&ID=1189477693_24578

I only want to scrape the 3 obituary paragraphs. So i figured it'd be easiest to start at the end and move up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

凶凌 2024-12-29 14:50:40

使用

(//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&lname=Meier">Click</a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&lname=Meier">
      <img src="/images/email_condol.gif" border="0" alt="View" align="top"/>
   </a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&lname=Meier">here</a>
</p>

Use:

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

This selects the last (up to three) p elements in the XML document.

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:strip-space elements="*"/>

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

When applied against the document, referenced in the question, this transformation evaluates the XPath expression and outputs the selected p elements.

The result is:

<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&lname=Meier">Click</a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&lname=Meier">
      <img src="/images/email_condol.gif" border="0" alt="View" align="top"/>
   </a>
   <a href="./obit-foldreq.jhtml?fname=Lyle&lname=Meier">here</a>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文