【问题标题】:Xpath reverse searchingXpath 反向搜索
【发布时间】:2012-01-25 13:33:43
【问题描述】:
【问题讨论】:
标签:
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&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>