Solved

xsl sum only 10 elements at a time

Posted on 2002-04-18
6
362 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Word files to Wordpress Posts 2 73
configure dependency in POM for new database 3 43
VB.Net. Reading xml value 6 36
DTD and JAVA versions 1 27
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…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

735 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