I'm working on a Greasemonkey script which needs to operate on each node between two others. Currently, I'm getting the first node using an (increasingly-complicated) XPath expression. I have another expression to get the "between" nodes, but it contains the initial expression twice and is getting rather long. Here's an earlier version which only contained two "clauses":
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]]"
What I'm looking for is a way to select the "contents" based on a context node rather than reincluding the original expression multiple times — that "header" expression is going to get considerably more complex very quickly. I know this can be done in XSLT using the current()
function, but of course that isn't available in 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>
As I type this, it occurs to me that at this point, it would probably be easier to use the DOM to collect the contents rather than XPath, but I'm still interested to know if this is something that can be done.
The original version of the script is available on UserScripts.org.