I have a xml structure like:
<PartsDetail> <Id>1481</Id> <Desc>test1</Desc> <GlobDesc>test2</GlobDesc> <Price Cur="UAH">+798.27</Price> </PartsDetail>
<PartsDetail> <Id>0741</Id> <Desc>test2</Desc> <GlobDesc>test2</GlobDesc> <Price Cur="UAH">+399.14</Price> </PartsDetail>
And in view, I make some transformation with "price" (I bring to view like 399.14).
I use this for transformation:
<xsl:call-template name="showNumberWithThousands">
<xsl:with-param name="value">
<xsl:call-template name="parseNumber">
<xsl:with-param name="value" select="Price"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
Also I need to take a sum of price now. I tried to use this:
<xsl:value-of select="sum(//Data/Paint//PartsDetail/@Price)"/>
But the a result is - NaN.
As I understand I need to transform the price in "normal view (without + and -)" before a send it to function "sum".
To: @michael.hor257k
Structure is more complicated. I use your solution - but it didn't work. It looks like I'm doing something wrong
<xsl:template name="PaintSum">
<xsl:variable name="corrected-prices">
<xsl:for-each select="//CalcData/Paint/PaintDtl">
<price> <xsl:value-of select="translate(MatAmnt, '+', '')"/> </price>
</xsl:for-each>
</xsl:variable>
<sum> <xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/> </sum>
</xsl:template>
And when I use <xsl:call-template name="PaintSum"/>
Nothing happens. Similarly, further request into templates stop working.
I'm trying to use:
<xsl:variable name="corrected-prices">
<xsl:for-each select="//CalcData/Paint//PaintDtl">
<price>
<xsl:value-of select="translate(MatAmnt, '+', '')"/>
</price>
</xsl:for-each>
</xsl:variable>
And add sum in text by :
<xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/>
But output file - crashes.
$corrected-prices contains "1086.65286.75".
How can I turn this into a sum?
This transformation doesn't require the use of any extension functions and doesn't produce an intermediary tree -- in fact it runs with 0 additional memory and is suitable for streaming, if the XSLT processor recognizes tail-recursion:
When it is applied on the provided XML document:
The wanted, correct result is produced:
That is more or less correct (you don't want to remove the minus sign, in case the number is negative). Given a well-formed input such as:
XML
the following stylesheet:
XSLT 1.0
will return: