如何在XSLT中获得总成本?

时间:2015-05-06 16:19:40

标签: xslt xslt-2.0

我需要你的指导就是解决这个xslt问题。一如既往,谢谢你的帮助。

这是我之前问题的扩展,我被要求提供最终结果。但是,由于格式已经改变,并且在另一篇文章中有一些非常好的信息和回复,我不想改变那篇文章。

但是,输入的xml文件已更改。 很抱歉很长的帖子。我认为提供更多信息可能会有用。

XML输入

<Output>         
    <Transaction>
        <TID>T01</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-01-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan A" />            
        <effective_date>2014-01-01-08:00</effective_date>
        <Annual_Cost>6000</Annual_Cost>
    </Transaction>


    <Transaction>
        <TID>T02</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-01-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan A" />            
        <effective_date>2014-10-01-08:00</effective_date>
        <Annual_Cost>12000</Annual_Cost>
    </Transaction>


    <Transaction>
        <TID>T03</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan B" />            
        <effective_date>2014-01-04-08:00</effective_date>
        <Annual_Cost>6000</Annual_Cost>
    </Transaction>


    <Transaction>
        <TID>T04</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan B" />            
        <effective_date>2014-09-04-08:00</effective_date>
        <Annual_Cost>15000</Annual_Cost>
    </Transaction>


    <Transaction>
        <TID>T05</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan B" />            
        <effective_date>2015-06-04-08:00</effective_date>
        <Annual_Cost>18000</Annual_Cost>

    </Transaction>



    <Transaction>
        <TID>T06</TID>
        <Employee_ID>E00002</Employee_ID>
        <Legal_Name Descriptor="Brad Pits" />         
        <lastName>Pits</lastName>
        <firstName>Brad</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan Z" />            
        <effective_date>2012-01-04-08:00</effective_date>

        <Annual_Cost>10000</Annual_Cost>

    </Transaction>

    <Transaction>
        <TID>T07</TID>
        <Employee_ID>E00002</Employee_ID>
        <Legal_Name Descriptor="Brad Pits" />         
        <lastName>Pits</lastName>
        <firstName>Brad</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-12-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan Z" />            
        <effective_date>2013-01-04-08:00</effective_date>
        <end_date>2015-09-30-08:00</end_date>
        <Annual_Cost>15000</Annual_Cost>

    </Transaction>

    <Transaction>
        <TID>T08</TID>
        <Employee_ID>E00001</Employee_ID>
        <Legal_Name Descriptor="John Doe" />         
        <lastName>Doe</lastName>
        <firstName>John</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-01-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan B" />            
        <effective_date>2015-09-04-08:00</effective_date>
        <end_date>2015-11-30-08:00</end_date>
        <Annual_Cost>24000</Annual_Cost>

    </Transaction>

    <Transaction>
        <TID>T09</TID>
        <Employee_ID>E00003</Employee_ID>
        <Legal_Name Descriptor="Tim Baktoo" />         
        <lastName>Baktoo</lastName>
        <firstName>Tim</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-01-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan B" />            
        <effective_date>2010-09-04-08:00</effective_date>
        <end_date>2014-10-31-08:00</end_date>
        <Annual_Cost>24000</Annual_Cost>

    </Transaction>

    <Transaction>
        <TID>T11</TID>
        <Employee_ID>E00003</Employee_ID>
        <Legal_Name Descriptor="Tim Baktoo" />         
        <lastName>Baktoo</lastName>
        <firstName>Tim</firstName>
        <P_From_Date>2015-0-01-08:00</P_From_Date>
        <P_To_Date>2015-01-31-08:00</P_To_Date>                           
        <Plan Descriptor="Plan C" />            
        <effective_date>2016-01-01-08:00</effective_date>
        <end_date>2014-10-31-08:00</end_date>
        <Annual_Cost>24000</Annual_Cost>

    </Transaction>



</Output>

预期输出

<Employees>
  <Employee>
     <ID>E00001</ID>
     <Amount>28586</Amount>
  </Employee>

  <Employee>
     <ID>E00002</ID>
     <Amount>11219</Amount>
  </Employee>

  <Employee>
     <ID>E00004</ID>
     <Amount>12000</Amount>
  </Employee>


</Employee>

其他信息

员工交易未必订购。             我无法控制文件。因此,为员工制定计划的交易             对于不同的有效日期不一定在一起或按升序或降序排列             生效日期。           2交易针对不同的计划,并在特定日期生效           3.生效日期表示该转换对特定计划的员工有效的日期。           4.交易从effective_date到下一个有效              交易生效日期(如果有的话),如果交易有结束日期,可以提前结束           5.交易可以在P_From_Date之前或P_To_Date之后生效。          6.类似地,End_Date可以在P_From_Date之前或在P_To_Date之后。

      Objective
      *********
      Determine the total amount for each employee for the period between P_From_Date and P_To_Date. 

      See expected output.

      Other Notes


      1. The total cost is to be determined for the period between P_From_Date and P_To_Date.
      2. The general formula to derive the cost for each transaction for a plan for an employee is as follows
           No of days div 365 or 366 for leap year * total_cost.
           Use P_To_Date for leap year determination.

           This will be added up for that employee to determine employee total cost.
      2. If there are trasactions with effective date before the p_from date, they will not impact the total cost
         unless that transaction is effective for part of the period between P_From_Date and P_To_Date. 
         If the end date of this transaction falls before the p_From_Date, then this transaction will also 
         not impact the cost as it is no longer effective.
      3. Transactions with effective_date between P_From_Date and P_To_Date will be included in determining the total cost as follows
          1. The No Of Days for which the cost will be determines is as follows.
             From date - will be the effective date of the transaction.
             To Date -  If the transaction has an end_date and the end date falls after P_to_Date, then P_To_Date
                        else, end_date

                        If the transaction does not have an end date But there is another transaction for same employee and plan
                        with an effective date grater than the effective date of this transaction and the effective date of
                        that other transction falls after then P_To_Date, the To Date will be P_To-Date. Else it is the
                         effective date of that other transaction - 1 day.

                        Period (No Of Days) = To date - From date + 1day.                       

      4. Similarly, if the end date of the transaction falls after the P_To-Date, then that end date is not relevant
        in the computation of the period as the employee will be charged upto p_to_date as long as the transaction is effective during
        the P_From_Date and P_To-Date

1 个答案:

答案 0 :(得分:0)

我投票决定将这个问题视为过于宽泛,但我想给你一个起点。以下样式表:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/Output">
    <Employees>
        <xsl:for-each-group select="Transaction" group-by="Employee_ID">
            <Employee>
                <ID>
                    <xsl:value-of select="current-grouping-key()"/>
                </ID>
                <Amount>
                    <xsl:value-of select="sum(current-group()/Annual_Cost)"/>
                </Amount>
            </Employee>
        </xsl:for-each-group>
    </Employees>  
</xsl:template>

</xsl:stylesheet>

当应用于您的输入时,将返回:

<?xml version="1.0" encoding="UTF-8"?>
<Employees>
   <Employee>
      <ID>E00001</ID>
      <Amount>81000</Amount>
   </Employee>
   <Employee>
      <ID>E00002</ID>
      <Amount>25000</Amount>
   </Employee>
   <Employee>
      <ID>E00003</ID>
      <Amount>48000</Amount>
   </Employee>
</Employees>

您可以通过两种方式继续前进:

  1. 不是对金额求和,而是将引用Annual_Cost(以及可能的事务中的其他节点)的计算相加。例如,您可以使用:

    <Amount>
        <xsl:value-of select="sum(current-group()/(2 * Annual_Cost))"/>
    </Amount>
    

    将值加倍;

  2. 如果计算过于复杂,您可以将事务初步传递给变量,计算您希望汇总的实际金额;然后按员工对结果节点进行分组,如上所示。