2017-01-12 42 views
0

我有個問題想辦法分組金額。我需要做兩種方法。基於變量信息在XSLT中對節點進行分組

-I-。第一種方法是指當我沒有多個費用時的情況。 (完成後)。輸入xml信息。

 <ListOfAFGFINSApplicationIncomeandExpensesFormFill> 
      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Rental Expense</AFGSubCategory> 
       <Amount>2000</Amount> 
       <Frequency>Quarterly</Frequency> 
      </AFGFINSApplicationIncomeandExpenses> 

      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Child Maintenance Paid</AFGSubCategory> 
       <Amount>1000</Amount> 
       <Frequency>4 Weekly</Frequency>      
      </AFGFINSApplicationIncomeandExpenses> 

     </ListOfAFGFINSApplicationIncomeandExpensesFormFill> 

我使用這個XSLT代碼:

<xsl:template match="ListOfAFGFINSApplicationIncomeandExpensesFormFill/AFGFINSApplicationIncomeandExpenses 
    [AFGSubCategory = ('Child Maintenance Paid', 'Rental Expense')]" mode="OtherCommitment"> 

    <OtherCommitment 
     UniqueID="{fn:UniqueID(Id)}"> 

     <xsl:attribute name="Amount"> 
      <xsl:apply-templates select="." mode="Amount"/> 
     </xsl:attribute> 

     <xsl:attribute name="Frequency" select="fn:IncomeFrequency(Frequency)"/> 

     <xsl:attribute name="Category"> 
      <xsl:choose> 
       <xsl:when test="AFGSubCategory = 'Child Maintenance Paid'">Child Maintenance</xsl:when> 
       <xsl:when test="AFGSubCategory = 'Rental Expense'">Rent</xsl:when> 
      </xsl:choose> 
     </xsl:attribute> 

    </OtherCommitment> 

要計算的金額我用下面的模板:

<xsl:template match="node()" mode="Amount"> 
    <xsl:choose> 
     <xsl:when test="Frequency = '4 Weekly'"> 
      <xsl:value-of select="(Amount * 13) div 12"/> 
     </xsl:when> 
     <xsl:when test="Frequency = 'Quarterly'"> 
      <xsl:value-of select="Amount * 4"/> 
     </xsl:when> 
     <xsl:when test="Frequency != ''"> 
      <xsl:value-of select="Amount"/> 
     </xsl:when> 
    </xsl:choose> 
</xsl:template> 

對於我使用的頻率:

<xsl:function name="fn:IncomeFrequency"> 
    <xsl:param name="Frequency"/> 
    <xsl:choose> 
     <xsl:when test="$Frequency = '4 Weekly'">Monthly</xsl:when> 
     <xsl:when test="$Frequency = 'Annually'">Yearly</xsl:when> 
     <xsl:when test="$Frequency = 'Quarterly'">Yearly</xsl:when> 
     <xsl:when test="$Frequency != ''"> 
      <xsl:value-of select="$Frequency"/> 
     </xsl:when> 
     <xsl:when test="$Frequency/../AFGAnnualAmount != ''">Yearly</xsl:when> 
    </xsl:choose> 
</xsl:function> 

這是對於這種情況的輸出重刑是正確的:

  <OtherCommitment Amount="8000" Frequency="Yearly" 
       Category="Rent"> 
     </OtherCommitment> 
     <OtherCommitment Amount="1083.3333333333333" 
          Frequency="Monthly" 
          Category="Child Maintenance"> 
     </OtherCommitment> 

-II-第二種情況是,當我有更多然後一個子類別使用相同的名稱,我需要變換分析在「每月」值的頻率。輸入的
示例:

 <ListOfAFGFINSApplicationIncomeandExpensesFormFill> 
      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Rental Expense</AFGSubCategory> 
       <Amount>2000</Amount> 
       <Frequency>Quarterly</Frequency> 
      </AFGFINSApplicationIncomeandExpenses> 

      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Rental Expense</AFGSubCategory> 
       <Amount>1000</Amount> 
       <Frequency>Mounthly</Frequency> 
      </AFGFINSApplicationIncomeandExpenses> 

      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Child Maintenance Paid</AFGSubCategory> 
       <Amount>1000</Amount> 
       <Frequency>4 Weekly</Frequency>      
      </AFGFINSApplicationIncomeandExpenses> 

     </ListOfAFGFINSApplicationIncomeandExpensesFormFill> 

輸出需要變得。

 <OtherCommitment Amount="1666.6666666666667" Frequency="Monthly" 
      Category="Rent"> 
    </OtherCommitment> 
    <!-- This will keep the first approach because is only one 'Child Maintenance'--> 
    <OtherCommitment Amount="1083.3333333333333" 
         Frequency="Monthly" 
         Category="Child Maintenance"> 
    </OtherCommitment> 

爲了計算金額,我需要在這種情況下可能是使用:

<xsl:template match="node()" mode="AmountMonthly"> 
    <xsl:choose> 
     <xsl:when test="Frequency = 'Weekly'"> 
      <xsl:value-of select="fn:remove-scientific-notation((Amount * 52) div 12)"/> 
     </xsl:when> 
     <xsl:when test="Frequency = 'Fortnightly'"> 
      <xsl:value-of select="fn:remove-scientific-notation((Amount * 26) div 12)"/> 
     </xsl:when> 
     <xsl:when test="Frequency = '4 Weekly'"> 
      <xsl:value-of select="fn:remove-scientific-notation((Amount * 13) div 12)"/> 
     </xsl:when> 
     <xsl:when test="Frequency = 'Monthly'"> 
      <xsl:value-of select="Amount"/> 
     </xsl:when> 
     <xsl:when test="Frequency = 'Quarterly'"> 
      <xsl:value-of select="fn:remove-scientific-notation((Amount * 4) div 12)"/> 
     </xsl:when> 
     <xsl:when test="Frequency = 'Annually'"> 
      <xsl:value-of select="fn:remove-scientific-notation(Amount div 12)"/> 
     </xsl:when> 
    </xsl:choose> 
</xsl:template> 

但我不能搞清楚,如何總結的費用。

回答

0

您已將該標籤標記爲XSLT 2.0,因此按類別使用xsl:for-each-group group-by進行分組。我有一個小例子來只處理量,我希望你能在現有的代碼將它集成:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
    xmlns:mf="http://example.com/mf" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs mf"> 


    <xsl:function name="mf:monthlyAmount" as="xs:decimal"> 
     <xsl:param name="amountEl" as="element(AFGFINSApplicationIncomeandExpenses)"/> 
     <xsl:variable name="amount" as="xs:decimal" select="xs:decimal($amountEl/Amount)"/> 
     <xsl:sequence select="if ($amountEl/Frequency = 'Weekly') 
           then $amount * 52 div 12 
           else if ($amountEl/Frequency = 'Quarterly') 
           then $amount * 4 div 12 
           else if ($amountEl/Frequency = 'Monthly') 
           then $amount 
           else 0"/> 
    </xsl:function> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="ListOfAFGFINSApplicationIncomeandExpensesFormFill"> 
     <xsl:for-each-group select="AFGFINSApplicationIncomeandExpenses" group-by="AFGSubCategory"> 
      <OtherCommitment Amount="{sum(for $item in current-group() return mf:monthlyAmount($item))}"/> 
     </xsl:for-each-group> 
    </xsl:template> 
</xsl:transform> 

變換

<ListOfAFGFINSApplicationIncomeandExpensesFormFill> 
      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Rental Expense</AFGSubCategory> 
       <Amount>2000</Amount> 
       <Frequency>Quarterly</Frequency> 
      </AFGFINSApplicationIncomeandExpenses> 

      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Rental Expense</AFGSubCategory> 
       <Amount>1000</Amount> 
       <Frequency>Monthly</Frequency> 
      </AFGFINSApplicationIncomeandExpenses> 

      <AFGFINSApplicationIncomeandExpenses> 
       <AFGSubCategory>Child Maintenance Paid</AFGSubCategory> 
       <Amount>1000</Amount> 
       <Frequency>4 Weekly</Frequency>      
      </AFGFINSApplicationIncomeandExpenses> 

     </ListOfAFGFINSApplicationIncomeandExpensesFormFill> 

<OtherCommitment Amount="1666.666666666666666667"/><OtherCommitment Amount="0"/> 

,所以我覺得對於第一組計算是正確的,您需要添加其他屬性,並且需要爲各種數量類型添加其他計算。

示例在線是http://xsltransform.net/3NSSEvq

+0

我已經解決了這個問題,就像你說的xsl:for-each-group group-by一樣。謝謝您的回覆。 – DanielCSD

相關問題