<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes"/>
	<xsl:variable name="chunk" select="100"/>

	<xsl:template match="//RolledUpItems">
		<root>
			<xsl:call-template name="inventory">
				<xsl:with-param name="key" select="./@key"/>
				<xsl:with-param name="quantity-per-pallet" select="number(@quantityPerPallet)"/>
				<xsl:with-param name="upperbound" select="number(@quantity)"/>
				<xsl:with-param name="top" select="number(@quantity)"/>
			</xsl:call-template>
		</root>
	</xsl:template>

	<xsl:template name="inventory">
		<xsl:param name="key"/>
		<xsl:param name="quantity-per-pallet"/>
		<xsl:param name="upperbound"/>
		<xsl:param name="count" select="0"/>
		<xsl:param name="top"/>

		<xsl:if test="$upperbound &lt;= $chunk">
			<xsl:call-template name="_inventory">
				<xsl:with-param name="key" select="$key"/>
				<xsl:with-param name="quantity-per-pallet" select="$quantity-per-pallet"/>
				<xsl:with-param name="upperbound" select="$upperbound"/>
				<xsl:with-param name="base" select="$count"/>
			</xsl:call-template>
		</xsl:if>


		<xsl:if test="$upperbound &gt; $chunk and $count &lt;= $top">
			<xsl:variable name="group" select="floor($upperbound div 2)"/>
			
			<xsl:call-template name="inventory">
				<xsl:with-param name="key" select="$key"/>
				<xsl:with-param name="quantity-per-pallet" select="$quantity-per-pallet"/>
				<xsl:with-param name="upperbound" select="$group"/>
				<xsl:with-param name="count" select="$count"/>
				<xsl:with-param name="top" select="$top"/>
			</xsl:call-template>

			<xsl:call-template name="inventory">
				<xsl:with-param name="key" select="$key"/>
				<xsl:with-param name="quantity-per-pallet" select="$quantity-per-pallet"/>
				<xsl:with-param name="upperbound" select="$group"/>
				<xsl:with-param name="count" select="$count + $group"/>
				<xsl:with-param name="top" select="$top"/>				
			</xsl:call-template>
		</xsl:if>
	</xsl:template>


	<xsl:template name="_inventory">
		<xsl:param name="key"/>
		<xsl:param name="quantity-per-pallet"/>
		<xsl:param name="upperbound"/>
		<xsl:param name="count" select="0"/>
		<xsl:param name="base"/>

		<Pallet count="{$count}" base="{$base}" key="{concat($key, '-', $count + $base)}" name="{$quantity-per-pallet}" qty="{$count * $quantity-per-pallet + $base}"/>

		<xsl:if test="$count * $quantity-per-pallet &lt; $upperbound">
			<xsl:call-template name="_inventory">
				<xsl:with-param name="key" select="$key"/>
				<xsl:with-param name="quantity-per-pallet" select="$quantity-per-pallet"/>
				<xsl:with-param name="upperbound" select="$upperbound"/>
				<xsl:with-param name="count" select="$count + 1"/>
				<xsl:with-param name="base" select="$base"/>
			</xsl:call-template>
		</xsl:if>
	</xsl:template>
</xsl:stylesheet><!-- Stylus Studio meta-information - (c) 2004-2005. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Stackoverflow.xml" userelativepaths="yes" externalpreview="no" url="Stackoverflow.xml" htmlbaseurl="" outputurl="" processortype="msxml4" useresolver="no" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no"/><MapperBlockPosition></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->