NiteOwls
NiteOwls

Reputation: 309

XSLT SUM() not working as expected

I've been trying to get the sum (total) value for elements in different nodes, but after a week messing around with example after example I am no step closer to the solution.

All I get is the different values listed in a string rather than the sum.

The selection used to get the values is:

<xsl:for-each select="//workgroups/workgroup/agentstatus/status">
  <xsl:if test="key='Available, No ACD'">
    <xsl:value-of select="sum(value)"/>
  </xsl:if>
</xsl:for-each>

This returns 111 rather than the expected value 3

What do I do wrong here?

This is xml version="1.0" encoding='utf-8'

Upvotes: 1

Views: 1435

Answers (2)

Your code select all status elements and then for each that have the desired key value, it outputs sum(value). So, the summary runs separately for each status hit with xsl:if.

You should use:

<xsl:value-of 
     select="sum(//workgroups/workgroup/agentstatus
                   /status[key/text()='Available, No ACD']/value)"/>

Upvotes: 1

JohnLBevan
JohnLBevan

Reputation: 24400

Try this:

<xsl:value-of select="sum(//workgroups/workgroup/agentstatus/status[./key/text()='Available, No ACD']/value)" />

The looping's taken care of by the way XSLT works, so you don't need to specify a for each or an if statement - it's all implied by the xpath statement. If this doesn't work please can you post a copy of the XML you're running it on and I'll then update the xpath as required.

Upvotes: 1

Related Questions