该组的多个记录中的字段总和

时间:2018-06-27 14:22:56

标签: xslt

我尝试采用已删除数量和添加数量的差值来报告下面示例中调整的数量。我在两个不同的记录中获得金额字段。我想从记录1减去记录1的金额1与记录2的金额2,如果两者都匹配的代码之一是已删除的记录,而另一个是添加的记录。 并仅打印带有计算量的添加记录。

这是我的输入xml

    <employee>
        <Compensation_One_Time_Payment isDeleted="1">
            <External_Payroll_Code>6090</External_Payroll_Code>
            <Earning_or_Deduction>E</Earning_or_Deduction>
            <Amount>100</Amount>
        </Compensation_One_Time_Payment>
        <Compensation_One_Time_Payment isAdded="1">
            <External_Payroll_Code>6090</External_Payroll_Code>
            <Earning_or_Deduction>E</Earning_or_Deduction>
            <Amount>200</Amount>
        </Compensation_One_Time_Payment>
        <Compensation_One_Time_Payment isAdded="1">
            <External_Payroll_Code>1111</External_Payroll_Code>
            <Earning_or_Deduction>E</Earning_or_Deduction>
            <Amount>300</Amount>
        </Compensation_One_Time_Payment>
    </employee>


My xslt is   

                                                                                                                                            删除                                                                                                                              加                                                                    

        <xsl:for-each-group select="$OTP_Group/root/OTP" group-by='concat(External_Payroll_Code,Action)'>
            <xsl:variable name="AdjustedAmount">
                <xsl:value-of select="sum(OTP[Action='Add']/Amount) - sum(OTP[Action='Delete']/Amount)"/>
            </xsl:variable>
            <OTP>
            <code>
                <xsl:value-of select="External_Payroll_Code"/> 
            </code>
            <NewAmount>
                <xsl:value-of select="$AdjustedAmount"/>
            </NewAmount>
            </OTP>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

请告知。

我当前的输出是。输入每个代码将包含多个记录,我想计算每个代码的金额。但是对于按代码分组,因为我已经在每个分组中了,所以我现在无法查看同一组中的其他记录。

当前输出:

<OTP>
   <code>6090</code>
   <NewAmount>0</NewAmount>
</OTP>
<OTP>
   <code>6090</code>
   <NewAmount>0</NewAmount>
</OTP>
<OTP>
   <code>1111</code>
   <NewAmount>0</NewAmount>
</OTP>

预期产量

 <OTP>
       <code>6090</code>
       <NewAmount>100</NewAmount>
    </OTP>
    <OTP>
       <code>1111</code>
       <NewAmount>300</NewAmount>
    </OTP>

1 个答案:

答案 0 :(得分:0)

您应该仅将External_Payroll_Code分组(因为您希望每个员工一个OTP),因此xsl:for-each-group应该像这样...

<xsl:for-each-group select="$OTP_Group/root/OTP" group-by='External_Payroll_Code'>

另外,要获取总数,您需要对当前组中的元素求和

<xsl:value-of select="sum(current-group()[Action='Add']/Amount) - sum(current-group()[Action='Delete']/Amount)"/>

但是,您实际上不需要在这里创建变量。我看不到添加Action节点的好处,因为您仍然可以轻松地查询xsl:for-each-group中的相关属性。

尝试使用此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs " version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:template match="employee">
        <xsl:for-each-group select="Compensation_One_Time_Payment" group-by='External_Payroll_Code'>
            <OTP>
            <code>
                <xsl:value-of select="External_Payroll_Code"/> 
            </code>
            <NewAmount>
                <xsl:value-of select="sum(current-group()[@isAdded='1']/Amount) - sum(current-group()[@isDeleted='1']/Amount)"/>
            </NewAmount>
            </OTP>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>