Solved

xsl sum only 10 elements at a time

Posted on 2002-04-18
6
363 Views
Last Modified: 2008-03-04
I have a style-sheet that displays the sums of attributes in all rows.  I want to limit it to summing only those rows that are displayed.(say the first 10 for example). Any ideas on how to approcah this would be appreciated.  Thanks

 <tr class="row1"><td colspan="5"/>
    <td><xsl:value-of select="format-number(sum(row/@Comp),'$#,##0.00')"/></td>
          <td><xsl:value-of select="format-number(sum(row/@Cash),'$#,##0.00')"/></td>
          <td><xsl:value-of select="format-number(sum(row/@Bank),'$#,##0.00')"/></td>
          <td><xsl:value-of select="format-number(sum(row/@Balance),'$#,##0.00')"/></td>
       </tr>
0
Comment
Question by:bqchristie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 23

Accepted Solution

by:
b1xml2 earned 200 total points
ID: 6950646
XML Document
===================
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<root>
 <user logs="123" />
 <user logs="347" />
 <user logs="1678" />
</root>

XSLT Document
=============
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<div>
First Match:
<xsl:value-of select="format-number(sum(//user[1]/@logs),'#,###,##0')" />
</div>
<div>
First And Second Match:
<xsl:value-of select="format-number(sum(//user[position() &lt; 3]/@logs),'#,###,##0')" />
</div>
<div>
All Matches:
<xsl:value-of select="format-number(sum(//user/@logs),'#,###,##0')" />
</div>
</xsl:template>
</xsl:stylesheet>

HTML Output
=============
<div>
First Match:
123</div>
<div>
First And Second Match:
470</div>
<div>
All Matches:
2,148</div>

0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6950667
It seems I misread your question, please post a sample of your XML and a sample of your XSLT so that I can provide corrections to your code.

Sincerely,

Brandon Driesen

PS Unless of course, my first reading of your question was correct.
PPS. Would appreciate more clarity in present and future posts in order to help you better =)
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6950681
If my reading of your question the first time is right, then the following is your solution:-

<xsl:variable name="max" select="number(10)" />
<tr class="row1">
<td colspan="5"></td>
<td>
<xsl:value-of select="format-number(sum(//row[position() &lt;= $max]/@Comp),'$#,##0.00')"/>
</td>
<td>
<xsl:value-of select="format-number(sum(//row[position() &lt;= $max]/@Cash),'$#,##0.00')"/>
</td>
<td>
<xsl:value-of select="format-number(sum(//row[position() &lt;= $max]/@Bank),'$#,##0.00')"/>
</td>
<td>
<xsl:value-of select="format-number(sum(//row[position() &lt;= $max]/@Balance),'$#,##0.00')"/>
</td>
</tr>
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 23

Expert Comment

by:b1xml2
ID: 6950714
Notes with regard to code
============================
We use a variable to store the maximum number of items that you want to count (according to the document structure, this is an important distinction!!!).

//row[position() &lt;= $max] means all row elements whose position is less or equal to that of the max variable which happens to be 10  here.

Regards,

Brandon Driesen
0
 

Author Comment

by:bqchristie
ID: 6950782
Thanks Brandon,
Your the man!!  Just tried it and it works.  Actually your first answer was pretty much what I was looking for the rest was gravy!!

I'll try to be more descriptive next time

Cheers
Bruce
0
 
LVL 23

Expert Comment

by:b1xml2
ID: 6950813
The following is a more exacting solution, providing running totals per group
XML Document
==========================
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<root>
 <user logs="123" />
 <user logs="347" />
 <user logs="1678" />
 <user logs="2345" />
 <user logs="123" />
 <user logs="23" />
 <user logs="245" />
</root>

XSLT Document
===============
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="iso-8859-1" indent="yes"/>
<xsl:template match="/">
<xsl:variable name="max" select="number(3)" />
<html>
<head>
<title>Running Totals</title>
<style>
table {width:400px;table-layout:fixed;border-left:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;}
td, th {font-family:Verdana;border-top:1px solid #000000;font-size:11pt;}
</style>
</head>
<body>
<table cellpadding="4" cellspacing="0">
<col width="30%" />
<col width="70%" />
<xsl:for-each select="//user[position() mod $max = 1]">
     <tr>
          <td><xsl:value-of select="concat(count(preceding-sibling::user) + 1,'.')" /></td>
          <td><xsl:value-of select="format-number(@logs,'#,###,##0')" /></td>
     </tr>
     <xsl:for-each select="following-sibling::user[position() &lt; $max]">
     <tr>
          <td><xsl:value-of select="concat(count(preceding-sibling::user) + 1,'.')" /></td>
          <td><xsl:value-of select="format-number(@logs,'#,###,##0')" /></td>
     </tr>
     </xsl:for-each>
     <xsl:variable name="total" select="sum(following-sibling::user[position() &lt; $max]/@logs) + @logs" />
     <tr>
          <th>Total</th>
          <td><xsl:value-of select="format-number($total,'#,###,##0')" /></td>
     </tr>
</xsl:for-each>
<xsl:variable name="grand-total" select="sum(//user/@logs)" />
<tr>
     <th>Grand Total</th>
     <td><xsl:value-of select="format-number($grand-total,'#,###,##0')" /></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

HTML Output
===========
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Running Totals</title>
<style>
table {width:400px;table-layout:fixed;border-left:1px solid #000000;border-right:1px solid #000000;border-bottom:1px solid #000000;}
td, th {font-family:Verdana;border-top:1px solid #000000;font-size:11pt;}
</style>
</head>
<body>
<table cellpadding="4" cellspacing="0">
<col width="30%">
<col width="70%">
<tr>
<td>1.</td>
<td>123</td>
</tr>
<tr>
<td>2.</td>
<td>347</td>
</tr>
<tr>
<td>3.</td>
<td>1,678</td>
</tr>
<tr>
<th>Total</th>
<td>2,148</td>
</tr>
<tr>
<td>4.</td>
<td>2,345</td>
</tr>
<tr>
<td>5.</td>
<td>123</td>
</tr>
<tr>
<td>6.</td>
<td>23</td>
</tr>
<tr>
<th>Total</th>
<td>2,491</td>
</tr>
<tr>
<td>7.</td>
<td>245</td>
</tr>
<tr>
<th>Total</th>
<td>245</td>
</tr>
<tr>
<th>Grand Total</th>
<td>4,884</td>
</tr>
</table>
</body>
</html>




0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question