Whoops! My solution had an error which meant it also didn't work with
Dimitre's example. My previous example didn't correctly handle the case
where the sum of elements so far was an exact multiple of 10. Recursion can
be tricky! :-)
Here it is corrected:
<xsl:template match="root">
<xsl:copy>
<xsl:call-template name="group-ele">
<xsl:with-param name="ele-list" select="ele"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="group-ele">
<xsl:param name="ele-list" select="/.."/>
<xsl:param name="count" select="0"/>
<xsl:if test="$ele-list">
<xsl:if test="$count = 10">
<br/>
</xsl:if>
<xsl:variable name="first-ele" select="$ele-list[1]"/>
<xsl:variable name="new-count" select="$count mod 10 +
$first-ele/@sum"/>
<xsl:if test="$new-count >= 10">
<br/>
</xsl:if>
<xsl:copy-of select="$first-ele"/>
<xsl:call-template name="group-ele">
<xsl:with-param name="ele-list"
select="$ele-list[position()>1]"/>
<xsl:with-param name="count" select="$new-count"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Cheers
Con
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com]On Behalf Of
Conal Tuohy
Sent: Wednesday, 23 April 2003 09:26
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Grouping problem?
Lars Huttar wrote:
I like it! Interesting way to form a group.
It might be slow for large source documents, maybe order(N*N)
where N is the number of ele elements (because for each element you
have to sum all preceding elements); but I can't see a way
around that... unless you want to recursively loop through
the elements,
keeping a running total.
Like this:
<xsl:template match="root">
<xsl:copy>
<xsl:call-template name="group-ele">
<xsl:with-param name="ele-list" select="ele"/>
</xsl:call-template>
</xsl:copy>
</xsl:template>
<xsl:template name="group-ele">
<xsl:param name="ele-list" select="/.."/>
<xsl:param name="count" select="0"/>
<xsl:if test="$ele-list">
<xsl:variable name="first-ele" select="$ele-list[1]"/>
<xsl:variable name="new-count" select="$count +
$first-ele/@sum"/>
<xsl:if test="$new-count > 10">
<br/>
</xsl:if>
<xsl:copy-of select="$first-ele"/>
<xsl:call-template name="group-ele">
<xsl:with-param name="ele-list"
select="$ele-list[position()>1]"/>
<xsl:with-param name="count"
select="$new-count mod 10"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
As you say, Lars, this approach would probably be a lot
quicker for large
documents.
Cheers!
Con
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list