数字正在连接而不是求和

时间:2013-02-24 04:53:30

标签: sum xslt-1.0

我有以下输入:

<nodes>
    <node>  
        <type>A</type>  
        <val>1000</val>  
    </node>  
    <node>  
        <type>B</type>  
        <val>2000</val>  
    </node>  
    <node>  
        <type>A</type>  
        <val>3000</val>  
    </node>  
</nodes>  

我的目标是获取一系列独特类型并汇总其所有值。我得到以下输出:

<nodes>  
    <node>  
        <type>A</type>  
        <sum>10003000</sum>  
    </node>  
    <node>  
        <type>B</type>  
        <sum>2000</sum>  
    </node>  
</nodes> 

我期待一个总和(对于A型)为4000,但我得到了10003000。

这是我的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:for-each select="/nodes/node[type=$t]">
                            <xsl:value-of select="sum(number(Value))"/>
                        </xsl:for-each>
                    </sum>
                </node>
            </xsl:for-each>
        </nodes>
    </xsl:template>
</xsl:stylesheet>

如何获得我正在寻找的结果?此外,要求和的一些值包含逗号作为分隔符(例如,1,000)。据我所知,sum()不处理逗号。我该如何解决这些问题?

1 个答案:

答案 0 :(得分:0)

只需替换

<sum>
  <xsl:for-each select="/nodes/node[type=$t]">
    <xsl:value-of select="sum(number(Value))"/>
  </xsl:for-each>
</sum>

<强>与

<sum><xsl:value-of select="sum(/nodes/node[type=$t]/val)"/></sum>

这是一个完整的转型:

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

 <xsl:template match="type[generate-id()=generate-id(key('kType',.)[1])]">
  <type><xsl:value-of select="."/></type>
  <sum><xsl:value-of select="sum(key('kType',.)/../val)"/></sum>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

在提供的XML文档上应用此转换时:

<nodes>
    <node>
        <type>A</type>
        <val>1000</val>
    </node>
    <node>
        <type>B</type>
        <val>2000</val>
    </node>
    <node>
        <type>A</type>
        <val>3000</val>
    </node>
</nodes>

产生了想要的正确结果:

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

<强>更新

OP增加了新要求:数字可以包含逗号。

下面的解决方案执行两遍转换,其中第一个通道删除任何val/text()节点中的任何非数字字符

第二遍对第一遍的结果进行操作,基本上是初始问题的解决方案

<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>2000</val>
    </node>
    <node>
        <type>A</type>
        <val>3000</val>
    </node>
</nodes>

产生了想要的正确结果:

<type>A</type>
<sum>4000</sum>
<type>B</type>
<sum>2000</sum>
相关问题