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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:42:51+00:00 2026-06-05T03:42:51+00:00

I have a set of xml nodes with identical node names but one attribute

  • 0

I have a set of xml nodes with identical node names but one attribute that differentiate them, and an amount attribute:

<exampleNode typeOfnode="1" amount="100"/>
<exampleNode typeOfnode="1" amount="540"/>
<exampleNode typeOfnode="2" amount="200"/>
<exampleNode typeOfnode="2" amount="200"/>
<exampleNode typeOfnode="3" amount="10"/>
<exampleNode typeOfnode="3" amount="1"/>
<exampleNode typeOfnode="3" amount="110"/>
<exampleNode typeOfnode="3" amount="110"/>
<exampleNode typeOfnode="4" amount="110"/>

I’m using a recursive template to calculate the sum of the amounts, but only want to do that for a specific typeOfNode. Here is the code I’m using to call the template:

<xsl:call-template name="addition">
    <xsl:with-param name="currentValue">0</xsl:with-param>
    <xsl:with-param name="counter"><xsl:value-of select="count(//exampleNode[@typeOfnode= '1'])"/></xsl:with-param>
    <xsl:with-param name="typeOfnode">1</xsl:with-param>
</xsl:call-template>

<xsl:template name="addition">
    <xsl:param name="currentValue"/>
    <xsl:param name="counter"/>
    <xsl:param name="typeOfNode"/>
    <xsl:variable name="amount" select="//exampleNode[@typeOfNode = '$typeOfnode' and $counter]/@amount"/>
    <xsl:variable name="recursiveValue" select="number($recursiveValue + $amount)"/>
    <xsl:choose>
        <xsl:when test="number($counter - 1) > 0">
            <xsl:call-template name="addition">
                <xsl:with-param name="currentValue">
                    <xsl:value-of select="$recursiveValue"/>
                </xsl:with-param>
                <xsl:with-param name="counter">
                    <xsl:value-of select="number($counter - 1)"/>
                </xsl:with-param>
                <xsl:with-param name="agreementType">
                    <xsl:value-of select="$agreementType"/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$recursiveValue"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Having debugged through wiTH XMLspy, the amount variable is not being set, and I assume it’s because I’m screwing up the query. Anybody have any idea what I’m doing wrong?

  • 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-06-05T03:42:53+00:00Added an answer on June 5, 2026 at 3:42 am

    for a quick answer, see the last code block. For some comments on the code, read from here.

    1. How about fixing the recursive loop by using currentValue iso recursiveValue to seed the recursiveValue:

      <xsl:variable name="recursiveValue" select="number($currentValue + $amount)"/>
      
    2. There are some XSLT errors here, e.g. this does not look like it will work:

      <xsl:variable name="amount" select="//exampleNode[@typeOfNode = '$typeOfnode' and $counter]/@amount"/>
      

      if you mean to say “amount is the @amount of the $counter-th /exampleNode with @typeOfNode=$typeofnode, this is not the way.

      • ‘escaping’ the variable will not work, it will compare to the literal value “$typeOfnode”.
      • “and $counter” always evaluates to true, unless $counter is not set. Try “position()=$typeOfnode” but be aware that position will be the position of the current exampleNode in the entire set of exampleNode siblings. This could be fixed by using an intermediate variable where you ‘copy’ the exampleNodes that pass the filter, into which you test for the index using the $counter variable.
    3. Your snippet

          <xsl:with-param name="agreementType">
            <xsl:value-of select="$agreementType"/>
          </xsl:with-param>
      

      is probably missing some confuscation, did you mean typeOfnode iso agreementType? 😀

    All of that aside, there are simpler solutions. for instance a quite direct:

    <xsl:value-of select="sum(exampleNode[@typeOfnode='1']/@amount)"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one huge XML document. I have set of XSL representing each node
I have a class that serializes a set of objects (using XML serialization) that
i have one xml like, Declare @xmldata xml; set @xmldata =' <Customers> <Id>1</Id> <Name>foo</Name>
I have an XML field that I know will have at least one ChildNode
I have set up multiple targets in a single xml file. I expect all
I have set the two buttons in preference.xml file. I want to go to
I have this sql script DECLARE @XmlStr XML SET @XmlStr = '<EmployeeID> <Employee>48f9194f-8d46-e111-8849-0050569445f1</Employee> <Employee>7d725561-8d46-e111-8849-0050569445f2</Employee>
I have a set of records, stored as XML files, where the XML files
Do you have some predefined set of targets which all build.xml files you create
Have a JUNIT test set up as such @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({ /applicationContext.xml, /applicationContext-security.xml }) @TransactionConfiguration(defaultRollback

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.