Using sum() function for string nodes in XSLT

2019-01-20 19:45发布

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

This is my 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>

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>

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...

1条回答
▲ chillily
2楼-- · 2019-01-20 20:05

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. 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

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

查看更多
登录 后发表回答