XSLT总和基于收入类型的收入金额

时间:2015-02-25 12:27:14

标签: xml xslt sum grouping

我是XSLT的新手。我有以下XML源结构:

INPUT XML:

<BORROWER _SSN="*********">
    <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="25000.0000" />
    <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="25000.0000" />
    <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="0.0000" />
    <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0.0000" />
    <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0.0000" />
</BORROWER>
<BORROWER _SSN="*********">
    <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="1000.0000" />
    <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="25000.0000" />
    <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="10.0000" />
    <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0.0000" />
    <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0.0000" />
</BORROWER>

我需要根据IncomeType汇总MonthlyTotalAmount金额。

我尝试了以下代码。但它没有用。

我的代码:

    <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:util="http://www.aaa.com/Schema/aaa/BTUtility"
                        xmlns:quan="http://www.aaaa.com/Schema/aaa"
                xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp"
                exclude-result-prefixes="msxsl">
  <xsl:template match="/">
    <xsl:apply-templates select="//quan:REQUEST_GROUP"/>
  </xsl:template>
  <xsl:template match="//quan:REQUEST_GROUP">
    <LOAN_APPLICATION>
      <xsl:variable name ="SSN" select ="./REQUEST/KEY[@_Name = 'Test_SSN']/@_Value"></xsl:variable>
      <xsl:for-each select="./REQUEST/REQUEST_DATA/LOAN_APPLICATION/BORROWER">
        <BORROWER>          
          <xsl:for-each select="./CURRENT_INCOME">
            <CURRENT_INCOME>
              <xsl:if test="not(userCSharp:IsBlankOrNull(userCSharp:TrimString(string(./@IncomeType))))">
                <xsl:attribute name="IncomeType">
                  <xsl:choose>
                    <xsl:when test="userCSharp:ToUpperText(./@IncomeType)= 'BASE'">
                      <xsl:text>Base</xsl:text>
                    </xsl:when>
                    <xsl:when test="userCSharp:ToUpperText(./@IncomeType)= 'BONUS'">
                      <xsl:text>Bonus</xsl:text>
                    </xsl:when>
                    <xsl:when test="userCSharp:ToUpperText(./@IncomeType)= 'COMMISSIONS'">
                      <xsl:text>Commissions</xsl:text>
                    </xsl:when>                    
                    <xsl:when test="userCSharp:ToUpperText(./@IncomeType)= 'OVERTIME'">
                      <xsl:text>Overtime</xsl:text>
                    </xsl:when>
                  </xsl:choose>
                </xsl:attribute>
              </xsl:if>
              <xsl:if test="not(userCSharp:IsBlankOrNull(userCSharp:TrimString(string(./@_MonthlyTotalAmount))))">
                <xsl:attribute name="_MonthlyTotalAmount">
                  <xsl:value-of select="format-number(./@_MonthlyTotalAmount, '#0.00')"/>
                </xsl:attribute>
              </xsl:if>
            </CURRENT_INCOME>
          </xsl:for-each>
        </BORROWER>
      </xsl:for-each>
    </LOAN_APPLICATION>
  </xsl:template>
</xsl:stylesheet>

输出需要类似于以下内容:

<BORROWER _SSN="*********">
    <CURRENT_INCOME IncomeType="Base" _MonthlyTotalAmount="5000.00" />
    <CURRENT_INCOME IncomeType="Bonus" _MonthlyTotalAmount="0.00" />
    <CURRENT_INCOME IncomeType="Commissions" _MonthlyTotalAmount="0.00" />
    <CURRENT_INCOME IncomeType="Overtime" _MonthlyTotalAmount="0.00" />
</BORROWER>
<BORROWER _SSN="*********">
    <CURRENT_INCOME IncomeType="Base" _MonthlyTotalAmount="1000.00" />
    <CURRENT_INCOME IncomeType="Bonus" _MonthlyTotalAmount="25010.00" />
    <CURRENT_INCOME IncomeType="Commissions" _MonthlyTotalAmount="0.00" />
    <CURRENT_INCOME IncomeType="Overtime" _MonthlyTotalAmount="0.00" />
</BORROWER>

非常感谢任何小帮助。

1 个答案:

答案 0 :(得分:1)

好吧,如果你采取格式良好的输入,例如:

<root>
    <BORROWER _SSN="*********">
        <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="25000.0000" />
        <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="25000.0000" />
        <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="0.0000" />
        <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0.0000" />
        <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0.0000" />
    </BORROWER>
    <BORROWER _SSN="*********">
        <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="1000.0000" />
        <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="25000.0000" />
        <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="10.0000" />
        <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0.0000" />
        <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0.0000" />
    </BORROWER>
</root>

并应用以下样式表:

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:template match="/root">
    <xsl:copy>
        <xsl:for-each select="BORROWER">
            <xsl:copy>
                <xsl:copy-of select="@*"/>
                <xsl:for-each-group select="CURRENT_INCOME" group-by="@IncomeType">
                    <CURRENT_INCOME IncomeType="{@IncomeType}" _MonthlyTotalAmount="{sum(current-group()/@_MonthlyTotalAmount)}" />
                </xsl:for-each-group>
            </xsl:copy> 
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

你会得到:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <BORROWER _SSN="*********">
      <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="50000"/>
      <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="0"/>
      <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0"/>
      <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0"/>
   </BORROWER>
   <BORROWER _SSN="*********">
      <CURRENT_INCOME IncomeType="BASE" _MonthlyTotalAmount="1000"/>
      <CURRENT_INCOME IncomeType="BONUS" _MonthlyTotalAmount="25010"/>
      <CURRENT_INCOME IncomeType="COMMISSIONS" _MonthlyTotalAmount="0"/>
      <CURRENT_INCOME IncomeType="OVERTIME" _MonthlyTotalAmount="0"/>
   </BORROWER>
</root>

格式化数字是一个微不足道的补充。