Advertisement

01.23.2008 at 07:35AM PST, ID: 23104640
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

XML/XSL : Calculating the grand total of a computed column
Tags: XML/XSL
I am a complete newbie in the XSL transformation world and I need your help.

Say I have this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<NAV>
      <Funds Description="Funds" Date="2008-01-17">
            <Fund Description="Fund 1">
                  <SubFund Description="Sub Fund 1.1">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>1000</ClosingCapital>
                        <XRate>1</XRate>
                  </SubFund>
                  <SubFund Description="Sub Fund  1.2">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>1000</ClosingCapital>
                        <XRate>1</XRate>
                  </SubFund>
                  <SubFund Description="Sub Fund 1.3">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>1000</ClosingCapital>
                        <XRate>1</XRate>
                  </SubFund>
            </Fund>

            <Fund Description="Fund 2" >
                  <SubFund Description="Sub Fund 2.1">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>4000</ClosingCapital>
                        <XRate>2</XRate>
                  </SubFund>
            </Fund>

            <Fund Description="Fund 3" >
                  <SubFund Description="Sub Fund 3.1">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>9000</ClosingCapital>
                        <XRate>3</XRate>
                  </SubFund>
                  <SubFund Description="Sub Fund 3.2">
                        <ValuationDate>2008-01-17</ValuationDate>
                        <ClosingCapital>9000</ClosingCapital>
                        <XRate>3</XRate>
                  </SubFund>
            </Fund>
      </Funds>
</NAV>

And also this XSL file:
<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
      version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl"
>


      <xsl:template match="/">
    <html>

            <body>

                  <h2>
                        <xsl:value-of select="NAV/Funds/@Description" />
                  </h2>
                  <xsl:value-of select="NAV/Funds/@Date" />
                  <br />
                  <br />
                  <br />


                  <table border="1">
                        <xsl:for-each select="NAV/Funds/Fund">

                              <tr>
                                    <td>
                                          <xsl:value-of select="@Description"/>
                                    </td>
                              </tr>

                              <tr>
                                    <td>Fund Name</td>
                                    <td>Valuation Date</td>
                                    <td>Closing Capital</td>
                                    <td>Closing Capital (Converted)</td>
                                    <td>% of Grand Total</td>
                              </tr>
                              <xsl:variable name="FundTotal" select="SubFund/ClosingCapital"></xsl:variable>
                              <xsl:variable name="FundTotalConverted" select="$FundTotal"></xsl:variable>
                              <xsl:variable name="XRate" select="@XRate"></xsl:variable>
                              <xsl:for-each select="SubFund">
                                    <xsl:variable name="Converted" select="ClosingCapital div ../@XRate"></xsl:variable>
                              <tr>
                                          <td>
                                                <xsl:value-of select="@Description" />
                                          </td>
                                          <td>
                                                <xsl:value-of select="ValuationDate" />
                                          </td>
                                          <td align="right">
                                                <xsl:value-of select="format-number(ClosingCapital, '###,##0')" />
                                          </td>
                                          <td align="right">
                                                <xsl:value-of select="format-number(ClosingCapital div XRate, '###,##0')" />
                                          </td>
                                          <td align="right">
                                                TODO
                                          </td>
                                    </tr>
                              </xsl:for-each>
                              <tr>
                                    <td colspan="2">Total <xsl:value-of select="@Description"/></td>
                                    <td align="right">
                                          <xsl:value-of select="format-number(sum($FundTotal), '###,##0')" />
                                    </td>
                                    <td align="right">
                                          TODO
                                    </td>
                                    <td align="right">TODO</td>
                              </tr>


                  </xsl:for-each>

                        <tr>
                        <td colspan="2">
                              Grand Total <xsl:value-of select="@Description"/>
                        </td>
                        <td align="right">
                              <xsl:value-of select="format-number(sum(//NAV/Funds/Fund/SubFund/ClosingCapital), '###,##0')" />
                        </td>
                              <td align="right">
                                    TODO
                              </td>
                              <td align="right">TODO</td>
                  </tr>
                  </table>

            </body>
    </html>
</xsl:template>

</xsl:stylesheet>


I need to add to this XSL file:
-the total by Fund of the Closing Capital (Converted) column
-the grand total for all funds of the Closing Capital (Converted) column
-calculate the % of Grand total column (which is the Closing Capital (Converted) value / Grand Total converted

Any ideas?
Start your free trial to view this solution
Question Stats
Zone: Web Development
Question Asked By: emoreau
Solution Provided By: Gertone
Participating Experts: 1
Solution Grade: A
Views: 12
Translate:
Loading Advertisement...
01.23.2008 at 08:10AM PST, ID: 20724487

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.23.2008 at 08:21AM PST, ID: 20724622

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.23.2008 at 08:10AM PST, ID: 20724487

Rank: Genius

You need to add some recursion to the mix to get this done
(this is a lot easier in XSLT2.0)

Note that I got rid of the nested for-each constructs.
It is a lot easier to maintain, if you let XSLT do the work instead of doing it yourself

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl"
    >
   
   
    <xsl:template match="/">
        <html>
            <body>
                <h2>
                    <xsl:value-of select="NAV/Funds/@Description" />
                </h2>
                <xsl:value-of select="NAV/Funds/@Date" />
                <br />
                <br />
                <br />
                <table border="1">
                    <xsl:apply-templates select="NAV/Funds/Fund"/>
                    <tr>
                        <td colspan="2">
                            Grand Total
                        </td>
                        <td align="right">
                            <xsl:value-of select="format-number(sum(//NAV/Funds/Fund/SubFund/ClosingCapital), '###,##0')" />
                        </td>
                        <td align="right">
                            TODO
                        </td>
                        <td align="right">TODO</td>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
   
    <xsl:template match="Fund">
            <xsl:variable name="totalConvertedPerFund">
                <xsl:call-template name="collectFundTotal">
                    <xsl:with-param name="thisSubFund" select="SubFund[1]"/>
                    <xsl:with-param name="curTot" select="0"/>
                </xsl:call-template>
          </xsl:variable>
            <tr>
                <td colspan="5">
                    <xsl:value-of select="@Description"/>
                </td>
            </tr>
            <tr>
                <td>Fund Name</td>
                <td>Valuation Date</td>
                <td>Closing Capital</td>
                <td>Closing Capital (Converted)</td>
                <td>% of Grand Total</td>
            </tr>
            <xsl:variable name="FundTotal" select="SubFund/ClosingCapital"></xsl:variable>
            <xsl:variable name="FundTotalConverted" select="$FundTotal"></xsl:variable>
            <xsl:variable name="XRate" select="@XRate"></xsl:variable>
            <xsl:apply-templates select="SubFund"/>
            <tr>
                <td colspan="2">Total <xsl:value-of select="@Description"/></td>
                <td align="right">
                    <xsl:value-of select="format-number(sum($FundTotal), '###,##0')" />
                </td>
                <td align="right">
                    <xsl:value-of select="format-number($totalConvertedPerFund, '###,##0')" />
                </td>
                <td align="right">TODO</td>
            </tr>
    </xsl:template>
   
    <xsl:template match="SubFund">
        <xsl:variable name="Converted" select="ClosingCapital div ../@XRate"></xsl:variable>
        <tr>
            <td>
                <xsl:value-of select="@Description" />
            </td>
            <td>
                <xsl:value-of select="ValuationDate" />
            </td>
            <td align="right">
                <xsl:value-of select="format-number(ClosingCapital, '###,##0')" />
            </td>
            <td align="right">
                <xsl:value-of select="format-number(ClosingCapital div XRate, '###,##0')" />
            </td>
            <td align="right">
                TODO
            </td>
        </tr>
    </xsl:template>
   
    <xsl:template name="collectFundTotal">
        <xsl:param name="thisSubFund"/>
        <xsl:param name="curTot"/>
        <xsl:param name="newTot" select="$curTot + $thisSubFund/ClosingCapital div  $thisSubFund/XRate"/>
        <xsl:choose>
            <xsl:when test="$thisSubFund/following-sibling::SubFund">
                <xsl:call-template name="collectFundTotal">
                    <xsl:with-param name="thisSubFund" select="$thisSubFund/following-sibling::SubFund[1]"/>
                    <xsl:with-param name="curTot" select="$newTot"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$newTot"/>
            </xsl:otherwise>
        </xsl:choose>
       
    </xsl:template>
</xsl:stylesheet>

I will look at the other tasks next, try to understand what I did
- the recursion for the total
- breaking up the code in different templates

cheers

Geert
 
01.23.2008 at 08:21AM PST, ID: 20724622

Rank: Genius

Here is the full result,
I put the calculation of the percentage in a named template, for if you want to change the style

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl"
    >

    <xsl:variable name="totalConvertedGrand">
        <xsl:call-template name="collectGrandTotal">
            <xsl:with-param name="thisSubFund" select="//SubFund[1]"/>
            <xsl:with-param name="curTot" select="0"/>
        </xsl:call-template>
    </xsl:variable>
   
   
    <xsl:template match="/">
        <html>
            <body>
                <h2>
                    <xsl:value-of select="NAV/Funds/@Description" />
                </h2>
                <xsl:value-of select="NAV/Funds/@Date" />
                <br />
                <br />
                <br />
                <table border="1">
                    <xsl:apply-templates select="NAV/Funds/Fund"/>
                    <tr>
                        <td colspan="2">
                            Grand Total
                        </td>
                        <td align="right">
                            <xsl:value-of select="format-number(sum(//NAV/Funds/Fund/SubFund/ClosingCapital), '###,##0')" />
                        </td>
                        <td align="right">
                            <xsl:value-of select="format-number($totalConvertedGrand, '###,##0')" />
                        </td>
                        <td align="right">
                        </td>
                    </tr>
                </table>
            </body>
        </html>
    </xsl:template>
   
    <xsl:template match="Fund">
            <xsl:variable name="totalConvertedPerFund">
                <xsl:call-template name="collectFundTotal">
                    <xsl:with-param name="thisSubFund" select="SubFund[1]"/>
                    <xsl:with-param name="curTot" select="0"/>
                </xsl:call-template>
          </xsl:variable>
            <tr>
                <td colspan="5">
                    <xsl:value-of select="@Description"/>
                </td>
            </tr>
            <tr>
                <td>Fund Name</td>
                <td>Valuation Date</td>
                <td>Closing Capital</td>
                <td>Closing Capital (Converted)</td>
                <td>% of Grand Total</td>
            </tr>
            <xsl:variable name="FundTotal" select="SubFund/ClosingCapital"></xsl:variable>
            <xsl:variable name="FundTotalConverted" select="$FundTotal"></xsl:variable>
            <xsl:variable name="XRate" select="@XRate"></xsl:variable>
            <xsl:apply-templates select="SubFund"/>
            <tr>
                <td colspan="2">Total <xsl:value-of select="@Description"/></td>
                <td align="right">
                    <xsl:value-of select="format-number(sum($FundTotal), '###,##0')" />
                </td>
                <td align="right">
                    <xsl:value-of select="format-number($totalConvertedPerFund, '###,##0')" />
                </td>
                <td align="right">
                    <xsl:call-template name="getPercentage">
                        <xsl:with-param name="val" select="$totalConvertedPerFund"/>
                    </xsl:call-template>
                </td>
            </tr>
    </xsl:template>
   
    <xsl:template match="SubFund">
        <xsl:variable name="Converted" select="ClosingCapital div ../@XRate"></xsl:variable>
        <tr>
            <td>
                <xsl:value-of select="@Description" />
            </td>
            <td>
                <xsl:value-of select="ValuationDate" />
            </td>
            <td align="right">
                <xsl:value-of select="format-number(ClosingCapital, '###,##0')" />
            </td>
            <td align="right">
                <xsl:value-of select="format-number(ClosingCapital div XRate, '###,##0')" />
            </td>
            <td align="right">
                <xsl:call-template name="getPercentage">
                    <xsl:with-param name="val" select="ClosingCapital div XRate"/>
                </xsl:call-template>
            </td>
        </tr>
    </xsl:template>
   
    <xsl:template name="collectFundTotal">
        <xsl:param name="thisSubFund"/>
        <xsl:param name="curTot"/>
        <xsl:param name="newTot" select="$curTot + $thisSubFund/ClosingCapital div  $thisSubFund/XRate"/>
        <xsl:choose>
            <xsl:when test="$thisSubFund/following-sibling::SubFund">
                <xsl:call-template name="collectFundTotal">
                    <xsl:with-param name="thisSubFund" select="$thisSubFund/following-sibling::SubFund[1]"/>
                    <xsl:with-param name="curTot" select="$newTot"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$newTot"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
   
    <xsl:template name="collectGrandTotal">
        <xsl:param name="thisSubFund"/>
        <xsl:param name="curTot"/>
        <xsl:param name="newTot" select="$curTot + $thisSubFund/ClosingCapital div  $thisSubFund/XRate"/>
        <xsl:choose>
            <xsl:when test="$thisSubFund/following::SubFund">
                <xsl:call-template name="collectGrandTotal">
                    <xsl:with-param name="thisSubFund" select="$thisSubFund/following::SubFund[1]"/>
                    <xsl:with-param name="curTot" select="$newTot"/>
                </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$newTot"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
   
    <xsl:template name="getPercentage">
        <xsl:param name="val"/>
        <xsl:value-of select="format-number($val div $totalConvertedGrand * 100, '###.00')" />
    </xsl:template>
   
   
</xsl:stylesheet>
Accepted Solution
 
 
01.23.2008 at 10:13AM PST, ID: 20725766
Many thanks. I will read it carefully to try to understand every parts of it.
 
 
01.23.2008 at 10:14AM PST, ID: 20725785
welcome,
don't hesitate to ask if things are unclear
 
 
01.23.2008 at 10:24AM PST, ID: 20725915
You said:
>>(this is a lot easier in XSLT2.0)

How easier? What is required to use it?

This XSLT file is used by a VB.Net application using the XslCompiledTransform class. The current version is working with the XslCompiledTransform class.
 
 
01.23.2008 at 10:49AM PST, ID: 20726138
Your option would be to download and use Saxon .NET (www.saxonica.com)
use the Saxon9B, which is open source and downloadable via SourceForge

This requires some fiddling before it works in VB.NET
Since the above works, there is no hard reason for changing

cheers

Geert
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628