2013-06-04 64 views
0

谢谢大家花时间查看我的问题。XSLT函数sum()不计算节点

当节点值不同时总结节点时遇到问题,但如果它是相同的,则工作。

这里是我的XML -

<statement> 
    <agency> 
     <employerShareAmt>75.00</employerShareAmt> 
    </agency> 
    <agency> 
     <employerShareAmt>76.00</employerShareAmt>   
    </agency> 
</statement> 

这里是我的XSLT -

<xsl:template match="/"> 
    <root> 
     <xsl:call-template name="shopData"/> 
    </root> 
</xsl:template> 

<xsl:template name="shopData"> 
    <RESULT> 
     <xsl:call-template name="sum"/> 
    </RESULT> 
</xsl:template> 

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

<xsl:template match="statement"> 
    <agency> 
    <xsl:for-each-group select="agency" 
         group-by="employerShareAmt"> 
     <employerShareAmt> 
     <xsl:value-of 
      select="sum(current-group()/employerShareAmt)"/> 
     </employerShareAmt> 
    </xsl:for-each-group> 
    </agency> 
</xsl:template> 

结果是75 76。但是,如果这两个分别为75,其结果将是150

我似乎认为它是按值分组然后添加它们。我怎样才能得到它的节点,所以上述XML结果将是151?

+0

这听起来像你只是想要所有'employerShareAmt'值的总和?在这种情况下,你可以忘记分组。除非有多个代理机构,并且您希望每个代理机构的总数?但是你按照份额的价值分组,并且这是不对的。真实数据中是否有某种代理ID? – Borodin

回答

0

您意识到样式表处理器按值对代理进行分组,然后总计他们的employerShareAmt元素。你是对的。它这样做是因为你要求它这样做:这就是group-by="employerShareAmt"所做的。

你在找什么东西?当前声明元素中的所有雇主共享元素的总和?

<xsl:template match="statement"> 
    <agency> 
    <employerShareAmt> 
     <xsl:value-of select="sum(agency/employerShareAmt)"/> 
    </employerShareAmt> 
    </agency> 
</xsl:template> 

给定代理机构内所有雇主股权总和的总和?对于你显示的输入,身份转换将会很好地完成;否则

<xsl:template match="agency"> 
    <agency> 
    <employerShareAmt> 
     <xsl:value-of select="sum(EmployerShareAmt)"/> 
    <employerShareAmt> 
    </agency> 
</xsl:template> 

所有employerShareAmt元素对于一些机构集团与你没有表现出一些其他财产的同一机构ID或名称或平等的总和?保持类似于您当前的结构,但将其归入代理机构的正确财产,而不归类为employerShareAmt的价值。