2017-01-12 10 views
0

金額をグループ化する方法がわかりません。私は2つのアプローチをする必要があります。可変情報に基づいてXSLTのノードをグループ化する

-I-。最初のアプローチは、私が複数の経費を持っていない状況を指しています。 (これは完了です)。入力XML情報。私が使用している周波数について

<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: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> 

iは以下のテンプレートを使用してい金額を計算するには、次の

 <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: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-私は同じ名前を持つ以上、その後1つのサブカテゴリを持っていると私は「月刊」の値に周波数をtranformする必要がある場合に、第2の状況です。入力の
例:

 <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グループバイと同じように問題を修正しました。ご返信ありがとうございます。 – DanielCSD

関連する問題