Sum and Count Together
Questions about XML that are not covered by the other forums should go here.
-
- Posts: 6
- Joined: Thu Jun 21, 2007 2:45 am
Sum and Count Together
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
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
-
- 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
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
-
- Posts: 6
- Joined: Thu Jun 21, 2007 2:45 am
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.
-
- 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
applied when the context element is the common ancestor.
I hope this helps,
Sorin
Code: Select all
<xsl:value-of select="count(.//people)"/>
I hope this helps,
Sorin
-
- Posts: 6
- Joined: Thu Jun 21, 2007 2:45 am
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.
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.
-
- Posts: 4141
- Joined: Fri Mar 28, 2003 2:12 pm
Post by sorin_ristache »
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.dogmar wrote:<xsl:variable name="holder" select="$holder + $adder"/>
Regards,
Sorin
-
- Posts: 6
- Joined: Thu Jun 21, 2007 2:45 am
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>
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>
-
- Posts: 6
- Joined: Thu Jun 21, 2007 2:45 am
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.
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.
Return to “General XML Questions”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service