[xsl] Recursion Problem

Subject: [xsl] Recursion Problem
From: vsubramanian@xxxxxxxxxx
Date: Tue, 25 May 2004 17:26:12 -0400
Hi all,

I am having a problem with recursion. 
A student can have multiple claims & I am trying to group 6 claims in every
node. I tried to do recursion but, i  lose one node always with every
recursion

============================================================================
=====
My INPUT DOM has the following structure 
============================================================================
=====


<?xml version="1.0" encoding="UTF-8"?>
<data_root>    
    <data>
        <service_list>
            <service>
                <cr_case_id_fk>14</cr_case_id_fk>
                <name>dolly</name>         
                <claim_amount>1.1</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>14</cr_case_id_fk>
                <name>dolly</name>           
                <claim_amount>1.2</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>14</cr_case_id_fk>
                <name>dolly</name>                
                <claim_amount>1.3</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>14</cr_case_id_fk>
                <name>dolly</name>
                <claim_amount>1.4</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>14</cr_case_id_fk>
                <name>dolly</name>
                <claim_amount>1.5</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.1</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.2</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.3</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.4</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.5</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.6</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.7</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>15</cr_case_id_fk>
                <name>maisy</name>
                <claim_amount>2.8</claim_amount>
            </service>
            <service>
                <cr_case_id_fk>16</cr_case_id_fk>
                <name>holly</name>
                <claim_amount>3.1</claim_amount>
            </service>
        </service_list>    
    </data>
</data_root>

============================================================================
=====
The recursive transform i attempted :- 
============================================================================
=====

<?xml version='1.0' encoding='utf-8' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
    <xsl:template match="@*|*">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="service_list" name="serv_classify">
        <xsl:param name="param_key" select="param_key"/>
        <serv_list>
            <xsl:for-each select="service[(position() mod 6)=1]">
                <final_serv_list>
                    
                    <xsl:variable name="key">
                        <xsl:choose>
                            <xsl:when test="$param_key != 0"><xsl:value-of
select="$param_key"/> </xsl:when>
                            <xsl:otherwise><xsl:value-of
select="cr_case_id_fk"/> </xsl:otherwise>
                        </xsl:choose>                        
                    </xsl:variable>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(cr_case_id_fk)=$key">
                            <name><xsl:value-of select="name"/></name>
                            <cr_case_id_fk_1>
                                <xsl:value-of select="cr_case_id_fk"/>
                            </cr_case_id_fk_1>
                            <claim_amount_1>
                                <xsl:value-of select="claim_amount"/>
                            </claim_amount_1>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(following::cr_case_id_fk)=$key">

                            <claim_amount_2>
                                <xsl:value-of
select="following::claim_amount"/>
                            </claim_amount_2>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(preceding-sibling::cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(following::cr_case_id_fk[2])=$key">

                            <claim_amount_3>
                                <xsl:value-of
select="following::claim_amount[2]"/>
                            </claim_amount_3>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(preceding-sibling::cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(following::cr_case_id_fk[3])=$key">

                            <claim_amount_4>
                                <xsl:value-of
select="following::claim_amount[3]"/>
                            </claim_amount_4>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(preceding-sibling::cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(following::cr_case_id_fk[4])=$key">

                            <claim_amount_5>
                                <xsl:value-of
select="following::claim_amount[4]"/>
                            </claim_amount_5>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(preceding-sibling::cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:choose>
                        <xsl:when
test="normalize-space(following::cr_case_id_fk[5])=$key">

                            <claim_amount_6>
                                <xsl:value-of
select="following::claim_amount[5]"/>
                            </claim_amount_6>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="serv_classify">
                                <xsl:with-param name="param_key"
select="normalize-space(preceding-sibling::cr_case_id_fk)"/>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                </final_serv_list>
            </xsl:for-each>
        </serv_list>
    </xsl:template>
</xsl:stylesheet>

============================================================================
=====

OUTPUT I GET
============================================================================
=====

<?xml version="1.0" encoding="UTF-8"?>
<data_root>
    <data>
        <serv_list>
            <final_serv_list>
                <name>dolly</name>
                <cr_case_id_fk_1>14</cr_case_id_fk_1>
                <claim_amount_1>1.1</claim_amount_1>
                <claim_amount_2>1.2</claim_amount_2>
                <claim_amount_3>1.3</claim_amount_3>
                <claim_amount_4>1.4</claim_amount_4>
                <claim_amount_5>1.5</claim_amount_5>
                <serv_list/>
            </final_serv_list>
            <final_serv_list>
                <name>maisy</name>
                <cr_case_id_fk_1>15</cr_case_id_fk_1>
                <claim_amount_1>2.2</claim_amount_1>
                <claim_amount_2>2.3</claim_amount_2>
                <claim_amount_3>2.4</claim_amount_3>
                <claim_amount_4>2.5</claim_amount_4>
                <claim_amount_5>2.6</claim_amount_5>
                <claim_amount_6>2.7</claim_amount_6>
            </final_serv_list>
            <final_serv_list>
                <name>maisy</name>
                <cr_case_id_fk_1>15</cr_case_id_fk_1>
                <claim_amount_1>2.8</claim_amount_1>
                <serv_list/>
                <serv_list/>
                <serv_list/>
                <serv_list/>
                <serv_list/>
            </final_serv_list>
        </serv_list>
    </data>
</data_root>


============================================================================
=====
THE IDEAL OUTPUT
============================================================================
=====

<?xml version="1.0" encoding="UTF-8"?>
<data_root>
    <data>
        <serv_list>
            <final_serv_list>
                <name>dolly</name>
                <cr_case_id_fk_1>14</cr_case_id_fk_1>
                <claim_amount_1>1.1</claim_amount_1>
                <claim_amount_2>1.2</claim_amount_2>
                <claim_amount_3>1.3</claim_amount_3>
                <claim_amount_4>1.4</claim_amount_4>
                <claim_amount_5>1.5</claim_amount_5>
                <serv_list/>
            </final_serv_list>
            <final_serv_list>
                <name>maisy</name>
                <cr_case_id_fk_1>15</cr_case_id_fk_1>
                <claim_amount_1>2.1</claim_amount_1>
                <claim_amount_2>2.2</claim_amount_2>
                <claim_amount_3>2.3</claim_amount_3>
                <claim_amount_4>2.4</claim_amount_4>
                <claim_amount_5>2.5</claim_amount_5>
                <claim_amount_6>2.6</claim_amount_6>
            </final_serv_list>
            <final_serv_list>
                <name>maisy</name>
                <cr_case_id_fk_1>15</cr_case_id_fk_1>
                <claim_amount_1>2.7</claim_amount_1>
                <claim_amount_2>2.8</claim_amount_2>
                <serv_list/>
                <serv_list/>
                <serv_list/>
                <serv_list/>
            </final_serv_list>
            <final_serv_list>
                <name>holly</name>
                <cr_case_id_fk_1>16</cr_case_id_fk_1>
                <claim_amount_1>3.1</claim_amount_1>
                <serv_list/>
                <serv_list/>
                <serv_list/>
                <serv_list/>
                <serv_list/>
            </final_serv_list>
        </serv_list>
    </data>
</data_root>

Current Thread