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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:40:43+00:00 2026-05-22T14:40:43+00:00

i have xml: <people> <man age=20 /> <man age=40 /> <man age=30 /> <man

  • 0

i have xml:

<people>
  <man age="20" />
  <man age="40" />
  <man age="30" />
  <man age="80" />
<people>

with xsl i am trying to output:

first age:20
first and second age (combined): 60
first second and third age(combined) :110
first second third and fouth age(combined) :190

i know how to select the ages, but how do i add them together and write it out?

Also note that the <man> elements can be more than just 4.

  • 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-22T14:40:44+00:00Added an answer on May 22, 2026 at 2:40 pm

    Ok, I just read that you just need the numbers, so the following stripped xslt

    <xsl:stylesheet
            version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" indent="yes" />
    
        <xsl:variable name="elements" select="/people/man"/>
        <xsl:variable name="count" select="count($elements)"/>
    
        <!-- Main Entry point -->
        <xsl:template match="/">
            <xsl:call-template name="addthem">
                <xsl:with-param name="pos" select="1"/>
                <xsl:with-param name="sum" select="$elements[1]/@age"/>
            </xsl:call-template>
        </xsl:template>
    
        <!-- recursive calling template to sum up the ages -->
        <xsl:template name="addthem">
            <xsl:param name="pos"/>
            <xsl:param name="sum"/>
    
            <xsl:value-of select="$sum"/>
            <xsl:text>
    </xsl:text>
    
            <!-- recursive call to sum up the ages -->
            <xsl:if test="$pos lt number($count)">
                <xsl:call-template name="addthem">
                    <xsl:with-param name="pos" select="$pos + 1"/>
                    <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    produces following on your sample input-

    20
    60
    90
    170
    

    The template (Original one with labels and stuff):

    <xsl:stylesheet
            version="2.0"
            xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" indent="yes" />
    
        <xsl:variable name="txtlabels" select="tokenize('first,second,third,fourth,fifth,sixth,seventh,eights,ninth,tenth,eleventh,twelveth,thirteenth,fourteenth,fifteenth', ',')"/>
    
        <!-- Util template to generate labels -->
        <xsl:template name="getlabel">
            <xsl:param name="startat" select="1"/>
            <xsl:param name="idx"/>
    
            <xsl:if test="number($startat) lt number($idx)">
                <xsl:value-of select="$txtlabels[$startat]"/>
                <xsl:text> </xsl:text>
                <xsl:call-template name="getlabel">
                    <xsl:with-param name="startat" select="$startat + 1"/>
                    <xsl:with-param name="idx" select="$idx"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    
    
        <!-- Main Entry point -->
        <xsl:template match="/">
            <xsl:variable name="count">
                <xsl:value-of select="count(/people/man)"/>
            </xsl:variable>
    
            <xsl:call-template name="addthem">
                <xsl:with-param name="count" select="count(/people/man)"/>
                <xsl:with-param name="pos" select="1"/>
                <xsl:with-param name="sum" select="/people/man[1]/@age"/>
                <xsl:with-param name="elements" select="/people/man"/>
            </xsl:call-template>
        </xsl:template>
    
        <!-- recursive calling template to sum up the ages -->
        <xsl:template name="addthem">
            <xsl:param name="count"/>
            <xsl:param name="pos"/>
            <xsl:param name="sum"/>
            <xsl:param name="elements"/>
    
            <!-- get the label prefix, without the 'and' clause -->
            <xsl:variable name="thelabelprefix">
                <xsl:call-template name="getlabel">
                    <xsl:with-param name="startat" select="1"/>
                    <xsl:with-param name="idx" select="$pos"/>
                </xsl:call-template>
            </xsl:variable>
    
            <!-- Now append the 'and' clause, if required, to the labels!!! -->
            <xsl:variable name="thelabel">
                <xsl:choose>
                    <xsl:when test="number($pos) eq 1">
                        <xsl:value-of select="$txtlabels[$pos]"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of 
    select="concat($thelabelprefix, ' and ', $txtlabels[$pos])"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
    
            <xsl:value-of select="$thelabel"/>
            <xsl:text> : </xsl:text> <xsl:value-of select="$sum"/>
            <xsl:text>
    
            </xsl:text>
    
            <!-- recursive call to sum up the ages -->
            <xsl:if test="$pos lt number($count)">
                <xsl:call-template name="addthem">
                    <xsl:with-param name="count" select="$count"/>
                    <xsl:with-param name="pos" select="$pos + 1"/>
                    <xsl:with-param name="sum" select="number($sum) + number($elements[$pos + 1]/@age)"/>
                    <xsl:with-param name="elements" select="$elements"/>
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    produces following output for your input xml:

    first : 20
    first  and second : 60
    first second  and third : 90
    first second third  and fourth : 170
    

    I have added comments inside, let me know if you need further help.
    It basically uses two recursive templates one each for the ‘labels’ and other for the addition.

    And, Your sample output should read 90 and 170 instead of 110 and 190 or your sample input should say age=50 instead of age=30

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

Sidebar

Related Questions

I have a file xml as this: <data> <first> <city> city </city> <people> 400
I have this below XML file <book> <person> <first>Kiran</first> <last>Pai</last> <age>22</age> </person> <person> <first>Bill</first>
I Have this XML File <?xml version=1.0 standalone=yes?> <Root> <Object> <referenceName>People</referenceName> <query>select * from
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
I have an XML document that's being generated from some content that people are
Say we have the following XML: <people> <person> <name>Jake</name> <skills> <skill>JavaScript</skill> <skill>HTML</skill> <skill>Flex</skill> <skill>CSS</skill>
I have a stored proc that does inserts of people. I have an xml
I have an XML file in res/values. The file contains a list of people.
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...
I have XML in the following form that I want to parse with PHP

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.