0

Thank you all for taking the time to view my problem.

I am having trouble summing the node when the nodes value is different, but works if it is the same.

Here is my XML -

<statement>
    <agency>
        <employerShareAmt>75.00</employerShareAmt>
    </agency>
    <agency>
        <employerShareAmt>76.00</employerShareAmt>        
    </agency>
</statement>

Here is my XSLT -

<xsl:template match="/">
    <root>
        <xsl:call-template name="shopData"/>
    </root>
</xsl:template>

<xsl:template name="shopData">
    <RESULT>
        <xsl:call-template name="sum"/>
    </RESULT>
</xsl:template>

<xsl:template match="node()|@*" name="sum">
    <xsl:apply-templates select="node()|@*"/>
</xsl:template>

<xsl:template match="statement">
  <agency>
    <xsl:for-each-group select="agency" 
                        group-by="employerShareAmt">
      <employerShareAmt>
        <xsl:value-of 
          select="sum(current-group()/employerShareAmt)"/>
      </employerShareAmt>
    </xsl:for-each-group>
  </agency>
</xsl:template>

The result is 75 76. But if both were 75, the result would be 150.

I seems that it is grouping by value then adding them. How can I get it to group by it's node so the above XML result will be 151?

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
proximityFlying
  • 153
  • 3
  • 13
  • It sounds like you just want the total of all the `employerShareAmt` values? In that case you can just forget about grouping. Unless there are multiple agencies and you want the total per agency? But you are grouping by the value of the share amount and that can't be right. Is there some sort of agency ID in the real data? – Borodin Jun 04 '13 at 22:01

1 Answers1

0

You sense that the stylesheet processor is grouping the agencies by value and then summing their employerShareAmt elements. You are right. It's doing so because you asked it to do so: that's what group-by="employerShareAmt" does.

What sum are you looking for? The sum of all employerShareAmt elements within the current statement element?

<xsl:template match="statement">
  <agency>
    <employerShareAmt>
      <xsl:value-of select="sum(agency/employerShareAmt)"/>
    </employerShareAmt>
  </agency>
</xsl:template>

The sum of all employerShareAmt elements within a given agency? For the input you show, the identity transform will do quite nicely; otherwise

<xsl:template match="agency">
  <agency>
    <employerShareAmt>
      <xsl:value-of select="sum(EmployerShareAmt)"/>
    <employerShareAmt>
  </agency>
</xsl:template>

The sum of all employerShareAmt elements for some group of agencies with the same agency ID or name or equality of some other property you didn't show? Keep something like your current structure but group on the correct property of the agency, not on the value of employerShareAmt.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65