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 3495226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:06:04+00:00 2026-05-18T12:06:04+00:00

Is it possible to perform conditional totalling in xsl? I have the following xml

  • 0

Is it possible to perform conditional totalling in xsl?

I have the following xml sample:

<?xml version="1.0" encoding="utf-8"?>
<export>
<stats set="1">
  <columns>
    <column id="0">
      <sum>100</sum>
    </column>
    <column id="1">
      <sum>102</sum>
    </column>
    <column id="2">
      <sum>12</sum>
    </column>
  </columns>
</stats>
  <stats set="2">
    <columns>
      <column id="0">
        <sum>100</sum>
      </column>
      <column id="1">
        <sum>101</sum>
      </column>
      <column id="2">
        <sum>19</sum>
      </column>
    </columns>
  </stats>
</export>

Is it possible to compute the total of all columns in each stat set where they are not equal to one another? So it would output the following:

               Set 1       Set 2       Diff(Set 1 - Set 2)
Total (Diff)   114         120           -6
column 2       102         101            1  
column 3       12          19            -7 

So in the output column 1 would be omitted as the sum in the two stat sets is the same.

I can get my xsl to output the columns that are different but unsure how to total these up and put in the total row.

Many thanks,

Andez

  • 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-18T12:06:05+00:00Added an answer on May 18, 2026 at 12:06 pm

    This transformation (64 lines):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
     <xsl:key name="kColByPosAndVal" match="column"
      use="concat(count(preceding-sibling::*),
                  '+',
                  sum)"/>
     <xsl:key name="kIdByVal" match="column/@id"
      use="."/>
    
     <xsl:template match="/*">
      <xsl:variable name="vsum1" select=
       "sum(stats[@set=1]/*/column
                      [not(key('kColByPosAndVal',
                                concat(count(preceding-sibling::*),
                                      '+',
                                       sum)
                               )[2]
                           )])"/>
      <xsl:variable name="vsum2" select=
       "sum(stats[@set=2]/*/column
                      [not(key('kColByPosAndVal',
                                concat(count(preceding-sibling::*),
                                      '+',
                                       sum)
                               )[2]
                           )])"/>
    
                     Set 1          Set 2       Diff(Set 1 - Set 2)
      Total (Diff)   <xsl:text/>
      <xsl:value-of select="$vsum1"/>
      <xsl:text>             </xsl:text>
      <xsl:value-of select="$vsum2"/>
      <xsl:text>             </xsl:text>
      <xsl:value-of select="$vsum1 -$vsum2"/>
      <xsl:text>&#xA;</xsl:text>
    
      <xsl:for-each select=
       "*/*/column/@id
           [generate-id()
           = generate-id(key('kIdByVal',.)[1])
           ]
           [not(key('kColByPosAndVal',
                    concat(count(../preceding-sibling::*),
                           '+',
                           ../sum)
                    )[2]
                )]">
        <xsl:variable name="vcolSet1" select=
         "/*/stats[@set=1]/*/column[@id=current()]/sum"/>
    
        <xsl:variable name="vcolSet2" select=
         "/*/stats[@set=2]/*/column[@id=current()]/sum"/>
      Column <xsl:value-of select=".+1"/><xsl:text/>
        <xsl:text>       </xsl:text>
        <xsl:value-of select="$vcolSet1"/>
        <xsl:text>             </xsl:text>
        <xsl:value-of select="$vcolSet2"/>
        <xsl:text>             </xsl:text>
        <xsl:value-of select="$vcolSet1 -$vcolSet2"/>
        <xsl:text>&#xA;</xsl:text>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <export>
    <stats set="1">
      <columns>
        <column id="0">
          <sum>100</sum>
        </column>
        <column id="1">
          <sum>102</sum>
        </column>
        <column id="2">
          <sum>12</sum>
        </column>
      </columns>
    </stats>
      <stats set="2">
        <columns>
          <column id="0">
            <sum>100</sum>
          </column>
          <column id="1">
            <sum>101</sum>
          </column>
          <column id="2">
            <sum>19</sum>
          </column>
        </columns>
      </stats>
    </export>
    

    produces the wanted, correct result:

                     Set 1          Set 2       Diff(Set 1 - Set 2)
      Total (Diff)   114             120             -6
    
      Column 2       102             101             1
    
      Column 3       12             19             -7
    

    Explanation:

    1. The key named kColByPosAndVal is used to select all columns that have a given position (among all column siblings) and a given value for their sum child-element.

    2. The key named kIdByVal is used in Muenchian grouping to find all different values for the id attribute.

    3. The two totals are calculated summing only those columns, whose kColByPosAndVal key selects only one column element (if it selects two column elements, they both are at the same position and have the same sum).

    4. The rest should be easy to understand.

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

Sidebar

Related Questions

Is it possible to perform a global reversed-find on NHibernate-managed objects? Specifically, I have
How is it possible to perform a conditional statement in html template in GAE
Is it possible to perform an asynchronous wait (read : non-blocking) on a conditional
Is it possible to perform a partial or patch commit in Team Foundation? I
Is it possible to perform query expansion with Lucene 3.6 using any ontology? I've
Is it possible to perform a replace-string operation inside of a rectangular region in
is it possible to perform a first commit on a new repository using a
Is it possible to perform some custom processing when Windsor instantiates a type? Something
Is it possible to perform an action when a UITableViewCell is highlighted? For example
Is it possible to perform an active wifi scan on android without using the,

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.