Note that some id's repeat and I only want the sum of the count of each unique id. The correct total will be 11.
Note: I had to edit the id on the first set to be 1
Sample Input File:
<?xml version="1.0" encoding="UTF-8"?>
<file>
<item>
<id>1</id>
<count>4</count>
</item>
<item>
<id>2</id>
<count>7</count>
</item>
<item>
<id>2</id>
<count>7</count>
</item>
<item>
<id>2</id>
<count>7</count>
</item>
</file>
Sample Ouput File:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<totalUniqueItemCount>11</totalUniqueItemCount>
</output>
XSLT Attempt:
Here you can see am trying to use for-each group but it is not working for me. I need to be able to get a sum of these counts but I can't figure out how to surround the output of the for-each-group to be able to sum the counts.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/file">
<output>
<totalUniqueItemCount>
<xsl:for-each-group select="item" group-by="id">
<xsl:value-of select="xs:integer(count)"/>
</xsl:for-each-group>
</totalUniqueItemCount>
</output>
</xsl:template>
</xsl:stylesheet>
I also tried this approach with no luck:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/file">
<output>
<totalUniqueItemCount>
<xsl:value-of select="sum(item[id ne following-sibling::id]/count)"/>
</totalUniqueItemCount>
</output>
</xsl:template>
</xsl:stylesheet>