XSL - 获取价格总和 [英] XSL - Get SUM of price

查看:29
本文介绍了XSL - 获取价格总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml 结构,如:

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> 

鉴于,我对价格"进行了一些转换(我认为像 399.14).

And in view, I make some transformation with "price" (I bring to view like 399.14).

我用它来进行转换:

<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)"/>

但结果是 - NaN.

But the a result is - NaN.

据我所知,在将价格发送到函数sum"之前,我需要在普通视图(没有 + 和 -)"中转换价格.

As I understand I need to transform the price in "normal view (without + and -)" before a send it to function "sum".

致:@michael.hor257k

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> 

当我使用 <xsl:call-template name="PaintSum"/>没发生什么事.同样,对模板的进一步请求停止工作.

And when I use <xsl:call-template name="PaintSum"/> Nothing happens. Similarly, further request into templates stop working.

我正在尝试使用:

<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 包含1086.65286.75".

$corrected-prices contains "1086.65286.75".

我怎样才能把它变成一个总和?

推荐答案

据我所知,我需要将价格转换为正常视图(没有 + 和-)"之前将其发送到函数sum".

As I understand I need transform price to "normal view (without + and -)" before a send it to function "sum".

这或多或少是正确的(您不想删除减号,以防数字为负).给定一个格式良好的输入,例如:

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

<root>
  <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>
</root>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <xsl:variable name="corrected-prices">
        <xsl:for-each select="PartsDetail">
            <price>
                <xsl:value-of select="translate(Price, '+', '')"/>
            </price>
        </xsl:for-each>
    </xsl:variable>
    <sum>
        <xsl:value-of select="sum(exsl:node-set($corrected-prices)/price)"/>
    </sum>
</xsl:template>

</xsl:stylesheet>

将返回:

<?xml version="1.0" encoding="UTF-8"?>
<sum>1197.41</sum>

这篇关于XSL - 获取价格总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆