我正在研究一个 Greasemonkey 脚本,该脚本需要在其他两个节点之间的每个节点上运行。目前,我正在使用(越来越复杂的)XPath 表达式获得第一个节点。我有另一个表达式来获取“中间”节点,但它包含两次初始表达式并且变得相当长。这是一个仅包含两个“子句”的早期版本:
var xpHeader = "//h2[a/@name='section-References' or a/@name='References']";
var xpContents = "//h2[a/@name='section-References' or a/@name='References']/following-sibling::*[following-sibling::h2[1] = //h2[a/@name='section-References' or a/@name='References']/following-sibling::h2[1]]"
我正在寻找的是一种基于上下文节点选择“内容”的方法,而不是多次重新包含原始表达式——“标题”表达式将很快变得相当复杂。我知道这可以在 XSLT 中使用该current()
函数来完成,但当然这在 vanilla XPath 中不可用:
<xsl:template match="//h2[a/@name='section-References' or a/@name='References']">
<xsl:for-each select="following-sibling::*[following-sibling::h2[1] = current()/following-sibling::h2[1]]">
<!-- do stuff -->
</xsl:for-each>
</xsl:template>
当我键入此内容时,我突然想到,此时使用 DOM 收集内容可能比 XPath 更容易,但我仍然很想知道这是否可以完成。
该脚本的原始版本可在 UserScripts.org 上找到。