Home > OS >  XSLT count element value and create new element with the value
XSLT count element value and create new element with the value

Time:02-08

I have the following source XML:

<ITEMS>
   <ITEM>
      <CODE>34155</CODE>
      <VARIANTS>
         <NAME>A</NAME>
         <STOCK>12</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>B</NAME>
         <STOCK>0</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>C</NAME>
         <STOCK>50</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>D</NAME>
         <STOCK>3</STOCK>
      </VARIANTS>
   </ITEM>
</ITEMS>

I would like to loop trough all <VARIANTS> elements count <STOCK> child value and create new <TOTALSTOCK> value under the parent element <ITEM>.

<ITEMS>
   <ITEM>
      <TOTALSTOCK>65</TOTALSTOCK>
      <CODE>34155</CODE>
      <VARIANTS>
         <NAME>A</NAME>
         <STOCK>12</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>B</NAME>
         <STOCK>0</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>C</NAME>
         <STOCK>50</STOCK>
      </VARIANTS>
      <VARIANTS>
         <NAME>D</NAME>
         <STOCK>3</STOCK>
      </VARIANTS>
   </ITEM>
</ITEMS>

Is such a mathematical operation possible with XSLT?

CodePudding user response:

There is no need "to loop trough all <VARIANTS> elements". You can do simply:

XSLT 1.0

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

<xsl:template match="/ITEMS">
    <xsl:copy>
        <xsl:for-each select="ITEM">
            <xsl:copy>
                <TOTALSTOCK>
                    <xsl:value-of select="sum(VARIANTS/STOCK)"/>
                </TOTALSTOCK>
                <xsl:copy-of select="*"/>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

CodePudding user response:

Please try the following XSLT.

Input XML

<ITEMS>
    <ITEM>
        <CODE>34155</CODE>
        <VARIANTS>
            <NAME>A</NAME>
            <STOCK>12</STOCK>
        </VARIANTS>
        <VARIANTS>
            <NAME>B</NAME>
            <STOCK>0</STOCK>
        </VARIANTS>
        <VARIANTS>
            <NAME>C</NAME>
            <STOCK>50</STOCK>
        </VARIANTS>
        <VARIANTS>
            <NAME>D</NAME>
            <STOCK>3</STOCK>
        </VARIANTS>
    </ITEM>
    <ITEM>
        <CODE>34770</CODE>
        <VARIANTS>
            <NAME>A</NAME>
            <STOCK>120</STOCK>
        </VARIANTS>
        <VARIANTS>
            <NAME>C</NAME>
            <STOCK>180</STOCK>
        </VARIANTS>
    </ITEM>
</ITEMS>

XSLT

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/ITEMS">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ITEM">
        <xsl:copy>
            <TOTALSTOCK>
                <xsl:value-of select="sum(./VARIANTS/STOCK)"/>
            </TOTALSTOCK>
            <xsl:copy-of select="*"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Output XML

<ITEMS>
  <ITEM>
    <TOTALSTOCK>65</TOTALSTOCK>
    <CODE>34155</CODE>
    <VARIANTS>
      <NAME>A</NAME>
      <STOCK>12</STOCK>
    </VARIANTS>
    <VARIANTS>
      <NAME>B</NAME>
      <STOCK>0</STOCK>
    </VARIANTS>
    <VARIANTS>
      <NAME>C</NAME>
      <STOCK>50</STOCK>
    </VARIANTS>
    <VARIANTS>
      <NAME>D</NAME>
      <STOCK>3</STOCK>
    </VARIANTS>
  </ITEM>
  <ITEM>
    <TOTALSTOCK>300</TOTALSTOCK>
    <CODE>34770</CODE>
    <VARIANTS>
      <NAME>A</NAME>
      <STOCK>120</STOCK>
    </VARIANTS>
    <VARIANTS>
      <NAME>C</NAME>
      <STOCK>180</STOCK>
    </VARIANTS>
  </ITEM>
</ITEMS>
  •  Tags:  
  • Related