You are counting the elements considering the full node-set of all page
elements. If you want to restart the numbering in each numberGroup
you have to select them in this context.
I'm assuming your n1.xml
, p1.xml
files have this format:
<pages>
<page/>
<page/>
<page/>
</pages>
You can restrict the context of your selection to the files that are in each numberGroup
by creating a template for numberGroup
and selecting the page
elements within that context:
<xsl:template match="numberGroup">
<xsl:apply-templates select="document(entry/file)//page"/>
</xsl:template>
Now, for each page you just have to get its position()
:
<xsl:template match="page">
<span class="r" id="pg{position()}"/>
</xsl:template>
This would produce this result:
<div>
<span class="r" id="pg1"/>
<span class="r" id="pg2"/>
...
<span class="r" id="pg99"/> <!-- total pages of all files in the first group -->
</div>
<div>
<span class="r" id="pg1"/>
<span class="r" id="pg2"/>
...
<span class="r" id="pg19"/> <!-- total pages in second group -->
</div>
<div>...</div>
So the solution should be much simpler than the one you were trying. You don't need any variables or complex XPath expressions. Here is a full stylesheet that should produce a result similar to what you expect:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="numberGroup">
<div>
<xsl:apply-templates select="document(entry/file)//page"/>
</div>
</xsl:template>
<xsl:template match="page">
<span class="r" id="pg{position()}"/>
</xsl:template>
</xsl:stylesheet>