?
Solved

xsl sum only 10 elements at a time

Posted on 2002-04-18
6
Medium Priority
?
365 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 800 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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 Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

741 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