xsl-list
[Top] [All Lists]

Re: grouping by x number of elements

2005-07-12 13:09:10

To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com From: David Carlisle 
<davidc(_at_)nag(_dot_)co(_dot_)uk>
Subject: Re: [xsl] grouping by x number of elements
Message-Id: 
<200507081255(_dot_)NAA25768(_at_)penguin(_dot_)nag(_dot_)co(_dot_)uk>

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



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>