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

  • Home
  • SEARCH
  • 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 3601856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:40:24+00:00 2026-05-18T20:40:24+00:00

I have a small fixed set of nodes: <a> , <b> , <c> ,

  • 0

I have a small fixed set of nodes: <a>, <b>, <c>, <d>. Each node can have a value of either 1 or 0.
Also, each node has a weight: 1, 2, 3, 4, respectively. Node attributes are not used.
How can I sum the value of each node multiplied by its weight using XSLT 1.0? Example:

<a>0</a>
<b>1</b>
<c>0</c>
<d>1</d>

Sum: 6

<a>1</a>
<b>1</b>
<c>0</c>
<d>0</d>

Sum: 3

<a>0</a>
<b>1</b>
<c>1</c>
<d>1</d>

Sum: 9

  • 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-18T20:40:24+00:00Added an answer on May 18, 2026 at 8:40 pm

    This XSLT 1.0 transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/*">
      <xsl:call-template name="sumWeighted"/>
     </xsl:template>
    
     <xsl:template name="sumWeighted">
       <xsl:param name="pList" select="*"/>
       <xsl:param name="pIndex" select="1"/>
       <xsl:param name="pAccum" select="0"/>
    
       <xsl:choose>
        <xsl:when test="$pIndex > count($pList)">
         <xsl:value-of select="$pAccum"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="sumWeighted">
            <xsl:with-param name="pList" select="$pList"/>
            <xsl:with-param name="pIndex"
                 select="$pIndex+1"/>
            <xsl:with-param name="pAccum"
                 select="$pAccum+$pList[$pIndex]*$pIndex"/>
          </xsl:call-template>
        </xsl:otherwise>
       </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied to the provided XML documents:

    <t>
        <a>0</a>
        <b>1</b>
        <c>0</c>
        <d>1</d>
    </t>
    

    ,

    <t>
        <a>1</a>
        <b>1</b>
        <c>0</c>
        <d>0</d>
    </t>
    

    and

    <t>
        <a>0</a>
        <b>1</b>
        <c>1</c>
        <d>1</d>
    </t>
    

    produces the wanted results, respectively:

    6

    3

    9


    Reference:

    If you want to get your hands dirty with really complicated math calculations using XSLT, see this:

    http://fxsl.sourceforge.net/articles/xslCalculator/The%20FXSL%20Calculator.html


    XPath 2.0 / XSLT 2.0 Solution:

    Use just this XPath 2.0 one-liner:

       sum(/*/*/(number()*position()))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small fixed set of nodes: <a> , <b> , <c> ,
I have small page which has label, DropDownList and a submit button. <div> <asp:label
We have a web service which serves small, arbitrary segments of a fixed inventory
I can't make td Date to have fixed height. If there is less in
I have a small java application with a swing gui. I have set a
I have some buttons which are fixed either side of my page, they work
Have a small doubt with static method. Documentation says that Static method can't access
I have small problem with my .net 2.0 winforms application. I want to embed
Where I work, we have small teams of 2 - 5 people. As a
I have a small JS function that does Ajax for me and another like

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.