xsl:for-each-group,条件产生额外输出

时间:2016-01-14 04:58:16

标签: xslt xslt-2.0

我正在努力从xml生成文本输出文件。 XML条目将有一个名为LoanAmt的节点,其值可以是0或大于零。现在,我必须检查两个条件,首先是查看LoanAmt的值是否大于0,然后如果是,我必须总结LoanAmt的条目,如果是唯一的参与者条目。换句话说,如果整个xml中只有一个参与者ID,则显示输出,但如果有多个参与者ID,则应仅填充每个唯一的参与者ID的一个记录,但应将每个唯一的Particpant ID中的LoanAmt汇总起来并显示出来。我的问题是我的xslt工作方式与我想要的相同,但是groupby和if条件不能处理xml的第一个条目,其中Loan Amt不大于零。因此,我得到的第一个条目显示为零金额,此行不应出现。

这不是完整的XML,但我从原来的样本中取样:

<?xml version='1.0' encoding='UTF-8'?>
<Report_Data >
    <Report_Entry>
    <Participant_ID>033</Participant_ID>
    <chk>673.58</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Vana</Last_Name>
        <First_Name>Sag</First_Name>
        <Middle_Name>N</Middle_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>037</Participant_ID>
    <chk>2167.89</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Pilay</Last_Name>
        <First_Name>Saath</First_Name>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>61.81</PreTaxAmt>
    <MatchAmt>61.81</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>043</Participant_ID>
    <chk>819.78</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>ouphan</Last_Name>
        <First_Name>angmala</First_Name>
        <Gender_Code>2</Gender_Code>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>84.19</PreTaxAmt>
    <MatchAmt>84.18</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>049</Participant_ID>
    <chk>2066.54</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Tizili</Last_Name>
        <First_Name>ard</First_Name>
        <Gender_Code>1</Gender_Code>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>136.37</PreTaxAmt>
    <MatchAmt>136.36</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>094</Participant_ID>
    <chk>946.71</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>holphakdy</Last_Name>
        <First_Name>Meya</First_Name>
        <Gender_Code>2</Gender_Code>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>81.25</PreTaxAmt>
    <MatchAmt>81.25</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>104</Participant_ID>
    <chk>1183.3</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Sai</Last_Name>
        <First_Name>Paupl</First_Name>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>234.52</PreTaxAmt>
    <MatchAmt>234.52</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>128.18</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>106</Participant_ID>
    <chk>185.44</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Leve</Last_Name>
        <First_Name>Alen</First_Name>
        <Middle_Name>S</Middle_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>106</Participant_ID>
    <chk>0</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Leve</Last_Name>
        <First_Name>Alen</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>10</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>131</Participant_ID>
    <chk>508.92</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>th</Last_Name>
        <First_Name>Pa</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>
<Report_Entry>
    <Participant_ID>153</Participant_ID>
    <chk>277.2</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Baer</Last_Name>
        <First_Name>Er</First_Name>
        <Middle_Name>D</Middle_Name>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>72.39</PreTaxAmt>
    <MatchAmt>72.38</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>0</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>155</Participant_ID>
    <chk>1250.83</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>Piye</Last_Name>
        <First_Name>Shirit</First_Name>
        <Middle_Name>M</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>96.3</LoanAmt>
</Report_Entry>
<Report_Entry>
    <Participant_ID>170</Participant_ID>
    <chk>0</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>terner</Last_Name>
        <First_Name>san</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>
<Report_Entry>
    <Participant_ID>170</Participant_ID>
    <chk>684.05</chk>
    <Workers>
        <FULL_PART_Time_Indicator>1</FULL_PART_Time_Indicator>
        <Last_Name>terner</Last_Name>
        <First_Name>san</First_Name>
    </Workers>
    <End_Date_from_Pay_Period>2016-01-03-08:00</End_Date_from_Pay_Period>
    <PreTaxAmt>162.31</PreTaxAmt>
    <MatchAmt>162.3</MatchAmt>
    <RothAmt>0</RothAmt>
    <LoanAmt>210.98</LoanAmt>
     </Report_Entry>

我正在使用我用过的xslt:

<?xml version="1.0" encoding="UTF-8"?>

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0" xmlns:functx="http://www.functx.com"  >


   <xsl:variable name="linefeed" select="'&#xA;'"/>
   <xsl:variable name="filler" select="'                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    '"/>
   <xsl:variable name="separator" select="','"/>
   <xsl:variable name="spaces"
   select="concat('          ','          ','          ','          ','          ','          ','          ','          ','          ')"/>




  <xsl:template match="/">

    <xsl:call-template name="LoanDataRecord"/>

  </xsl:template>


  <xsl:template name="LoanDataRecord">

   <xsl:for-each-group select="Report_Data/Report_Entry" group-by="if(  LoanAmt[. &gt;  0 ] ) then Participant_ID else '' " >      
   <xsl:variable name="Var1" select="current-grouping-key()"/> 

    <!-- Plan Number -->
    <xsl:value-of select="substring(concat('768002',$filler),1,6)"/>
        <xsl:value-of select="$separator"/>


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

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

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

</xsl:stylesheet>

768002,033709571,0

768002,104603206,128.18

等等

但我不想看到第一行有0 LoanAmt。

1 个答案:

答案 0 :(得分:0)

问题是<xsl:for-each-group />指令。将其更改为使用谓词:

<xsl:for-each-group select="Report_Data/Report_Entry[LoanAmt &gt; 0 ]" 
                    group-by="Participant_ID">

我使用您的XML对其进行了测试,它的工作原理为:Your updated JSFiddle

顺便说一下,我添加了<xsl:output method="text" />,就像你想要的一样。