Solved

xsl sum only 10 elements at a time

Posted on 2002-04-18
6
361 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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