从先前的for-each语句中获取变量

时间:2018-11-30 15:25:06

标签: xml xslt xslt-2.0

我无所不在,并且已经使用XSLT代码玩了几天,但我只是想不通。我将尽力解释,因为这是一个复杂的问题。该文件必须按科目和部门进行汇总,然后有一个偏移行以按部门进行汇总(如下输出所示)。

wd:Earning_Code ='FLSA-01'具有不正确的 wd:Department wd:Earning_Code ='FLSA-01'交易金额应重新绑定到 wd:Earning_Code ='OT'。问题在于旧约收入代码上可能有多个部门。

我必须将 FLSA 的总收入和加班的总收入 Amount 除以职位,然后除以OT的总工时由职位。然后,我将金额除以小时数以获得小时费率,该费率乘以旧时费率乘以相应的部门。所有这些都适用于按科目和部门汇总,而不适用于按部门部分汇总。我在下面尽了最大的努力简化了XML和XSL。

下面是我的XML:

<?xml version='1.0' encoding='UTF-8'?>
<wd:Report_Data
    xmlns:wd="urn:com.workday.report/bsvc">
    <wd:Report_Entry>
        <wd:Emplid>831186318</wd:Emplid>
        <wd:Ledger_ID>2325</wd:Ledger_ID>
        <wd:Earning_Code>OT</wd:Earning_Code>
        <wd:Total_PayLine_hours>8.25</wd:Total_PayLine_hours>
        <wd:Position>P111827</wd:Position>
        <wd:Transaction_Amount>203.61</wd:Transaction_Amount>
        <wd:Department>2002000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>831186318</wd:Emplid>
        <wd:Ledger_ID>6767</wd:Ledger_ID>
        <wd:Earning_Code>SALARY</wd:Earning_Code>
        <wd:Total_PayLine_hours>8.25</wd:Total_PayLine_hours>
        <wd:Position>P111827</wd:Position>
        <wd:Transaction_Amount>100</wd:Transaction_Amount>
        <wd:Department>2002000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>831186318</wd:Emplid>
        <wd:Ledger_ID>2325</wd:Ledger_ID>
        <wd:Earning_Code>FLSA-01</wd:Earning_Code>
        <wd:Total_PayLine_hours>8.25</wd:Total_PayLine_hours>
        <wd:Position>P111827</wd:Position>
        <wd:Transaction_Amount>132.79</wd:Transaction_Amount>
        <wd:Department>2002000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>342441735</wd:Emplid>
        <wd:Ledger_ID>2387</wd:Ledger_ID>
        <wd:Earning_Code>OT</wd:Earning_Code>
        <wd:Total_PayLine_hours>2</wd:Total_PayLine_hours>
        <wd:Position>P114386</wd:Position>
        <wd:Transaction_Amount>86.8</wd:Transaction_Amount>
        <wd:Department>2002000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>342441735</wd:Emplid>
        <wd:Ledger_ID>2387</wd:Ledger_ID>
        <wd:Earning_Code>OT</wd:Earning_Code>
        <wd:Total_PayLine_hours>8</wd:Total_PayLine_hours>
        <wd:Position>P114386</wd:Position>
        <wd:Transaction_Amount>347.2</wd:Transaction_Amount>
        <wd:Department>2049000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>342441735</wd:Emplid>
        <wd:Ledger_ID>2387</wd:Ledger_ID>
        <wd:Earning_Code>FLSA-01</wd:Earning_Code>
        <wd:Total_PayLine_hours>10</wd:Total_PayLine_hours>
        <wd:Position>P114386</wd:Position>
        <wd:Transaction_Amount>311.44</wd:Transaction_Amount>
        <wd:Department>2943000</wd:Department>
    </wd:Report_Entry>
    <wd:Report_Entry>
        <wd:Emplid>342441735</wd:Emplid>
        <wd:Ledger_ID>6767</wd:Ledger_ID>
        <wd:Earning_Code>SALARY</wd:Earning_Code>
        <wd:Total_PayLine_hours>10</wd:Total_PayLine_hours>
        <wd:Position>P114386</wd:Position>
        <wd:Transaction_Amount>200</wd:Transaction_Amount>
        <wd:Department>2049000</wd:Department>
    </wd:Report_Entry>
</wd:Report_Data>

下面是我的XSL:

    <?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:wd="urn:com.workday.report/bsvc">
        <xsl:output indent="no" method="text"/>
        <xsl:template match="/wd:Report_Data">
            <header>
                <xsl:text>Account</xsl:text><xsl:text>|</xsl:text>
                <xsl:text>Department</xsl:text><xsl:text>|</xsl:text>
                <xsl:text>Amount</xsl:text>
                <xsl:text>&#xA;</xsl:text>
            </header>
         <xsl:for-each-group select="wd:Report_Entry[ wd:Earning_Code != 'FLSA-01']" group-by="wd:Department">
                        <xsl:variable name="Department" select="current-grouping-key()"/>
                        <xsl:for-each-group select="current-group()" group-by="wd:Ledger_ID">
                            <!-- Ledger ID --><xsl:value-of select="current-grouping-key()" /><xsl:text>|</xsl:text>
                            <!-- Dept ID --><xsl:value-of select="$Department"/><xsl:text>|</xsl:text>

                            <!-- The Department on the wd:Earning_Code = 'FLSA-01' is incorrect. I will sum FLSA and OT amount, then divide by the OT hours. Tie back using OT department.-->

                            <!-- Sum of OT hours by Position -->
                            <xsl:variable name="SumOfOvertimeHours" select="sum(../wd:Report_Entry[wd:Earning_Code = 'OT'  and wd:Position = current-group()/wd:Position]/wd:Total_PayLine_hours)"/>

                            <!-- To get the Sum Amount of OT and FLSA by position-->
                            <xsl:variable name="OverTimeandFLSAAmount" select="sum(../wd:Report_Entry[(wd:Ledger_ID = '2325' or wd:Ledger_ID  ='2387') and (wd:Position = current-group()/wd:Position) ]/wd:Transaction_Amount )"/>

                            <!-- To get Hourly Rate for all of Overtime and FLSA-->
                            <xsl:variable name="OTHourlyRate" select="$OverTimeandFLSAAmount div $SumOfOvertimeHours"/>

                            <!-- To get the Sum of OT and FLSA to tie Department. Use Hourly rate * hours from OT-->
                            <xsl:variable name="LedgerAmount" select="format-number($OTHourlyRate * sum(current-group()[wd:Earning_Code = 'OT']/wd:Total_PayLine_hours),'##.00')"/>
                            <xsl:value-of select="$LedgerAmount"/>
                            <xsl:text>&#xA;</xsl:text>
                        </xsl:for-each-group>    

    <!-- *****************************************************Offset************************************************************************************** -->                
                                <!-- Ledger ID --><xsl:text>Balance</xsl:text><xsl:text>|</xsl:text>
                                <!-- Dept ID --><xsl:value-of select="$Department"/><xsl:text>|</xsl:text>

                                 <!-- Sum of OT hours by Position -->
                                 <xsl:variable name="SumOfOTHours" select="sum(../wd:Report_Entry[wd:Earning_Code = 'OT'  and wd:Position = current-group()/wd:Position]/wd:Total_PayLine_hours)"/>

                                <!-- Sum of OT and FLSA amount by Position -->
                                <xsl:variable name="SumOfOTandFLSAAmount" select="sum(../wd:Report_Entry[( (wd:Ledger_ID = '2387' or wd:Ledger_ID = '2325' )  and wd:Position = current-group()/wd:Position)]/wd:Transaction_Amount)"/>

                                <!-- To get Hourly Rate for all of Overtime and FLSA-->
                                <xsl:variable name="OTHourlyRate" select="$SumOfOTandFLSAAmount div $SumOfOTHours"/>

                                <!-- To get the Sum of OT and FLSA to tie Department. Use Hourly rate * hours from OT-->
                                <xsl:value-of select="number(format-number($OTHourlyRate * sum(../wd:Report_Entry[wd:Earning_Code = 'OT' and wd:Department = $Department ]/wd:Total_PayLine_hours),'##.00'))"/>

                                <xsl:text>&#xA;</xsl:text>               
                    </xsl:for-each-group>  
        </xsl:template>   
    </xsl:stylesheet>

这是我的输出。 2002000部门余额不合适。应该是585.49而不是607.61

    Account|Department|Amount
    2325|2002000|336.40
    2387|2002000|149.09
    Balance|2002000|607.61
    2387|2049000|596.35
    Balance|2049000|596.35

是否可以仅从父for-each语句中复制 LedgerAmount 变量并按部门进行匹配?还是有另一种方法?

非常感谢任何帮助!!! 谢谢-Remo

1 个答案:

答案 0 :(得分:1)

正如我首先评论的那样,从嵌套分组中存储数据的一种方法是使用某种XML结构来存储它,而不是直接将其作为文本输出,然后您可以轻松地对中间XML结构中的数据使用sum并且当然也输出其他数据:

<?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:wd="urn:com.workday.report/bsvc">
    <xsl:output indent="no" method="text"/>
    <xsl:template match="/wd:Report_Data">
        <header>
            <xsl:text>Account</xsl:text><xsl:text>|</xsl:text>
            <xsl:text>Department</xsl:text><xsl:text>|</xsl:text>
            <xsl:text>Amount</xsl:text>
            <xsl:text>&#xA;</xsl:text>
        </header>
     <xsl:for-each-group select="wd:Report_Entry[ wd:Earning_Code != 'FLSA-01']" group-by="wd:Department">
                    <xsl:variable name="Department" select="current-grouping-key()"/>

                    <xsl:variable name="groups">
                        <xsl:for-each-group select="current-group()" group-by="wd:Ledger_ID">

                            <group>
                                <ledger-id>
                                    <xsl:value-of select="current-grouping-key()"/>
                                </ledger-id>
                                <department>
                                    <xsl:value-of select="$Department"/>
                                </department>


                            <!-- The Department on the wd:Earning_Code = 'FLSA-01' is incorrect. I will sum FLSA and OT amount, then divide by the OT hours. Tie back using OT department.-->

                            <!-- Sum of OT hours by Position -->
                            <xsl:variable name="SumOfOvertimeHours" select="sum(../wd:Report_Entry[wd:Earning_Code = 'OT'  and wd:Position = current-group()/wd:Position]/wd:Total_PayLine_hours)"/>

                            <!-- To get the Sum Amount of OT and FLSA by position-->
                            <xsl:variable name="OverTimeandFLSAAmount" select="sum(../wd:Report_Entry[(wd:Ledger_ID = '2325' or wd:Ledger_ID  ='2387') and (wd:Position = current-group()/wd:Position) ]/wd:Transaction_Amount )"/>

                            <!-- To get Hourly Rate for all of Overtime and FLSA-->
                            <xsl:variable name="OTHourlyRate" select="$OverTimeandFLSAAmount div $SumOfOvertimeHours"/>

                            <!-- To get the Sum of OT and FLSA to tie Department. Use Hourly rate * hours from OT-->
                            <xsl:variable name="LedgerAmount" select="format-number($OTHourlyRate * sum(current-group()[wd:Earning_Code = 'OT']/wd:Total_PayLine_hours),'##.00')"/>
                            <xsl:value-of select="$LedgerAmount"/>
                               <ledger-amount>
                                   <xsl:value-of select="$LedgerAmount"/>
                               </ledger-amount>
                            </group>

                        </xsl:for-each-group>    
                    </xsl:variable>
                    <xsl:value-of select="$groups/group/string-join(*, '|')" separator="&#10;"/>
                    <xsl:text>&#10;</xsl:text>

<!-- *****************************************************Offset************************************************************************************** -->                
                            <!-- Ledger ID --><xsl:text>Balance</xsl:text><xsl:text>|</xsl:text>
                            <!-- Dept ID --><xsl:value-of select="$Department"/><xsl:text>|</xsl:text>

                             <!-- Sum of OT hours by Position -->
                             <xsl:variable name="SumOfOTHours" select="sum(../wd:Report_Entry[wd:Earning_Code = 'OT'  and wd:Position = current-group()/wd:Position]/wd:Total_PayLine_hours)"/>

                            <!-- Sum of OT and FLSA amount by Position -->
                            <xsl:variable name="SumOfOTandFLSAAmount" select="sum(../wd:Report_Entry[( (wd:Ledger_ID = '2387' or wd:Ledger_ID = '2325' )  and wd:Position = current-group()/wd:Position)]/wd:Transaction_Amount)"/>

                            <!-- To get Hourly Rate for all of Overtime and FLSA-->
                            <xsl:variable name="OTHourlyRate" select="$SumOfOTandFLSAAmount div $SumOfOTHours"/>

                            <!-- To get the Sum of OT and FLSA to tie Department. Use Hourly rate * hours from OT-->
                            <xsl:value-of select="sum($groups/group/ledger-amount)"/>

                            <xsl:text>&#xA;</xsl:text>               
                </xsl:for-each-group>  
    </xsl:template>   
</xsl:stylesheet> 

https://xsltfiddle.liberty-development.net/pPqsHUw上的在线示例。