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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:09:19+00:00 2026-05-12T06:09:19+00:00

XML: <Budget> <Table1> <AllocID>1</AllocID> <Amount>1000</Amount> </Table1> <Table1> <AllocID>2</AllocID> <Amount>2000</Amount> </Table1> <Table1> <AllocID>3</AllocID> <Amount>3000</Amount> </Table1>

  • 0

XML:

<Budget>
    <Table1>
        <AllocID>1</AllocID>
        <Amount>1000</Amount>
    </Table1>
    <Table1>
        <AllocID>2</AllocID>
        <Amount>2000</Amount>
    </Table1>
    <Table1>
        <AllocID>3</AllocID>
        <Amount>3000</Amount>
    </Table1>
    <Table2>
        <AllocID>1</AllocID>
        <Split>100</Split>
    </Table2>
    <Table2>
        <AllocID>2</AllocID>
        <Split>100</Split>
    </Table2>
</Budget>

I am displaying the amounts in a table, but only if a “Split” value exists in Table2.

<xsl:for-each select="Budget/Table1">
    <xsl:for-each select="//Budget/Table2[AllocID = current()/AllocID]">
        <xsl:value-of select="Amount"/>
    </xsl:for-each>
</xsl:for-each>
//Total for the records here.

I need to get the sum of Amounts from Table1, but only if a value for the AllocID exists in Table2. So in this example, I only want the sum of Amount for AllocIDs 1 & 2. How would I do this in xslt without modifying the given datasets?

  • 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-12T06:09:20+00:00Added an answer on May 12, 2026 at 6:09 am

    Knowing nothing else about your input data (I have no idea why you chose not to use actual XML), I’m going to have to to guess a little:

    <Budget>
      <Table1>
        <AllocID>1000</AllocID>
        <AllocID>2000</AllocID>
        <AllocID>3000</AllocID>
      </Table1>
      <Table2>
        <AllocID>1000</AllocID>
        <AllocID>2000</AllocID>
      </Table2>
    </Budget>
    

    Easiest is the XPath sum() function along with the right XPath expression:

    <!-- this will return 3000 for the above input -->
    <xsl:template match="/" >
      <xsl:value-of select="
        sum(Budget/Table1/AllocID[. = //Budget/Table2/AllocID])
      " />
    </xsl:template>
    

    Running sums can also be calculated with a recursive function, like this:

    <!-- this will also return 3000 for the above input -->
    <xsl:template match="/" >
      <xsl:call-template name="total">
        <xsl:with-param name="nodes" select="
          Budget/Table1/AllocID[. = //Budget/Table2/AllocID]
        " />
      </xsl:call-template>
    </xsl:template>
    
    <xsl:template name="total">
      <xsl:param name="nodes" />
    
      <xsl:choose>
        <!-- either there is something to calculate... -->
        <xsl:when test="string(number($nodes[1])) != 'NaN'">
          <xsl:variable name="subtotal">
            <xsl:call-template name="total">
              <xsl:with-param name="nodes" select="$nodes[position() &gt; 1]" />
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="number($nodes[1]) + $subtotal" /> 
        </xsl:when>
        <!-- ...or we assume 0 -->
        <xsl:otherwise>
          <xsl:value-of select="0" /> 
        </xsl:otherwise>
      </xsl:choose>
    
    </xsl:template>
    

    This is slower, but allows for greater flexibility in the calculation process. You can replace all the non-numeric values with 0, for example. Or use different values of the given nodes according to your own rules.

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

Sidebar

Ask A Question

Stats

  • Questions 183k
  • Answers 183k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You have two choices, urlencode() or http_build_query() // Using urlencode()… May 12, 2026 at 4:42 pm
  • Editorial Team
    Editorial Team added an answer When you do this:- Set excelRecordset = recordset.clone You replace… May 12, 2026 at 4:42 pm
  • Editorial Team
    Editorial Team added an answer Write a trigger on the table; on the value you… May 12, 2026 at 4:42 pm

Related Questions

I have to invoke a web service with a single parameter, a String in
XML: <A><B></B></A> or <A></A> I want to get all A nodes that do not
My XML: <root> <child> <childOfChild> <anotherLostChild> <currentSelectedNode> SOME TEXT </currentSelectedNode> </anotherLostChild> </childOfChild> </child> </root>
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.