对 XSLT 中的字符串节点使用 sum() 函数 [英] Using sum() function for string nodes in XSLT

查看:28
本文介绍了对 XSLT 中的字符串节点使用 sum() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xslt 1.0 中使用 sum() 函数时遇到问题.简而言之:我想对 L_AMOUNT 节点中的值求和.

I have a problem using sum() function in xslt 1.0. In short: I want to sum values in L_AMOUNT nodes.

这是我的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="XX_EXEC_PRINT_DOCUMENT.xsl"?>
<ROWSET>
 <ROW>
  <LINES>
   <LINES_ROW>
    <L_AMOUNT>330,00</L_AMOUNT>
   </LINES_ROW>
   <LINES_ROW>
    <L_AMOUNT>995 650,00</L_AMOUNT>
   </LINES_ROW>
  </LINES>
 </ROW>
</ROWSET>

以及 xsl 文件的重要部分:

And significant part of xsl file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns="http://www.w3.org/1999/xhtml">
  <xsl:output method="html"/>
  <xsl:template match="*">
    <html>
      <body class="OraBody">

          <xsl:value-of select="sum(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.'))"/>

      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

正如你所看到的,我试图截断中间的所有空格并用点替换逗号.但是,firefox 解析器说 XPath 表达式应该返回 NodeSet.转换函数返回的数字是值而不是节点,因此错误很明显.但是我该怎么做呢?我没有想法...

As You can see i tried to trunctate all white spaces in between and replaced comma with dot. But then, firefox parser says that XPath expression should return NodeSet. Numbers returned by translate functions are values not nodes, so the error is obvious. But how can I do it then? I'm out of ideas...

推荐答案

在 XSLT 1.0 中,sum() 的参数必须是节点集,translate 的结果不是节点-set(它是一个字符串).XSLT 1.0 中有多种计算值求和的方法,但都不是很令人满意:

In XSLT 1.0, the argument of sum() must be a node-set, and the result of translate is not a node-set (it is a string). There are various ways of summing computed values in XSLT 1.0, none of them very satisfactory:

  1. 编写一个递归命名模板,为每个值调用一次并添加下一个值
  2. 使用exslt:node-set()扩展函数构造一个节点集,并将sum()应用于结果
  3. 使用 Dimitre Novatchev 的 FXSL 库
  1. write a recursive named template that is called once for each value and adds the next value
  2. construct a node-set using the exslt:node-set() extension function and apply sum() to the result
  3. use Dimitre Novatchev's FXSL library

如果可以,请移至 XSLT 2.0,这样这些问题就变得微不足道了.

If you can, move to XSLT 2.0 where such problems become trivial.

这篇关于对 XSLT 中的字符串节点使用 sum() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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