 |
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

June 20th, 2007, 06:56 PM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sum and Count - Please Help
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.
|

June 21st, 2007, 02:50 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Firstly, your syntax is pretty bizarre. You can't mix XSLT and XPath in this way. You can embed XPath expressions inside XSLT instructions, but you can't embed XSLT instructions inside XPath expressions.
If it were possible to compose the functions this way, it would be written select="format-number(sum(count(people)))". But this doesn't work because count returns a number, and what does it mean to sum a number? In XPath 2.0 you can sum a sequence of computed numbers: select="sum(for $x in SSS return count($x/TTT))". You can't do that in 1.0, but there's a simple workaround: provided the sets are disjoint, the sum of the counts is the same as the count of the union of the sets, so for example to count all the employees of a company then instead of summing the counts in each department you just apply count() to the set of all employees.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

June 21st, 2007, 07:27 AM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello mhkay,
What you have said makes sense I think, but I am not sure if I understand. If I do the following:
<xsl:value-of select="count(people)"/>
then I get the number that I am looking for, but it is in a loop and so I am not summing those numbers (which is what I would really need to do). That is why I tried to sum the return of the count. Does this make sense? Anything else I can try? Thank you!
|

June 21st, 2007, 07:51 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
OK, supposing it is in a loop like this:
<xsl:for-each select="department">
<xsl:value-of select="count(people)"/>
</xsl:for-each>
Then you can get the total number of people as
count(department/people)
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

June 21st, 2007, 09:11 AM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Not sure if that will work. I do not think there is a parent in that respect.
I am now going down the path of:
<xsl:template match = "/" >
<xsl:adder>
</xsl:adder>
<xsl:value-of select="count(people)"/>
<xsl:adder> + </xsl:adder>
</xsl:template>
Would this work? Thank you very much, btw for your assistance.
|

June 21st, 2007, 10:33 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
That last example will return 1 from the count() function if people is the outermost element of your document, and 0 otherwise.
Except that I've no idea what kind of an instruction <xsl:adder> is supposed to be.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

June 21st, 2007, 11:24 AM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes...
I seem to be spinning...heh...Just trying to output final. All seems ok except that it says holder is out of scope...
<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>
|

June 21st, 2007, 12:04 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
You're trying to increment the variable in a loop, as you would in a procedural language! XSLT is a functional language, it doesn't work that way. Remember that xsl:for-each isn't a sequential loop: it doesn't process the selected items in any particular order, it processes each of them independently.
In any case, <xsl:for-each select="@Desc"> is going to select either zero or one nodes and is therefore going to iterate at most once.
If you show me your source document and your desired output then perhaps I can show you what's needed.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|

June 21st, 2007, 02:22 PM
|
Registered User
|
|
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hello mhkay,
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.
|

June 21st, 2007, 02:46 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
For a given SCHED, the count of ACTs is count(ASSGN/ACT).
For the parent of the SCHED elements, the count of ACTs in all the SCHEDs is count(SCHED/ASSIGN/ACT).
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|
 |