Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xpath - Using sum() function for string nodes in XSLT

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...