Re: [xsl] Processing node-sets in batches

Subject: Re: [xsl] Processing node-sets in batches
From: Jeff Hooker <jeff@xxxxxxxxxxxx>
Date: Mon, 15 Mar 2010 13:37:36 -0700 (PDT)
Hi all,

Couldn't get David's XSLT to work; I did a very sloppy job of
describing my environment and that's likely to blame. I thought the node-set
was basically a generic term used whenever you read set of nodes into a single
variable. I'm working with XSLT 2.0, and the following script achieved my
goal. Obviously it does not recreate valid docbook yet, but I'll do that next.
<xsl:templatematch="/">
<root>
<xsl:call-templatename="rackem"/>
</root>
</xsl:template>

<xsl:templatename="rackem">
<xsl:variablename="setsize">10</xsl:variable> <!-- the size of the sets I want
things broken into -->
<xsl:variablename="x"select="//testnode"></xsl:variable>
<xsl:for-eachselect="$x[(position() mod $setsize) = 1]">
<xsl:variablename="postion"select="position()"></xsl:variable><!-- the
for-each will divide the nodeset off into a number of sets; this is the set
number I'm working on now -->
<table>
<xsl:call-templatename="stackem">
<xsl:with-paramname="set"select="$position"/>
<xsl:with-paramname="total"select="$setsize"></xsl:with-param>
</xsl:call-template>
</table>
</xsl:for-each> 
</xsl:template>
<xsl:templatename="stackem">
<xsl:paramname="set">0</xsl:param>
<xsl:paramname="total">0</xsl:param>
<xsl:paramname="count">1</xsl:param>
<xsl:variablename="p"select="(($set - 1) * $total) + $count"></xsl:variable>
<xsl:variablename="x"select="//node"></xsl:variable>
<xsl:iftest="$count &lt;
($total + 1)">
<row>
<xsl:value-ofselect="$x[$p]"></xsl:value-of>
</row>
<xsl:call-templatename="stackem">
<xsl:with-paramname="set"select="$set"/>
<xsl:with-paramname="count"select="$count + 1"></xsl:with-param>
<xsl:with-paramname="total"select="$total"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>

I've likely used 30 or so more
lines than a clever person would need. Ah well. 
Jeff.



----- Original
Message ----
> From: David Carlisle <davidc@xxxxxxxxx>
> To:
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Cc: Jeff Hooker <jeff@xxxxxxxxxxxx>
> Sent:
Sun, March 7, 2010 4:04:06 PM
> Subject: Re: [xsl] Processing node-sets in
batches
> 
> On 07/03/2010 23:50, Jeff Hooker wrote:
>  so I'm trying to read
all 
> of the nodes into a node-set() and process them out into a series of
100-row 
> tables.

if you need the node-set() extension then probably you
have 
> copied the entire source into a result tree fragment, which might
account for 
> your
memory problems. hard to say. But that may not be what you
mean 
> (node-set() isn't valid xpath syntax so I'm having to guess)

I'm
assuming 
> you are using xslt 1 (you don't say) as xslt 2 doesn't have node
sets.

If 
> $x contains a node set then

<xsl:for-each select="$x[position()
mod 
> 100 = 1]">
<xsl:variable name="p" 
> select="position()"/>
<xsl:for-each select="$x[position()> $p * 
> 100][position()<= ($p+1) * 100]">
will process the nodes in batches 
> of a hundred

David

Current Thread