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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:24:51+00:00 2026-06-18T18:24:51+00:00

So basically I need a function that loops through XML nodes and IF a

  • 0

So basically I need a function that loops through XML nodes and IF a condition is true, it adds that value to a variable. I am aggregating social posts and need to count how many of each social post is in the feed. Here is my XML:

 <feed>
   <channel>
     <sources>
       <source>
           <name>Facebook</name>
           <count>3</count>
       </source>
       <source>
           <name>Twitter</name>
           <count>2</count>
       </source>
       <source>
           <name>Twitter</name>
           <count>8</count>
        </source>
     </sources>
   </channel>
  </feed>

The catch is the same source can appear multiple times, and I need to add those together. So I would need a twitter count of 10 for the above XML. Here is where I am at so far:

<xsl:variable name="num_tw">
<xsl:for-each select="feed/channel/sources/source">
  <xsl:choose>
    <xsl:when test="name, 'twitter')">
      <xsl:value-of select="count"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
</xsl:variable>

<xsl:variable name="num_fb">
<xsl:for-each select="feed/channel/sources/source">
  <xsl:choose>
    <xsl:when test="name, 'facebook')">
      <xsl:value-of select="count"/>
    </xsl:when>
    <xsl:otherwise></xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
</xsl:variable>

This doesn’t work because if there is two twitter feeds it puts the numbers side by side and outputs “28” instead of “10”. Any help is appreciated!

  • 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-18T18:24:53+00:00Added an answer on June 18, 2026 at 6:24 pm

    You don’t need to iterate over the nodes with xsl:for-each here. Instead you can just make use of the sum operator. For example, your num_tw variable can just be re-written like so

    <xsl:variable name="num_tw" select="sum(feed/channel/sources/source[name='Twitter']/count)"/>
    

    However, do you really want to hard-code your feed names here? This is really a ‘grouping’ issue, and in XSLT 1.0 you use a technique called Muencian Grouping to solve it. In your case, you want to group source elements by their name element, and so you define a key like so:

    <xsl:key name="source" match="source" use="name" />
    

    Then, you look at all the source elements, and pick the one that is first in the group for their given name element:

    <xsl:apply-templates 
       select="feed/channel/sources/source[generate-id() = generate-id(key('source', name)[1])]" />
    

    Then, within the template that matches this, you can sum up the counts like so:

    <xsl:value-of select="sum(key('source', name)/count)" />
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:key name="source" match="source" use="name" />
    
        <xsl:template match="/">
        <xsl:apply-templates select="feed/channel/sources/source[generate-id() = generate-id(key('source', name)[1])]" />
    
        </xsl:template>
    
        <xsl:template match="source">
            <source>
                <xsl:copy-of select="name" />
                <count><xsl:value-of select="sum(key('source', name)/count)" /></count>
            </source>
        </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML, the following is output:

    <source>
       <name>Facebook</name>
       <count>3</count>
    </source>
    <source>
       <name>Twitter</name>
       <count>10</count>
    </source>
    

    Note that if you did really want to find out the count of a specific feed, like ‘Facebook’ it would still be preferably to use the key here. For example:

    <xsl:variable name="num_fb" select="sum(key('source', 'Facebook')/count)"/>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I need to do is write a function that takes in a
basically need to change the value that - admin_url() returns any idea?
I basically need a custom function to be used only when, for example, the
I am trying to create an OCaml function rv that will go through a
I'm trying to get a homemade path navigation function working - basically I need
I recently wrote a small number-crunching program that basically loops over an N-dimensional grid
I need to build a push system in django, basicly its function is to
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
I basically need to get user input: gets.chomp(input?) And then to convert the given
I basically need to check if there is an easier way to do this

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.