Solved

xsl sum only 10 elements at a time

Posted on 2002-04-18
6
351 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 23

Expert Comment

by:b1xml2
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now