Pavel Lepin said:
Unless I misunderstood your description of the problem, it
seems to me that the thing to do would be to use two
separate transformations.
The first one would aggregate your chunks into the XML
document containing all relevant information (you would
invoke this transformation on current version of aggregate
document, providing, say, filenames of new chunks as
parameters).
And here's how it would look in code:
pavel@debian:~/dev/xslt/aggr$ a aggregate.xsl
<xsl:stylesheet
version="1.0"
xmlns:xsl="
http://www.w3.org/1999/XSL/Transform"
xmlns:meta="
http://example.org/meta-data">
<xsl
aram name="chunks"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="data">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:call-template name="process-chunks">
<xsl:with-param name="chunk-list"
select="concat($chunks,',')"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="process-chunks">
<xsl
aram name="chunk-list"/>
<xsl:variable name="car"
select="substring-before($chunk-list,',')"/>
<xsl:variable name="cdr"
select="substring-after($chunk-list,',')"/>
<xsl:call-template name="process-chunk">
<xsl:with-param name="chunk" select="$car"/>
</xsl:call-template>
<xsl:if test="$cdr">
<xsl:call-template name="process-chunks">
<xsl:with-param name="chunk-list" select="$cdr"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="process-chunk">
<xsl
aram name="chunk"/>
<xsl:if test="not(/data/item[@meta:chunk=$chunk])">
<xsl:apply-templates
select=
"
document(concat('chunk_',$chunk,'.xml'))
/data/item
"
mode="aggregate-chunk-items">
<xsl:with-param name="chunk-name" select="$chunk"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<xsl:template match="data/item"
mode="aggregate-chunk-items">
<xsl
aram name="chunk-name"/>
<xsl:copy>
<xsl:attribute name="meta:chunk">
<xsl:value-of select="$chunk-name"/>
</xsl:attribute>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
pavel@debian:~/dev/xslt/aggr$ a data.xml
<data/>
pavel@debian:~/dev/xslt/aggr$ a chunk_1.xml
<data>
<item timestamp="127468743" value="13"/>
<item timestamp="127468746" value="12"/>
<item timestamp="127468749" value="16"/>
<item timestamp="127468750" value="9"/>
<item timestamp="127468755" value="10"/>
</data>
pavel@debian:~/dev/xslt/aggr$ xsltproc --stringparam chunks
1 aggregate.xsl data.xml >data_1.xml
pavel@debian:~/dev/xslt/aggr$ a data_1.xml
<?xml version="1.0"?>
<data><item xmlns:meta="
http://example.org/meta-data"
meta:chunk="1" timestamp="127468743" value="13"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468746" value="12"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468749" value="16"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468750" value="9"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468755" value="10"/></data>
pavel@debian:~/dev/xslt/aggr$ a chunk_2.xml
<data>
<item timestamp="127468762" value="13"/>
<item timestamp="127468763" value="8"/>
<item timestamp="127468769" value="19"/>
</data>
pavel@debian:~/dev/xslt/aggr$ xsltproc --stringparam chunks
2 aggregate.xsl data_1.xml >data_2.xml
pavel@debian:~/dev/xslt/aggr$ a data_2.xml
<?xml version="1.0"?>
<data><item xmlns:meta="
http://example.org/meta-data"
meta:chunk="1" timestamp="127468743" value="13"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468746" value="12"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468749" value="16"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468750" value="9"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468755" value="10"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468762" value="13"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468763" value="8"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468769" value="19"/></data>
pavel@debian:~/dev/xslt/aggr$ a chunk_3.xml
<data>
<item timestamp="127468770" value="21"/>
<item timestamp="127468771" value="7"/>
<item timestamp="127468781" value="18"/>
<item timestamp="127468783" value="9"/>
<item timestamp="127468785" value="2"/>
<item timestamp="127468795" value="11"/>
</data>
pavel@debian:~/dev/xslt/aggr$ xsltproc --stringparam chunks
1,2,3 aggregate.xsl data_2.xml >data_3.xml
pavel@debian:~/dev/xslt/aggr$ a data_3.xml
<?xml version="1.0"?>
<data><item xmlns:meta="
http://example.org/meta-data"
meta:chunk="1" timestamp="127468743" value="13"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468746" value="12"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468749" value="16"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468750" value="9"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="1"
timestamp="127468755" value="10"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468762" value="13"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468763" value="8"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="2"
timestamp="127468769" value="19"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468770" value="21"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468771" value="7"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468781" value="18"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468783" value="9"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468785" value="2"/><item
xmlns:meta="
http://example.org/meta-data" meta:chunk="3"
timestamp="127468795" value="11"/></data>
pavel@debian:~/dev/xslt/aggr$