Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2007, 06:56 PM
Registered User
 
Join Date: Jun 2007
Location: Charlotte, NC, USA.
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.
Reply With Quote
  #2 (permalink)  
Old June 21st, 2007, 02:50 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #3 (permalink)  
Old June 21st, 2007, 07:27 AM
Registered User
 
Join Date: Jun 2007
Location: Charlotte, NC, USA.
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!

Reply With Quote
  #4 (permalink)  
Old June 21st, 2007, 07:51 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #5 (permalink)  
Old June 21st, 2007, 09:11 AM
Registered User
 
Join Date: Jun 2007
Location: Charlotte, NC, USA.
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.

Reply With Quote
  #6 (permalink)  
Old June 21st, 2007, 10:33 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #7 (permalink)  
Old June 21st, 2007, 11:24 AM
Registered User
 
Join Date: Jun 2007
Location: Charlotte, NC, USA.
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>


Reply With Quote
  #8 (permalink)  
Old June 21st, 2007, 12:04 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
  #9 (permalink)  
Old June 21st, 2007, 02:22 PM
Registered User
 
Join Date: Jun 2007
Location: Charlotte, NC, USA.
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.

Reply With Quote
  #10 (permalink)  
Old June 21st, 2007, 02:46 PM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:11 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.