Reputation: 13905
Working XSLT 2.0 implementation:
<xsl:template name="SummaryRows">
<xsl:for-each-group select="//Item/ItemGroup/ItemEntry" group-by="Description">
<tr>
<td>
<xsl:value-of select="current-grouping-key()"/>
</td>
<td></td>
<td>
<xsl:call-template name="formatNumber2">
<xsl:with-param name="number" select="format-number(number(sum(current-group()/ItemSum/text())), '#.##')"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each-group>
</xsl:template>
Attempt to implement Muenchian grouping:
<xsl:key name="items-by-description" match="ItemEntry" use="Description"/>
<xsl:template name="SummaryRows">
<xsl:for-each select="//Item/ItemGroup/ItemEntry[count(. | key('items-by-description', Description)[1]) = 1]">
<xsl:variable name="current-grouping-key" select="Description"/>
<xsl:variable name="current-group" select="key('items-by-description', $current-grouping-key)"/>
<xsl:for-each select="$current-group">
<tr>
<td>
<xsl:value-of select="$current-grouping-key"/>
</td>
<td/>
<td align="right">
<xsl:call-template name="formatNumber2">
<xsl:with-param name="number" select="format-number(number(sum(./ItemSum/text())), '#.##')"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Example xml snippet:
<Item>
<ItemGroup groupId="1">
<ItemEntry>
<Description>A</Description>
<ItemSum>6301.1</ItemSum>
</ItemEntry>
<ItemEntry>
<Description>B</Description>
<ItemSum>901.1</ItemSum>
</ItemEntry>
</ItemGroup>
<ItemGroup groupId="2">
<ItemEntry>
<Description>C</Description>
<ItemSum>631.1</ItemSum>
</ItemEntry>
<ItemEntry>
<Description>A</Description>
<ItemSum>9.1</ItemSum>
</ItemEntry>
</ItemGroup>
</Item>
It should return list of ItemEntry
's Description
(that suppose to be current-grouping-key() in xslt 2.0). For each such group it has to have sum of all ItemSum
of that group.
Example result:
A 6310.2
B 901.1
C 631.1
The <xsl:for-each select="$current-group">
loop is not executed resulting in empty set. What is the implementation missing?
Update:
The environment is Java 8
Transformation factory properties / initialization:
System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" + XPathFactory.DEFAULT_OBJECT_MODEL_URI,
"org.apache.xpath.jaxp.XPathFactoryImpl");
XPathFactory xpf = XPathFactory.newInstance();
Upvotes: 0
Views: 105
Reputation: 167401
As I said in a comment, you don't need two nested for-each
elements to replace a single for-each-group
, you just use one, then a minimal sample would be
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" doctype-system="about:legacy-doctype"/>
<xsl:key name="items-by-description" match="ItemEntry" use="Description"/>
<xsl:template match="/*">
<xsl:for-each select="//Item/ItemGroup/ItemEntry[count(. | key('items-by-description', Description)[1]) = 1]">
<xsl:variable name="current-grouping-key" select="Description"/>
<xsl:variable name="current-group" select="key('items-by-description', $current-grouping-key)"/>
<tr>
<td>
<xsl:value-of select="$current-grouping-key"/>
</td>
<td/>
<td align="right">
<xsl:value-of select="format-number(number(sum($current-group/ItemSum/text())), '#.##')"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
https://xsltfiddle.liberty-development.net/3MvmXiv/3 and would output three tr
elements for your sample, just like the XSLT 2 approach https://xsltfiddle.liberty-development.net/3MvmXiv/0
Upvotes: 2