Perform SUM operation in XSLT -
in below input have check supplier code if match of node supplier code have perform sum operation on quantity.otherwise directly map quantity.
input:
<move-afile> <afile> <item> <suppliercode>1</suppliercode> <packnumber>1234</packnumber> <quantity>12</quantity> </item> <item> <suppliercode>2</suppliercode> <packnumber>567</packnumber> <quantity>3</quantity> </item> <item> <suppliercode>1</suppliercode> <packnumber>567</packnumber> <quantity>8</quantity> </item> <item> <suppliercode>3</suppliercode> <packnumber>126</packnumber> <quantity>11</quantity> </item> <item> <suppliercode>4</suppliercode> <packnumber>876</packnumber> <quantity>32</quantity> </item> </afile> </move-afile>
if supplier code equal perform sum operation on quantity,otherwise directly map quantity.
output:
<a> <target> <item> <suppliercode>1</suppliercode> <packnumber>1234</packnumber> <quantity>20</quantity> </item> <item> <suppliercode>2</suppliercode> <packnumber>567</packnumber> <quantity>3</quantity> </item> <item> <suppliercode>1</suppliercode> <packnumber>567</packnumber> <quantity>20</quantity> </item> <item> <suppliercode>3</suppliercode> <packnumber>126</packnumber> <quantity>11</quantity> </item> <item> <suppliercode>4</suppliercode> <packnumber>876</packnumber> <quantity>32</quantity> </item> </target> </a>
i need sum logic in separate temporary variable below.
<varaible name=tempvar> <xsl:choose> <xsl:when suppliercode=suppliercode> <xsl:value-of select=sum(quntity)/> <xsl:when> <xsl:otherwise> <xsl:value-of select=quntity/> </xsl:otherwise> </xsl:choose> </variable>
this stylesheet require. copies elements item
downwards, , has special template change value of quantity
adding values of quantity
elements item
elements have same value of suppliercode
.
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:strip-space elements="*"/> <xsl:output method="xml" indent="yes" encoding="utf-8" omit-xml-declaration="yes"/> <xsl:template match="/"> <a> <target> <xsl:apply-templates select="move-afile/afile/item"/> </target> </a> </xsl:template> <xsl:template match="node()"> <xsl:copy> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template> <xsl:template match="quantity"> <xsl:variable name="supplier-code" select="parent::item/suppliercode"/> <xsl:copy> <xsl:value-of select="sum(ancestor::afile/item[suppliercode = $supplier-code]/quantity)"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
output
<a> <target> <item> <suppliercode>1</suppliercode> <packnumber>1234</packnumber> <quantity>20</quantity> </item> <item> <suppliercode>2</suppliercode> <packnumber>567</packnumber> <quantity>3</quantity> </item> <item> <suppliercode>1</suppliercode> <packnumber>567</packnumber> <quantity>20</quantity> </item> <item> <suppliercode>3</suppliercode> <packnumber>126</packnumber> <quantity>11</quantity> </item> <item> <suppliercode>4</suppliercode> <packnumber>876</packnumber> <quantity>32</quantity> </item> </target> </a>
update put total variable before using it, can replace last template this
<xsl:template match="quantity"> <xsl:variable name="supplier-code" select="parent::item/suppliercode"/> <xsl:variable name="total" select="sum(ancestor::afile/item[suppliercode = $supplier-code]/quantity)"/> <xsl:copy> <xsl:value-of select="$total"/> </xsl:copy> </xsl:template>
which sets value of $total
sum of quantities same suppplier code.
Comments
Post a Comment