xsl:for-每个组都有一个产生额外输出的条件
xsl:for-each-group with a condition producing extra output
我正在努力从 xml 生成文本输出文件。 XML 条目将有一个名为 LoanAmt 的节点,其值可以为 0 或大于零。现在,我必须检查两个条件,首先是查看 LoanAmt 的值是否大于 0,然后如果是,我必须对 LoanAmt 的条目求和以获取唯一的 Participant 条目。换句话说,如果整个 xml 中只有一个参与者 ID,则显示输出,但如果有多个参与者 ID,则每个唯一参与者 ID 仅应填充一个记录,但每个唯一参与者中的 LoanAmt ID应该被总结和显示。我的问题是我的 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="'
'"/>
<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[. > 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
等等
但我不想看到 LoanAmt 为 0 的第一行。
问题是 <xsl:for-each-group />
指令。将其更改为使用谓词:
<xsl:for-each-group select="Report_Data/Report_Entry[LoanAmt > 0 ]"
group-by="Participant_ID">
我用你的 XML 测试了它,它有效:http://xsltransform.net/bnnZWu。
顺便说一下,我添加了 <xsl:output method="text" />
,因为这可能是您想要的。
我正在努力从 xml 生成文本输出文件。 XML 条目将有一个名为 LoanAmt 的节点,其值可以为 0 或大于零。现在,我必须检查两个条件,首先是查看 LoanAmt 的值是否大于 0,然后如果是,我必须对 LoanAmt 的条目求和以获取唯一的 Participant 条目。换句话说,如果整个 xml 中只有一个参与者 ID,则显示输出,但如果有多个参与者 ID,则每个唯一参与者 ID 仅应填充一个记录,但每个唯一参与者中的 LoanAmt ID应该被总结和显示。我的问题是我的 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="'
'"/>
<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[. > 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
等等
但我不想看到 LoanAmt 为 0 的第一行。
问题是 <xsl:for-each-group />
指令。将其更改为使用谓词:
<xsl:for-each-group select="Report_Data/Report_Entry[LoanAmt > 0 ]"
group-by="Participant_ID">
我用你的 XML 测试了它,它有效:http://xsltransform.net/bnnZWu。
顺便说一下,我添加了 <xsl:output method="text" />
,因为这可能是您想要的。