Sum and Count Together

Questions about XML that are not covered by the other forums should go here.
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Sum and Count Together

Post by dogmar »

Hello,

I am attempting to calculate the sum of the return of a count procedure and I seem not to be able to solve this. The following is what I have:

<xsl:value-of select="format-number(sum(value-of select="count(people)")"/>

The goal is the following:

If the count of people returns 1, 2, 3, 4 and then 5, then I would like for the sum to return 15. I would appreciate any help available. Thank you.

DS
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

Hello,

Please post a sample of the input XML and specify for what element of the input XML you expect the value 15 from the xsl:value-of element.


Regards,
Sorin
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Post by dogmar »

Unfortunately, I do not have the XML right here now. I think that what I need to do is to have the count in a for-each loop and declare a variable for sum. Then update sum with the value of count so that at the end, it is what I was looking for. The problem is that I can not get that to work. Does anyone know what I am talking about by any chance? Thank you.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

It seems you want to count all people elements that have a common ancestor element in which the sum is applied. In this case you need only an element like

Code: Select all


<xsl:value-of select="count(.//people)"/> 
applied when the context element is the common ancestor.


I hope this helps,
Sorin
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Post by dogmar »

hello Sorin,

Thank you for the help. I am not sure that will work.

<xsl:template match = "/" >
<xsl:variable name="adder"></xsl:variable>
<xsl:value-of select="count(.//@Department)"/>
<xsl:variable name="holder" select="$holder + $adder"/>
</xsl:template>

Does this seem like it will sum the return of the count? Thank you again.
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Post by sorin_ristache »

dogmar wrote:<xsl:variable name="holder" select="$holder + $adder"/>
I think you don't need that. I suggested only one xsl:value-of element for avoiding other holder variables for intermediate results. Just use the xsl:value-of element for the total count. If you show us a sample XML document you can get the exact xsl:template that you need.


Regards,
Sorin
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Post by dogmar »

Hello Sorin,

I am very lost I think. Would you mind telling me if the following is correct syntax at least? It tells me that I can not use header because it is out of scope. I want to output final. I think that this will work. Might not be perfect...but I am still learning. thank you very much for your help!

<xsl:for-each select="@Desc">

<xsl:variable name="adder"><xsl:value-of select="count(.//@Desc)"/></xsl:variable>

<xsl:variable name="holder" select="$holder + $adder"/>

</xsl:for-each>

<xsl:variable name="final" select="$holder"/>

</xsl:template>
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Post by dogmar »

dogmar wrote:header because it is out of scope.
should be "holder because it is out of scope"
dogmar
Posts: 6
Joined: Thu Jun 21, 2007 2:45 am

Post by dogmar »

Hello Sorin,

It may be easier to say it like this:

The XML looks like this:

<SCHED ID="1" Desc="Opening">
<ASSGN ID="10" Desc="Do Something 1">
<ACT ID="A1" Desc="Description 1"/>
<ACT ID="A2" Desc="Description 2"/>
</ASSGN>
<ASSGN "11" Desc="Do Something 2"/>
<ACT ID="A3" Desc="Description 3"/>
<ACT ID="A4" Desc="Description 4"/>
<ACT ID="A5" Desc="Description 5"/>
</ASSGN>
</SCHED>

The desired output for this would be "5". 5 is the total count of all of the ACT's. This will repeat for each <SCHED> and there may be a few of them. I would need to total count of ACT's for all SCHED's. Does this make sense? Thank you.
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

Inside a SCHED context, eg, a <xsl:for-each select="SCHED"> or a <xsl:template match="SCHED">

<xsl:value-of select="count(ASSGN/ACT)"/>
jkmyoung
Posts: 89
Joined: Mon Mar 06, 2006 10:13 pm

Post by jkmyoung »

Then afterwards, just use
<xsl:value-of select="//ACT"/>
or
<xsl:value-of select="SCHED//ACT"/>
Post Reply