'Group by Plant and Sum the quantity value - XSLT

I am beginner in XSLT and stuck at a point, need little help. I need to sum all the quantity in each plant. find below Input xml.

<?xml version='1.0' encoding='UTF-8'?>    
<stock>
    <records>
        <Plant>1001</Plant>     
        <quantity>1381</quantity>       
        <StorageLocation>1001</StorageLocation>
    </records>
    <records>       
        <Plant>1001</Plant>     
        <quantity>20</quantity>
        <StorageLocation>4001</StorageLocation>
    </records>
    <records>       
        <Plant>1002</Plant>     
        <quantity>0</quantity>      
        <StorageLocation>5001</StorageLocation>
    </records>
    <records>       
        <Plant>1002</Plant>     
        <quantity>28</quantity>     
        <StorageLocation>1901</StorageLocation>
    </records>
    <records>       
        <Plant>1003</Plant>     
        <quantity>1</quantity>      
        <StorageLocation>1006</StorageLocation>
    </records>
    <records>       
        <Plant>1003</Plant>     
        <quantity>0</quantity>      
        <StorageLocation>1001</StorageLocation>
    </records>
</stock>

I did good amount of research on the internet and found a piece of code. It is not working and am not able to fix it.

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>


    <!-- Match the stock element  -->
    <xsl:template match="records">
        <WebshopResponseArea>
            <!-- Group each records element by its plant -->
            <xsl:for-each-group select="records" group-by="Plant">
                                
                    <!-- Sum all the elements from the current group -->
                    <xsl:value-of select="sum(current-group()/quantity)" />
                
            </xsl:for-each-group>
        </WebshopResponseArea>
    </xsl:template>
</xsl:stylesheet>

Expected output xml is below.

<?xml version='1.0' encoding='UTF-8'?>
<WebshopResponseArea>
    <Records>        
        <AvailableStock>1401</AvailableStock>
        <Plant>1001</Plant>
    </Records>
    <Records>
        <AvailableStock>28</AvailableStock>
        <Plant>1002</Plant>
    </Records>
    <Records>
        <AvailableStock>1</AvailableStock>
        <Plant>1003</Plant>
    </Records>       
</WebshopResponseArea>

your help would be very much appreciated. thanks in advance.



Solution 1:[1]

Just change your match from

<xsl:template match="records">

to

<xsl:template match="stock">

And some more context and it wil look like this:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
  
  
  <!-- Match the stock element  -->
  <xsl:template match="stock">
    <WebshopResponseArea>
      <!-- Group each records element by its plant -->
      <xsl:for-each-group select="records" group-by="Plant">
        <Records>
          <AvailableStock>
            <xsl:value-of select="sum(current-group()/quantity)" />
          </AvailableStock>
          <xsl:copy-of select="current-group()[1]/Plant"/>
        </Records>
        
      </xsl:for-each-group>
    </WebshopResponseArea>
  </xsl:template>
</xsl:stylesheet>

Solution 2:[2]

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>

<!-- Match the stock element -->
<xsl:template match="/*">
    <WebshopResponseArea>
        
            <!--Group each records element by its plant -->
            <xsl:for-each-group select="//records" group-by="Plant">
                <Records>
                <Plant><xsl:value-of select="current-grouping-key()" /></Plant>
                <Quantity><xsl:value-of select="sum(current-group()/quantity)"/></Quantity>
                </Records>
            </xsl:for-each-group>
        
    </WebshopResponseArea>
</xsl:template>
</xsl:stylesheet>

Above code worked for my requirement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Mallashetty Avinash