Re: [xsl] grouping by x number of elements

Subject: Re: [xsl] grouping by x number of elements
From: "kevin p" <kpear1@xxxxxxxxxxx>
Date: Tue, 12 Jul 2005 20:09:10 +0000
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx From: David Carlisle <davidc@xxxxxxxxx>
Subject: Re: [xsl] grouping by x number of elements
Message-Id: <200507081255.NAA25768@xxxxxxxxxxxxxxxxx>

Iv'e been able to test position at the record level and do something every 5 records or whatever, but basically once I hit that position of 65,535, I want to re-write a new worksheet tag,

beware the T-word. XSLT has no access to the tags in the source document
and can not generate tags in the result.

You want to generate a Worksheet node every 65,535 records so that's
something like
<xsl:for-each select="record[position() mod 65535 = 1]">
<Worksheet ss:name="{$WORKSHEET}">


The problem here is that there has to be a closing worksheet tag, so i have to process all the rows recursively, but when i do that I can't specifiy over approximately 500 rows as a split point, or I run out of memory basically, under saxon, xalan, etc. Here's my new style sheet, but I'm sure I'm needlessly recursing, but I couldn't figure out how else to create the worksheet names cat1, cat2, cat3, dog1, dog2, dog3, etc. as i'm calling into the group without the COUNT parameter being supplied back to itself in group mode.


<?xml version="1.0" encoding="UTF-8"?>
<?mso-application ="Excel.Sheet"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:variable name="GROUPSIZE">500</xsl:variable>
<xsl:template match="/">
<Workbook>
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author/>
<LastAuthor/>
<Created/>
<Company/>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
</ExcelWorkbook>
<xsl:for-each select="/workbook/filename">
<xsl:apply-templates select="document(.)/DATA_RESULT">
<xsl:with-param name="FILE" select=".">
</xsl:with-param>
</xsl:apply-templates>
</xsl:for-each>
</Workbook>
</xsl:template>



<xsl:template match="DATA_RESULT"> <xsl:param name="FILE"/> <xsl:variable name="WORKSHEET"> <xsl:if test="substring($FILE, string-length($FILE) -3) = '.xml'" > <xsl:value-of select="substring($FILE, 1, string-length($FILE) -4)"> </xsl:value-of> </xsl:if> </xsl:variable> <xsl:call-template name="worksheet"> <xsl:with-param name="WSBASE" select="$WORKSHEET"/> </xsl:call-template> </xsl:template>


<xsl:template name="worksheet"> <xsl:param name="WSBASE"/> <xsl:variable name="POSITION" select="position()"/> <xsl:apply-templates select="record[1]" mode="group"> <xsl:with-param name="WSBASE" select="$WSBASE" /> <xsl:with-param name="COUNT" select="$POSITION"/> </xsl:apply-templates> </xsl:template>


<xsl:template match="record" mode="group">
<xsl:param name="WSBASE" />
<xsl:param name="COUNT" />
<Worksheet>
<xsl:attribute name="ss:Name">
<xsl:value-of select="concat($WSBASE, $COUNT)"/>
</xsl:attribute>
<Table>
<xsl:apply-templates select="ancestor-or-self::record" mode="item"/>
</Table>
</Worksheet>
<!--xsl:value-of select="following-sibling::record[500]" /-->
<xsl:apply-templates select="following-sibling::record[500]" mode="group">
<xsl:with-param name="WSBASE" select="$WSBASE" />
<xsl:with-param name="COUNT" select="$COUNT + 1" />
</xsl:apply-templates>
</xsl:template>




	<xsl:template match="record" mode="item">
		<xsl:param name="remaining" select="$GROUPSIZE - 1"/>
		<Row>
		<xsl:for-each select="*">
			<xsl:call-template name="cell"/>
		</xsl:for-each>
		</Row>
		<xsl:if test="$remaining">
			<xsl:apply-templates select="following-sibling::record[1]" mode="item" >
			<xsl:with-param name="remaining" select="$remaining -1" />
			</xsl:apply-templates>
		</xsl:if>
 </xsl:template>


<xsl:template name="cell"> <Cell> <xsl:if test='number(.)'> <Data ss:Type="Number"> <xsl:value-of select="."/> </Data> </xsl:if> <xsl:if test="not(number(.))"> <Data ss:Type="String"> <xsl:if test="(.) != 'NULL'"> <xsl:value-of select="."/> </xsl:if> </Data> </xsl:if> </Cell> </xsl:template>


</xsl:stylesheet>





kp


Current Thread