Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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
 
Old June 20th, 2007, 06:56 PM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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.
 
Old June 21st, 2007, 02:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 21st, 2007, 07:27 AM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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!

 
Old June 21st, 2007, 07:51 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 21st, 2007, 09:11 AM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.

 
Old June 21st, 2007, 10:33 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
 
Old June 21st, 2007, 11:24 AM
Registered User
 
Join Date: Jun 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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>


 
Old June 21st, 2007, 12:04 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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

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.

 
Old June 21st, 2007, 02:46 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
Using count or sum to validate radiobutton rdove84 SQL Server ASP 0 November 8th, 2006 04:17 PM
Help: Running Sum (or Cumulative Sum) timdasa VB Databases Basics 1 August 22nd, 2006 03:12 PM
is there any in built function to count page count g.tamilselvan MySQL 1 February 15th, 2006 07:43 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.