title: How to calculate sum of repeating elements using XSLT 1.0
body: <p>========================================================================</p>
  
  <pre><code>**Input XML:** 
  &lt;Customers&gt; 
  &lt;Customer&gt;
      &lt;Items&gt; 
          &lt;Item&gt;
              &lt;ItemId&gt;5639&lt;/ItemId&gt;
              &lt;ProductName&gt; Lawnmower &lt;/ProductName&gt;
              &lt;Quantity&gt;1&lt;/Quantity&gt;
              &lt;USPrice&gt;148.95&lt;/USPrice&gt;
              &lt;Comment&gt;Confirm this is electric&lt;/Comment&gt;
          &lt;/Item&gt;
          &lt;Item&gt;
              &lt;ItemId&gt;6000&lt;/ItemId&gt;
              &lt;ProductName&gt;Baby Monitor&lt;/ProductName&gt;
              &lt;Quantity&gt;2&lt;/Quantity&gt;
              &lt;USPrice&gt;39.98&lt;/USPrice&gt;
              &lt;Comment&gt;Confirm this is electric&lt;/Comment&gt;
          &lt;/Item&gt;
      &lt;/Items&gt;
  &lt;/Customer&gt;
  &lt;Customer&gt;
      &lt;Items&gt;
          &lt;Item&gt;
              &lt;ItemId&gt;7000&lt;/ItemId&gt;
              &lt;ProductName&gt; Cutter &lt;/ProductName&gt;
              &lt;Quantity&gt;1&lt;/Quantity&gt;
              &lt;USPrice&gt;15.95&lt;/USPrice&gt;
              &lt;Comment&gt;Confirm this is electric&lt;/Comment&gt;
          &lt;/Item&gt;
          &lt;Item&gt;
              &lt;ItemId&gt;9000&lt;/ItemId&gt;
              &lt;ProductName&gt; Laptop &lt;/ProductName&gt;
              &lt;Quantity&gt;5&lt;/Quantity&gt;
              &lt;USPrice&gt;2048.95&lt;/USPrice&gt;
              &lt;Comment&gt;Confirm this is electric&lt;/Comment&gt;
          &lt;/Item&gt;
      &lt;/Items&gt;
  &lt;/Customer &gt;
  </code></pre>
  
  <p> </p>
  
  <p><strong>Output XML:</strong> </p>
  
  <pre><code>&lt;Customers&gt; 
      &lt;Customer&gt; 
          &lt;TotalPurchase&gt;sum(USPRice)&lt;/TotalPurchase&gt; 
      &lt;/Customer&gt; 
      &lt;Customer&gt; 
          &lt;TotalPurchase&gt;sum(USPrice)&lt;/TotalPurchase&gt; 
      &lt;/Customer&gt; 
  &lt;/Customers&gt;
  </code></pre>
  
  <p><strong>XSLT Code:</strong></p>
  
  <pre><code>&lt;Customers&gt; 
     &lt;xsl:for-each select="Customers/Customer"&gt; 
       &lt;Customer&gt; 
          &lt;xsl:for-each select="Items/Item"&gt; 
             &lt;TotalPurchase&gt; 
                &lt;xsl:value-of select="sum(/USPrice)"/&gt; 
             &lt;/TotalPurchase&gt; 
          &lt;/xsl:for-each&gt; 
       &lt;/Customer&gt; 
     &lt;/xsl:for-each&gt; 
  &lt;/Customers&gt;
  </code></pre>
  
  <p>Can someone guide me how to achieve the output in XSLT 1.0. As the sum function didnt worked properly. It returs zero.</p>