如何使用 sum() 用逗号对值求和 [英] how to sum values with commas using sum()

查看:50
本文介绍了如何使用 sum() 用逗号对值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 文档:

I have the following XML document:

<nodes>
    <node>  
        <type>A</type>  
        <val>1,000</val>  
    </node>  
    <node>  
        <type>B</type>  
        <val>2,000</val>  
    </node>  
    <node>  
        <type>A</type>  
        <val>3,000</val>  
    </node>  
</nodes>  

我的目标是获取唯一类型的列表并将它们的所有 相加.我得到以下输出:

My goal is to get a list of unique types and sum all their <val>s. I'm getting the following output:

<nodes>  
    <node>  
        <type>A</type>  
        <sum>3</sum>  
    </node>  
    <node>  
        <type>B</type>  
        <sum>2</sum>  
    </node>  
</nodes> 

我期望总和(对于 A 型)为 4000,但我得到了 3.

I was expecting a sum (for type A) of 4000, but I'm getting 3 instead.

这是我的 xslt:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>  
    <xsl:key name="type" match="/nodes/node/type/text()" use="." />
    <xsl:template match="/">
        <nodes>
            <xsl:for-each select="/nodes/node/type/text()[generate-id()=generate-id(key('type',.)[1])]">
                <node>
                    <xsl:variable name="t" select="."/>
                    <type><xsl:value-of select="$t"/></type>
                    <sum>
                       <xsl:value-of select="sum(/nodes/node[type=$t]/val)"/>
                    </sum>
                </node>
            </xsl:for-each>
        </nodes>
    </xsl:template>
</xsl:stylesheet>  

关于如何使用 sum() 对包含逗号的值求和的任何想法?

Any ideas on how I can sum values with commas in them using sum()?

推荐答案

这种转变:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kType" match="type" use="."/>

 <xsl:variable name="vrtfPass1">
  <xsl:apply-templates select="/*"/>
 </xsl:variable>

 <xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>

 <xsl:template match="node()|@*">
     <xsl:copy>
       <xsl:apply-templates select="node()|@*"/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="/">
  <xsl:apply-templates mode="pass2" select=
   "$vPass1/*/*/type[generate-id()=generate-id(key('kType',.)[1])]"/>
 </xsl:template>

 <xsl:template match="val/text()[true()]">
  <xsl:value-of select="translate(., translate(.,'01234567890', ''), '')"/>
 </xsl:template>

 <xsl:template match="type" mode="pass2">
  <type><xsl:value-of select="."/></type>
  <sum><xsl:value-of select="sum(key('kType',.)/../val)"/></sum>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<nodes>
    <node>
        <type>A</type>
        <val>1,000</val>
    </node>
    <node>
        <type>B</type>
        <val>2,000</val>
    </node>
    <node>
        <type>A</type>
        <val>3,000</val>
    </node>
</nodes>

产生想要的、正确的结果:

<type>A</type>
<sum>4000</sum>
<type>B</type>
<sum>2000</sum>

这篇关于如何使用 sum() 用逗号对值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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