Reputation: 105
INPUT
<root>
<TL>
<msg>
<output_getquerydata>
<queries>
<query name="q1">
<parameters>
<parameter name="id">906OREA</parameter>
</parameters>
<queryErrors/>
<queryResults>
<record>
<column name="actionState">sdss</column>
</record>
</queryResults>
</query>
<query name="q2">
<parameters>
<parameter name="resCode">CTL-3819</parameter>
<parameter name="prodCode">89CMID</parameter>
</parameters>
<queryErrors/>
<queryResults>
<record id="1">
<column name="resource_externalId">CTL-000002</column>
<column name="compartmentCode">CTL-3819-01</column>
<column name="ExternalProductId"/>
</record>
<record id="2">
<column name="resource_externalId">CTL-000002</column>
<column name="compartmentCode">CTL-3819-02</column>
<column name="ExternalProductId"/>
</record>
<record id="3">
<column name="resource_externalId"/>
<column name="compartmentCode"/>
<column name="position"/>
<column name="ExternalProductId">316442</column>
</record>
</queryResults>
</query>
<query name="q2">
<parameters>
<parameter name="resCode">CTL-3819</parameter>
<parameter name="prodCode">91VPRM</parameter>
</parameters>
<queryErrors/>
<queryResults>
<record id="1">
<column name="resource_externalId">CTL-000002</column>
<column name="compartmentCode">CTL-3819-01</column>
<column name="position">1</column>
<column name="ExternalProductId"/>
</record>
<record id="2">
<column name="resource_externalId"/>
<column name="compartmentCode"/>
<column name="position"/>
<column name="ExternalProductId">316495</column>
</record>
</queryResults>
</query>
</queries>
</output_getquerydata>
</msg>
<TL>
<id>65004</id>
<ArticleNr>89CMID</ArticleNr>
<Gross>2700</Gross>
</TL>
<TL>
<id>65005</id>
<ArticleNr>89CMID</ArticleNr>
<Gross>1700</Gross>
<BOLNumber>18117</BOLNumber>
</TL>
<TL>
<id>65006</id>
<ArticleNr>89CMID</ArticleNr>
<Gross>2100</Gross>
<BOLNumber>18117</BOLNumber>
</TL>
<TL>
<id>65007</id>
<ArticleNr>91VPRM</ArticleNr>
<Gross>500</Gross>
<BOLNumber>18117</BOLNumber>
</TL>
</TL>
</root>
XSL:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output encoding="ISO-8859-1" indent="yes" method="xml"/>
<xsl:key name="Article" match="TL" use="./ArticleNr"/>
<xsl:key name="prod" match="query[@name='q2']/queryResults/record" use="../../parameters/parameter[@name='prodCode']"/>
<xsl:template match="root">
<msglist>
<xsl:for-each select="./TL[./msg/output_getquerydata/queries/query/queryResults/record/column[@name='actionState'] !='finished'] ">
<xsl:variable name="distinctArticle" select="//TL[string(ArticleNr)][count(. | key('Article',ArticleNr)[1]) = 1]"/>
<msg>
<CMT>
<m>
<a>
<AP>
<xsl:variable name="position">
<!--variable to concatenate all products -->
<xsl:for-each select="$distinctArticle">
<xsl:variable name="pos" select="concat(key('prod', .//ArticleNr)/column[@name='ExternalProductId'][. != ''],';')"/>
<xsl:value-of select="$pos"/>
</xsl:for-each>
</xsl:variable>
<xsl:for-each select="TL[generate-id() =
generate-id(key('Article', .//ArticleNr)[1])]">
<grossVolume>
<xsl:value-of select="sum(concat(.,';'))"/>
</grossVolume>
</xsl:for-each>
<product>
<xsl:choose>
<xsl:when test="substring($position, string-length($position))=';'">
<xsl:value-of select="substring($position, 1, string-length($position)-1)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$position"/>
</xsl:otherwise>
</xsl:choose>
</product>
</AP>
</a>
</m>
</CMT>
</msg>
</xsl:for-each>
</msglist>
</xsl:template>
</xsl:stylesheet>
The output is almost good. The thing is I need in the grossVolume node to have the sum of all "Gross" nodes for the same ArticleNr. SO, my output would be:
<grossVolume>6500;500</grossVolume>
<product>316442;316495</product>
Because for ArticleNr - 89CMID -, we have the sum values in gross 2700+1700+2100=6500.
Basically, for all "ArticleNr" nodes that have the same values, I need to sum all the numbers from their respective "Gross" nodes, and then produce an output where we concatenate these sums. For the Product node output, I concatenate the required values, and it works, I need to have that similar information in the grossVolume node also.
Thank you!
Upvotes: 1
Views: 591
Reputation: 3247
You cannot do a sum()
and concat()
in the same instruction as it would throw an error for string and number combination. The sum will have to be stored in a local variable and then used to concatenate with other sums to form the value of <grossVolume>
.
Please change the for-each
loop for TL
<xsl:for-each select="TL[generate-id() = generate-id(key('Article', .//ArticleNr)[1])]">
<grossVolume>
<xsl:value-of select="sum(concat(.,';'))"/>
</grossVolume>
</xsl:for-each>
to the below snippet
<grossVolume>
<xsl:for-each select="TL[generate-id() = generate-id(key('Article', ./ArticleNr)[1])]">
<xsl:variable name="sumGross" select="sum(key('Article', ./ArticleNr)/Gross)" />
<xsl:value-of select="$sumGross" />
<xsl:if test="position() != last()">
<xsl:value-of select="';'" />
</xsl:if>
</xsl:for-each>
</grossVolume>
Output
<grossVolume>6500;500</grossVolume>
Upvotes: 1