xml - How can I put result of a calculation with xsl in a new element -
i use xsl calculate cumulative total per element "basisproduct". output has put in same xml, after "totals" in new elements, this:
<totals> <totalproduct> <basisproduct>110</basisproduct> <cumul_amount>1,52</custinvoice_lineamount> </totalproduct> <totalproduct> <basisproduct>198</basisproduct> <cumul_amount>294,77</custinvoice_lineamount> </totalproduct> <totalproduct> <basisproduct>992</basisproduct> <cumul_amount>163,32</custinvoice_lineamount> </totalproduct> <totalproduct> <basisproduct>993</basisproduct> <cumul_amount>193,78</custinvoice_lineamount> </totalproduct> <totals> <-----i have xml input.------> <?xml version="1.0" encoding="utf-8"?> <report name="salesinvoice"> <reportname>salesinvoice</reportname> <invoice> <invoicingname>test company</invoicingname> </invoice> <conditionalarea> <bodycustinvoice> <basisproduct>110</basisproduct> <custinvoice_lineamount>1,52</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20,11</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20,22</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>3033</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30,34</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30,35</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.45</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30,46</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>100.99</custinvoice_lineamount> </bodycustinvoice>, </conditionalarea> <totals> </totals> </report>
the xsl use calculate cumlative sum of "basisproduct" is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:decimal-format name="eu" decimal-separator="," grouping-separator="."/> <xsl:key name="ktype" match="basisproduct" use="."/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="totalsbasis"> <totalsbasis> <xsl:for-each select="/*/*/*/basisproduct [generate-id() = generate-id(key ('ktype', .)[1])]"> <xsl:variable name="currprod" select="."/> <totalproduct> <xsl:copy-of select="."/> <cumul_amount> <xsl:value-of select="sum(/*/*/bodycustinvoicetrans [basisproduct=$currprod]/custinvoicetrans_lineamount)"/> </cumul_amount> </totalproduct> </xsl:for-each> </totalsbasis> </xsl:template> </xsl:stylesheet>
what should use identity transform , override totals
element.
edit: based on comment op in @james.garriss answer, modified calculation sum everything.
input xml
<report name="salesinvoice"> <reportname>salesinvoice</reportname> <invoice> <invoicingname>test company</invoicingname> </invoice> <conditionalarea> <bodycustinvoice> <basisproduct>110</basisproduct> <custinvoice_lineamount>1.52</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20.11</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20.22</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.33</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30.34</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.35</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.45</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30.46</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>100.99</custinvoice_lineamount> </bodycustinvoice> </conditionalarea> <totals> </totals> </report>
xslt stylesheet
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:key name="ktype" match="basisproduct" use="."/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="totals"> <totals> <xsl:for-each select="/*/*/*/basisproduct [generate-id() = generate-id(key ('ktype', .)[1])]"> <xsl:variable name="currprod" select="."/> <totalproduct> <xsl:copy-of select="."/> <cumul_amount> <!--<xsl:value-of select= "/*/*/*[basisproduct = current()][last()]/custinvoice_lineamount + sum(/*/*/*[basisproduct = current()][last()]/preceding-sibling::*/custinvoice_lineamount)"/>--> <xsl:value-of select="sum(/*/*/bodycustinvoice[basisproduct=$currprod]/custinvoice_lineamount)"/> </cumul_amount> </totalproduct> </xsl:for-each> </totals> </xsl:template> </xsl:stylesheet>
output xml (using xalan :-( )
<report name="salesinvoice"> <reportname>salesinvoice</reportname> <invoice> <invoicingname>test company</invoicingname> </invoice> <conditionalarea> <bodycustinvoice> <basisproduct>110</basisproduct> <custinvoice_lineamount>1.52</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20.11</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>20.22</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.33</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30.34</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.35</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>992</basisproduct> <custinvoice_lineamount>30.45</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>993</basisproduct> <custinvoice_lineamount>30.46</custinvoice_lineamount> </bodycustinvoice> <bodycustinvoice> <basisproduct>198</basisproduct> <custinvoice_lineamount>100.99</custinvoice_lineamount> </bodycustinvoice> </conditionalarea> <totals> <totalproduct> <basisproduct>110</basisproduct> <cumul_amount>1.52</cumul_amount> </totalproduct> <totalproduct> <basisproduct>198</basisproduct> <cumul_amount>141.32</cumul_amount> </totalproduct> <totalproduct> <basisproduct>992</basisproduct> <cumul_amount>91.13</cumul_amount> </totalproduct> <totalproduct> <basisproduct>993</basisproduct> <cumul_amount>60.8</cumul_amount> </totalproduct> </totals> </report>
Comments
Post a Comment