在for-each中添加到XSL变量

时间:2022-09-10 14:28:09

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:

我希望每次迭代for-each时都会将变量的值附加到自身,问题是我不确定如何解决这个问题。这是我目前拥有的:

<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

这只输出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?

但这输出12345.原因是它完成了1x1,2x1,3x1,4x1,5x1,因为我的5行每个都有1个数量。所以现在需要发生的是将这些值加起来,这可能吗?

1 个解决方案

#1


4  

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

你在使用XSLT 2.0吗?如果是这样,试试这个:

    <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:

@Markus建议更短的版本:

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

Thanks :)

谢谢 :)

#1


4  

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

你在使用XSLT 2.0吗?如果是这样,试试这个:

    <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:

@Markus建议更短的版本:

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

Thanks :)

谢谢 :)