Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to re-use a recursive template

pdrapeau

Programmer
Aug 21, 2024
2
0
1
I have the following sums that are conditional to Employee_ID being present in my xml


Code:
        <xsl:variable name="sumTFSA">
            <xsl:apply-templates select="Report_Entry[1]" mode="TFSA"/>   
        </xsl:variable>

        <xsl:variable name="sumRRSP">
            <xsl:apply-templates select="Report_Entry[1]" mode="RRSP"/>   
        </xsl:variable>



Code:
<xsl:template match="Report_Entry" mode="TFSA">
        <xsl:param name="runningTotal" select="number('0')"/>
        <xsl:variable name="contributionTFSA">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/>  
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="CF_INT2007_LFP_Pay_Results_TFSA"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
             <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='TFSA'>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contributionTFSA"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contributionTFSA"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>
    
    <xsl:template match="Report_Entry" mode="RRSP">
        <xsl:param name="runningTotal" select="number('0')"/>
        <xsl:variable name="contribution">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/>  
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="CF_INT2007_LFP_Pay_Results_Optional_RRSP"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
            <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='RRSP'>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contribution"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contribution"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>


Aside from CF_INT2007_LFP_Pay_Results_TFSA and CF_INT2007_LFP_Pay_Results_Optional_RRSP, the two templates (modeS RRSP and RFSA) are identical.
Does anyone have an idea how to use only one template, but feeding from either CF_INT2007_LFP_Pay_Results_TFSA or CF_INT2007_LFP_Pay_Results_Optional_RRSP within the recursive call to itself?
 
I actually found a solution!

Code:
        <xsl:variable name="sumTFSA">
            <xsl:apply-templates select="wd:Report_Entry[1]" mode="calcul">
                <xsl:with-param name="contributionType" select="'wd:CF_INT2007_LFP_Pay_Results_TFSA'"/>
            </xsl:apply-templates>
        </xsl:variable>

        <xsl:variable name="sumRRSP">
            <xsl:apply-templates select="wd:Report_Entry[1]" mode="calcul">
                <xsl:with-param name="contributionType" select="'wd:CF_INT2007_LFP_Pay_Results_Optional_RRSP'"/>
            </xsl:apply-templates>
        </xsl:variable>

          


    <xsl:template match="Report_Entry" mode="calcul">
        <xsl:param name="contributionType"/>
        <xsl:param name="runningTotal"  select="number('0')"/>
        <xsl:variable name="contribution" as="xs:decimal">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/>
                </xsl:when>
                <xsl:otherwise><xsl:evaluate xpath="$contributionType" context-item="."/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
          <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='calcul'>
                    <xsl:with-param name="contributionType" select="$contributionType"/>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contribution"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contribution"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>
I have the following sums that are conditional to Employee_ID being present in my xml


Code:
        <xsl:variable name="sumTFSA">
            <xsl:apply-templates select="Report_Entry[1]" mode="TFSA"/>  
        </xsl:variable>

        <xsl:variable name="sumRRSP">
            <xsl:apply-templates select="Report_Entry[1]" mode="RRSP"/>  
        </xsl:variable>



Code:
<xsl:template match="Report_Entry" mode="TFSA">
        <xsl:param name="runningTotal" select="number('0')"/>
        <xsl:variable name="contributionTFSA">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/> 
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="CF_INT2007_LFP_Pay_Results_TFSA"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
             <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='TFSA'>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contributionTFSA"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contributionTFSA"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>
   
    <xsl:template match="Report_Entry" mode="RRSP">
        <xsl:param name="runningTotal" select="number('0')"/>
        <xsl:variable name="contribution">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/> 
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="CF_INT2007_LFP_Pay_Results_Optional_RRSP"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
            <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='RRSP'>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contribution"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contribution"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>


Aside from CF_INT2007_LFP_Pay_Results_TFSA and CF_INT2007_LFP_Pay_Results_Optional_RRSP, the two templates (modeS RRSP and RFSA) are identical.
Does anyone have an idea how to use only one template, but feeding from either CF_INT2007_LFP_Pay_Results_TFSA or CF_INT2007_LFP_Pay_Results_Optional_RRSP within the recursive call to itself?
I actually found a solution!
Code:
        <xsl:variable name="sumTFSA">
            <xsl:apply-templates select="wd:Report_Entry[1]" mode="calcul">
                <xsl:with-param name="contributionType" select="'wd:CF_INT2007_LFP_Pay_Results_TFSA'"/>
            </xsl:apply-templates>
        </xsl:variable>

        <xsl:variable name="sumRRSP">
            <xsl:apply-templates select="wd:Report_Entry[1]" mode="calcul">
                <xsl:with-param name="contributionType" select="'wd:CF_INT2007_LFP_Pay_Results_Optional_RRSP'"/>
            </xsl:apply-templates>
        </xsl:variable>


    <xsl:template match="Report_Entry" mode="calcul">
        <xsl:param name="contributionType"/>
        <xsl:param name="runningTotal"  select="number('0')"/>
        <xsl:variable name="contribution" as="xs:decimal">
            <xsl:choose>
                <xsl:when test="not(Employee_ID)"><xsl:value-of select="number('0')"/>
                </xsl:when>
                <xsl:otherwise><xsl:evaluate xpath="$contributionType" context-item="."/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
            <xsl:choose>
          <xsl:when test="following-sibling::Report_Entry[1]">
                  <xsl:apply-templates select="following-sibling::Report_Entry[1]" mode='calcul'>
                    <xsl:with-param name="contributionType" select="$contributionType"/>
                    <xsl:with-param name="runningTotal" select="$runningTotal + $contribution"/>
                </xsl:apply-templates>
          </xsl:when>
          <xsl:otherwise>
                <xsl:value-of select="$runningTotal + $contribution"/>
          </xsl:otherwise>
           </xsl:choose>
    </xsl:template>
 

Part and Inventory Search

Sponsor

Back
Top