想知道如何在 XSLT 中创建多个分组。以下 XML 应按 memberID、存款日期和存款类型分组。每个会员的总金额应按日期和存款类型求和。我已经提出了这个问题,答案在 3.0 版本中。我需要一个使用 XSLT 2.0 版的解决方案,因为我正在使用的系统对 3.0 版有限制。谢谢你,.
<?xml version="1.0" encoding="UTF-8"?>
<Report>
<Deposit_Data>
<MemberID>12345</MemberID>
<MemberFirstName>Mickey</MemberFirstName>
<MemberLastName>Mouse</MemberLastName>
<DepositDate>2023-11-22</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>50.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>30.00</Amount>
</DepositInfo>
</Deposit_Data>
<Deposit_Data>
<MemberID>12345</MemberID>
<MemberFirstName>Mickey</MemberFirstName>
<MemberLastName>Mouse</MemberLastName>
<DepositDate>2023-11-22</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>10.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>5.00</Amount>
</DepositInfo>
</Deposit_Data>
<Deposit_Data>
<MemberID>12345</MemberID>
<MemberFirstName>Mickey</MemberFirstName>
<MemberLastName>Mouse</MemberLastName>
<DepositDate>2023-12-05</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>25.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>10.00</Amount>
</DepositInfo>
</Deposit_Data>
<Deposit_Data>
<MemberID>78910</MemberID>
<MemberFirstName>Donald</MemberFirstName>
<MemberLastName>Duck</MemberLastName>
<DepositDate>2023-11-22</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>10.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>30.00</Amount>
</DepositInfo>
</Deposit_Data>
<Deposit_Data>
<MemberID>78910</MemberID>
<MemberFirstName>Donald</MemberFirstName>
<MemberLastName>Duck</MemberLastName>
<DepositDate>2023-12-05</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>20.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>10.00</Amount>
</DepositInfo>
</Deposit_Data>
<Deposit_Data>
<MemberID>78910</MemberID>
<MemberFirstName>Donald</MemberFirstName>
<MemberLastName>Duck</MemberLastName>
<DepositDate>2023-12-05</DepositDate>
<DepositInfo>
<DepositType>CHK</DepositType>
<Amount>5.00</Amount>
</DepositInfo>
<DepositInfo>
<DepositType>SAV</DepositType>
<Amount>10.00</Amount>
</DepositInfo>
</Deposit_Data>
最终结果应该是:
MemberID|DepositDate|DepositType|Amount
12345|2023-11-22|CHK|60
12345|2023-11-22|SAV|35
12345|2023-12-05|CHK|25
12345|2023-12-05|SAV|10
78910|2023-11-22|CHK|10
78910|2023-11-22|SAV|30
78910|2023-12-05|CHK|25
78910|2023-12-05|SAV|20
我尝试根据日期和存款类型运行 for-each-group,但这不起作用。请参阅下面的 XSLT。
<xsl:choose>
<xsl:when test="exists(DepositInfo)">
<xsl:for-each-group select="." group-by="DepositDate">
<xsl:variable name="DepositDateOfCurrentGroup" select="current-grouping-key()"/>
<!-- MemberID --><xsl:value-of select="MemberID"/>
<xsl:value-of select="$vPipeDelimiter"/>
<!-- DepositDate --><xsl:value-of select="format-date($DepositDateOfCurrentGroup,'[Y0001][M01][D01]')"/>
<xsl:value-of select="$vPipeDelimiter"/>
<!-- DepositType -->
<xsl:choose>
<xsl:when test="DepositInfo/DepositType = 'CHK'">
<!-- Amount --><xsl:text>ACCT1</xsl:text>
</xsl:when>
<xsl:when test="DepositInfo/DepositType = 'SAV'">
<!-- Amount --><xsl:text>ACCT2</xsl:text>
</xsl:when>
<xsl:when test="DepositInfo/DepositType = 'CD'">
<!-- Amount --><xsl:text>ACCT3</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text/>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$vPipeDelimiter"/>
<xsl:choose>
<xsl:when test="exists(DepositDate = $DepositDateOfCurrentGroup and DepositInfo/DepositType = 'CHK')">
<xsl:value-of select="format-number(sum(DepositDate[DepositDate = $DepositDateOfCurrentGroup]/Amount),'#######0.00')"/>
</xsl:when>
<xsl:when test="exists([DepositDate = $DepositDateOfCurrentGroup and DepositInfo/DepositType = 'SAV'])">
<xsl:value-of select="format-number(sum(DepositDate[DepositDate = $DepositDateOfCurrentGroup]/Amount),'#######0.00')"/>
</xsl:when>
<xsl:when test="exists(DepositDate = $DepositDateOfCurrentGroup and DepositInfo/DepositType = 'CD')">
<xsl:value-of select="format-number(sum(DepositDate[DepositDate = $DepositDateOfCurrentGroup]/Amount),'#######0.00')"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>0.00</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$vLineFeed"/>
</xsl:for-each-group>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
最终结果没有分组:
MemberID|DepositDate|DepositType|Amount|
12345|20231122|CHK|0.00
12345|20231122|CHK|0.00
12345|20231205|CHK|0.00
78910|20231122|CHK|0.00
78910|20231205|CHK|0.00
78910|20231205|CHK|0.00
xslt 2.0 似乎不支持
composite="yes"
。试试这个:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each-group select="Report/Deposit_Data/DepositInfo"
group-by="string-join((../MemberID, ../DepositDate, DepositType), '|')">
<xsl:value-of select="current-grouping-key(), sum(current-group()/Amount)" separator="|"/>
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>