在 xslt 中一次在多个节点分组
Group by at multiple node at once in xslt
下面是输入xml结构-
<root>
<Entry>
<id>id1</id>
<Line>
<tax>tax1</tax>
<amount>13.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>4.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>2.00</amount>
</Line>
<Line>
<tax>tax4</tax>
<amount>8.00</amount>
</Line>
</Entry>
<Entry>
<id>id1</id>
<Line>
<tax>tax2</tax>
<amount>15.00</amount>
</Line>
<Line>
<tax>tax3</tax>
<amount>35.00</amount>
</Line>
</Entry>
<Entry>
<id>id2</id>
<Line>
<tax>tax2</tax>
<amount>12.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>22.00</amount>
</Line>
<Line>
<tax>tax1</tax>
<amount>5.00</amount>
</Line>
</Entry>
</root>
我想得到如下结构的输出-
id1, tax1, 13.00
id1, tax2, 21.00
id1, tax3, 35.00
id1, tax4, 8.00
id2, tax1, 5.00
id2, tax2, 34.00
我尝试实现以下逻辑。但它没有用。
forEach 条目组按 id
forEach distinct(id) groupBy 税
有人可以帮我进行 xslt 转换吗?
以下 XSLT 2.0 样式表使用 xsl:for-each-group
to group the amount
by a composite key of their id
and tax
values concatenated, calculates the sum()
of the grouped amount
and uses format-number()
来确保两位小数。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each-group select="/root/Entry/Line/amount" group-by="string-join((../../id, ../tax), '-')">
<xsl:sort select="current-grouping-key()"/>
<xsl:value-of select="../../id, ../tax, format-number(sum(current-group()), '#.00')" separator=", " />
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
下面是输入xml结构-
<root>
<Entry>
<id>id1</id>
<Line>
<tax>tax1</tax>
<amount>13.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>4.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>2.00</amount>
</Line>
<Line>
<tax>tax4</tax>
<amount>8.00</amount>
</Line>
</Entry>
<Entry>
<id>id1</id>
<Line>
<tax>tax2</tax>
<amount>15.00</amount>
</Line>
<Line>
<tax>tax3</tax>
<amount>35.00</amount>
</Line>
</Entry>
<Entry>
<id>id2</id>
<Line>
<tax>tax2</tax>
<amount>12.00</amount>
</Line>
<Line>
<tax>tax2</tax>
<amount>22.00</amount>
</Line>
<Line>
<tax>tax1</tax>
<amount>5.00</amount>
</Line>
</Entry>
</root>
我想得到如下结构的输出-
id1, tax1, 13.00
id1, tax2, 21.00
id1, tax3, 35.00
id1, tax4, 8.00
id2, tax1, 5.00
id2, tax2, 34.00
我尝试实现以下逻辑。但它没有用。 forEach 条目组按 id forEach distinct(id) groupBy 税
有人可以帮我进行 xslt 转换吗?
以下 XSLT 2.0 样式表使用 xsl:for-each-group
to group the amount
by a composite key of their id
and tax
values concatenated, calculates the sum()
of the grouped amount
and uses format-number()
来确保两位小数。
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each-group select="/root/Entry/Line/amount" group-by="string-join((../../id, ../tax), '-')">
<xsl:sort select="current-grouping-key()"/>
<xsl:value-of select="../../id, ../tax, format-number(sum(current-group()), '#.00')" separator=", " />
<xsl:text> </xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>