Thursday 26 March 2015

XSLT group by postion

Issue

Need to create individual files each with n nodes from the master file

Resolution

There is a simple resolution to this although it is specific to XSLT 2. This uses the for-each-group routine and the idiv operator. idiv is an integer division which is equivalent to (x div y) cast as xs:integer

Consider a nodeset held as a variable $nodes and a group size of nodes defined by the integer variable $groupsize, then using the following routine will split the nodeset into distinct files with $groupsize nodes to each file

XSLT

<xsl:for-each-group select="$nodes" 
    group-adjacent="(position() - 1) idiv $groupsize">
 
    <xsl:result-document href="{concat('resultdoc', format-number(position(),'000'), '.xml')}">
        <result>
            <xsl:sequence select="current-group()"/>
        </result>
    </xsl:result-document>
</xsl:for-each-group>

No comments:

Post a Comment