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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T13:55:40+00:00 2026-05-16T13:55:40+00:00

I am new to xsl transformations and I have a question. I am looping

  • 0

I am new to xsl transformations and I have a question.
I am looping through an xml like this:

 <PO>
<Items>
  <Item>
     <Price>2</Price>
     <Quantity>5</Quantity>
  </Item>
  <Item>
     <Price>3</Price>
     <Quantity>2</Quantity>
  </Item>    
 </Items>
 <QuantityTotal></QuantityTotal>
 </PO>

Now I want to insert a value in the QuantityTotal node:
The value is the sum of price*quantity of all items, in this case (2*5)+(3*2) = 16
How can I do this, I tried it with a loop and variables, but variables are immutable so I don’t know how I can achieve this.

Thx for your help

  • 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-16T13:55:41+00:00Added an answer on May 16, 2026 at 1:55 pm

    Here is an XSLT solution — no extension functions required:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="QuantityTotal">
      <xsl:copy>
       <xsl:call-template name="sumProducts">
         <xsl:with-param name="pNodes" select="../Items/Item"/>
       </xsl:call-template>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template name="sumProducts">
      <xsl:param name="pNodes"/>
      <xsl:param name="pSum" select="0"/>
      <xsl:param name="pEname1" select="'Price'"/>
      <xsl:param name="pEname2" select="'Quantity'"/>
    
      <xsl:choose>
       <xsl:when test="not($pNodes)">
        <xsl:value-of select="$pSum"/>
       </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="sumProducts">
          <xsl:with-param name="pNodes" select=
          "$pNodes[position() > 1]"/>
          <xsl:with-param name="pSum" select=
          "$pSum
          +
           $pNodes[1]/*[name()=$pEname1]
          *
           $pNodes[1]/*[name()=$pEname2]
           "/>
        </xsl:call-template>
      </xsl:otherwise>
      </xsl:choose>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <PO>
        <Items>
            <Item>
                <Price>2</Price>
                <Quantity>5</Quantity>
            </Item>
            <Item>
                <Price>3</Price>
                <Quantity>2</Quantity>
            </Item>
        </Items>
        <QuantityTotal></QuantityTotal>
    </PO>
    

    the wanted result is produced:

    <PO>
       <Items>
          <Item>
             <Price>2</Price>
             <Quantity>5</Quantity>
          </Item>
          <Item>
             <Price>3</Price>
             <Quantity>2</Quantity>
          </Item>
       </Items>
       <QuantityTotal>16</QuantityTotal>
    </PO>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to xml and xsl, if this is a easy question, and
I am relatively new to XSL and I think this is a basic question.
This code:: xslProcessor = new XSLTProcessor(); xslProcessor.importStylesheet(xsl); result = xslProcessor.transformToFragment(xml, document); works fine in
I'm new in xml, sorry for the dumb question. I'm trying to create xsl
I am new to xsl xml transformation. For now, I have an xml file
I am very new to XSL Transformation here is the question. If I have
Very new to XSL (and XML for that matter), but I need to step
New to PHP and MySQL, have heard amazing things about this website from Leo
I'm currently working on a project which uses XSL-Transformations to generate HTML from XML.
I am new to XSL and I have a confusion whether we can read

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.