Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:46:42+00:00 2026-05-26T06:46:42+00:00

The source XML (this is just foobar data, in reality it is thousands of

  • 0

The source XML (this is just foobar data, in reality it is thousands of rows wich can be both positive and negative):

<accounting>
    <entry id="1">
        <accounting_date>2010-10-29</accounting_date>
        <transfer_date>2010-10-29</transfer_date>
        <description>Start balance</description>
        <vat>0</vat>
        <sum>87287</sum>
    </entry>
    <entry id="2">
        <accounting_date>2011-01-24</accounting_date>
        <transfer_date>2011-02-17</transfer_date>
        <description>Bill 1</description>
        <vat>175</vat>
        <sum>875</sum>
    </entry>
    <entry id="3">
        <accounting_date>2011-01-31</accounting_date>
        <transfer_date>2011-01-18</transfer_date>
        <description>Bill 2</description>
        <vat>350</vat>
        <sum>1750</sum>
    </entry>
</accounting>

I want to transform this XML to an HTML table to display to the user. Most of the transformation is just putting values in the right places, but the balance-field is giving me headache.

My XSLT (that does not work):

<table>
    <tr>
        <th>Accounting date</th>
        <th>Description</th>
        <th>Sum</th>
        <th>Balanche</th>
    </tr>
    <xsl:for-each select="/accounting/entry">
        <tr>
            <td><xsl:value-of select="accounting_date" /></td>
            <td><xsl:value-of select="description" /></td>
            <td><xsl:value-of select="sum" /></td>
            <td><xsl:value-of select="sum(../entry[position() &lt; current()/position()]/sum)" /></td><!-- This XPath is the problem! -->
        </tr>
    </xsl:for-each>
</table>

Expected result:

<table>
    <tr>
        <th>Accounting date</th>
        <th>Description</th>
        <th>Sum</th>
        <th>Balanche</th>
    </tr>
    <tr>
        <td>2010-10-29</td>
        <td>Start balance</td>
        <td>87287</td>
        <td>87287</td>
    </tr>
    <tr>
        <td>2011-01-24</td>
        <td>Bill 1</td>
        <td>875</td>
        <td>88162</td>
    </tr>
    <tr>
        <td>2011-01-31</td>
        <td>Bill 2</td>
        <td>1750</td>
        <td>89912</td>
    </tr>
</table>

Chrome is blank, and Firefox gives me:

Error loading stylesheet: XPath parse failure: Name or Nodetype test expected:

I’m stuck, please help. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:46:43+00:00Added an answer on May 26, 2026 at 6:46 am

    The best solution might depend a bit on whether you are using XSLT 1.0 or XSLT 2.0. You really need to say, since at present there’s a roughly even mix of both in use in the field. (It seems you’re running it in the browser, which suggests you want a 1.0 solution, so that’s what I’ve given you).

    But either way, recursion is your friend. In this case, “sibling recursion” where you write a template to process an entry, and it does apply-templates to process the next entry, passing the total so far as a parameter: something like this

    <xsl:template match="entry">
      <xsl:param name="total-so-far" select="0"/>
             <tr>
                <td><xsl:value-of select="accounting_date" /></td>
                <td><xsl:value-of select="description" /></td>
                <td><xsl:value-of select="sum" /></td>
                <td><xsl:value-of select="$total-so-far + sum"/></td><
            </tr>  
        <xsl:apply-templates select="following-sibling::entry[1]">
          <xsl:with-param name="total-so-far" select="$total-so-far + sum"/>
        </xsl:apply-templates>
    </xsl:template>
    

    Then you need to start the process off with

    <xsl:template match="accounting">
     <table>
       <xsl:apply-templates select="entry[1]"/>
     </table>
    </xsl:template>
    

    If there are thousands of rows then this could cause stack overflow in an XSLT processor that doesn’t do tail call optimisation. I’ve no idea whether the XSLT processors in today’s browsers implement this optimisation or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to transform an XML document. The source XML looks like this: <svc:ElementList>
I have only this in my mxml source code: <?xml version=1.0 encoding=utf-8?> <mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
Please help me with this xslt transformation. Source Xml <xml> <test>This is a <bold>sample</bold>
I have an application which gets it's data ( XML ) from a source
I have a source XML file that is used to create C# files that
Have a source xml document that uses namespace containing prefixes and a default namespace.
I would like to process a frame element in my source xml file using
I have an XML source in a Microsoft SSIS 2005 package and when I
I want some slightly 'dynamic' XML source files. I want some of the element
I have the following XML source structure: <turnovers> <turnover repid=1 amount=500 rate=0.1/> <turnover repid=5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.