CallumVass
CallumVass

Reputation: 11448

Adding to an XSL Variable in a for-each

I wish to append the value of a variable to itself each time it iterates through a for-each, problem is I'm not sure on how to go about this. Here is what I currently have:

<xsl:variable name="lineQty" select="0" />
<xsl:for-each select="/*/*/*/*/*/xsales:CustInvoiceTrans">
  <xsl:value-of select="$lineQty + (xsales:LineNum * xsales:Qty)" />
</xsl:for-each>
<test>
  <xsl:value-of select="$lineQty" />
</test>

This just outputs 0

Edit

I changed it to the following:

<xsl:variable name="lineQty">
  <xsl:for-each select="/*/*/*/*/*/xsales:CustInvoiceTrans">
    <xsl:value-of select="xsales:LineNum * xsales:Qty" />
  </xsl:for-each>
</xsl:variable>

But this outputs 12345. The reason for this is that it's done 1x1, 2x1, 3x1, 4x1, 5x1 as my 5 lines each have a quantity of 1. So all that needs to happen now is adding up these values, is that possible?

Upvotes: 1

Views: 3370

Answers (1)

Lukasz
Lukasz

Reputation: 7662

Are you using XSLT 2.0? If so, try this:

    <xsl:variable name="seq" as="xs:double*">
        <xsl:for-each select="/*/*/*/*/*/xsales:CustInvoiceTrans">
            <xsl:sequence select="xs:double(xsales:LineNum) *  xs:double(xsales:Qty)"/>
        </xsl:for-each>
    </xsl:variable>
    <test>
        <xsl:value-of select="sum($seq)"/>
    </test>

EDIT

@Markus suggested even shorter version:

    <test>
        <xsl:value-of select="sum(/*/*/*/*/*/xsales:CustInvoiceTrans/(xs:double(xsales:LineNum) * xs:double(xsales:Qty)))" />
    </test>

Thanks :)

Upvotes: 5

Related Questions