如何对XML中出现的相同ID的xslt中的金额求和

时间:2016-01-13 21:02:57

标签: xslt-1.0 xslt-2.0

我需要在输出中生成一个文本文件。我必须首先检查perosn是否具有以下节点PreTaxAmt,MatchAmt,RothAmt,然后在输出中生成具有所需值的单行,然后我必须检查perosn是否具有LoanAmt节点,如果他们有,那么我必须为该人创建另一个条目。我能够编码直到这一点,但现在我有一个requiremnet,如果同一个人被多次重复,例如在laurel Alex之下,我需要为单个参与者id和PreTaxAmt,MatchAmt,RothAmt金额生成一个条目应该在输出的第一个条目和贷款条目行中总结LoanAmt应该总结。

这是我的xml。

<Report_Data >
<Report_Entry>
    <Participant_ID>03300</Participant_ID>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Mary</Last_Name>
        <First_Name>Mehul</First_Name>

    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>100.8</PreTaxAmt>
    <MatchAmt>100.8</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>10600</Participant_ID>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Laurel</Last_Name>
        <First_Name>Alex</First_Name>      
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>61.48</PreTaxAmt>
    <MatchAmt>61.47</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>      
</Report_Entry>
<Report_Entry>
    <Participant_ID>10600</Participant_ID>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Laurel</Last_Name>
        <First_Name>Alex</First_Name>
        <Middle_Name>S</Middle_Name>  
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>0</PreTaxAmt>
    <MatchAmt>0</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>  
</Report_Entry>
<Report_Entry>
    <Participant_ID>13100</Participant_ID>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Smita</Last_Name>
        <First_Name>Paul</First_Name>       
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>0</PreTaxAmt>
    <MatchAmt>0</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>

这是我的xslt:

 <xsl:template match="/">

    <xsl:for-each select="Report_Data/Report_Entry">
    <xsl:if test="(PreTaxAmt) or(MatchAmt) or (RothAmt) ">
         <xsl:call-template name="DataRecords"/>
    </xsl:if>
    </xsl:for-each>
    <xsl:for-each select="Report_Data/Report_Entry">
    <xsl:if test="LoanAmt">
        <xsl:call-template name="LoanDataRecord"/>
    </xsl:if>

    </xsl:for-each>

</xsl:template>

<xsl:template name="DataRecords">
    <xsl:for-each-group select="." group-by="wd:Participant_ID">
    <xsl:variable name="ParticpantLoan" select="current-grouping-key()"/> 

!-- Transaction Code -->
<xsl:value-of select="'Contribution'"/>

<!-- Participant Number -->
<xsl:value-of select="Participant_ID,$filler),1,9"/>

<!-- FT/PT Indicator -->
<xsl:value-of select="substring(concat(Workers/FULL_PART_Time_Indicator,$filler),1,1)"/>  

<!-- Contribution Amount1 -->  
<xsl:value-of select="(sum(current-group()/PreTaxAmt))"/>

<!-- Contribution Amount2 -->
<xsl:value-of select="(sum(current-group()/MatchAmt))"/>

<!-- Contribution Amount3 -->
<xsl:value-of select="(sum(current-group()/RothAmt))"/>
<xsl:value-of select="$linefeed"/> 

</xsl:for-each-group>
</xsl:template>     


<xsl:template name="LoanDataRecord">

    <xsl:for-each-group select="." group-by="Participant_ID">
    <xsl:variable name="ParticpantLoan" select="current-grouping-key()"/> 

<!-- Transaction Code -->
<xsl:value-of select="substring(concat('LoanData',$filler),1,3)"/>


<!-- Participant Number -->
<xsl:value-of select="substring(concat(Participant_ID,$filler),1,9)"/>


<!-- Loan Amt -->
<xsl:value-of select="(sum(current-group()/LoanAmt))"/>


<xsl:value-of select="$linefeed"/> 

</xsl:for-each-group>
</xsl:template> 

输出应该有六行。 另外,如何计算输出文件中记录的总数。早些时候我使用下面的代码,但现在因为有重复的SSN我应该如何修改我现有的代码进行计数..  0]或Report_Data / Report_Entry / MatchAmt [number(。)&gt; 0]或Report_Data / Report_Entry / RothAmt [number(。)&gt; 0])+

 count(Report_Data/Report_Entry/LoanAmt[number(.) > 0 ])

0 个答案:

没有答案