Using sum() function for string nodes in XSLT












0















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










share|improve this question



























    0















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










    share|improve this question

























      0












      0








      0


      0






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










      share|improve this question














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







      xslt xpath xslt-1.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 '14 at 2:10









      tytantytan

      1051210




      1051210
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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.






          share|improve this answer


























          • Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

            – tytan
            Jan 4 '14 at 13:29













          • Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

            – Michael Kay
            Jan 4 '14 at 17:53






          • 1





            Example of Michael's solution: xsltransform.net/948Fn5c

            – Joep
            Jan 4 '14 at 18:28











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20915982%2fusing-sum-function-for-string-nodes-in-xslt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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.






          share|improve this answer


























          • Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

            – tytan
            Jan 4 '14 at 13:29













          • Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

            – Michael Kay
            Jan 4 '14 at 17:53






          • 1





            Example of Michael's solution: xsltransform.net/948Fn5c

            – Joep
            Jan 4 '14 at 18:28
















          1














          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.






          share|improve this answer


























          • Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

            – tytan
            Jan 4 '14 at 13:29













          • Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

            – Michael Kay
            Jan 4 '14 at 17:53






          • 1





            Example of Michael's solution: xsltransform.net/948Fn5c

            – Joep
            Jan 4 '14 at 18:28














          1












          1








          1







          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 4 '14 at 8:58









          Jens Erat

          29.1k156179




          29.1k156179










          answered Jan 4 '14 at 8:40









          Michael KayMichael Kay

          111k663119




          111k663119













          • Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

            – tytan
            Jan 4 '14 at 13:29













          • Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

            – Michael Kay
            Jan 4 '14 at 17:53






          • 1





            Example of Michael's solution: xsltransform.net/948Fn5c

            – Joep
            Jan 4 '14 at 18:28



















          • Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

            – tytan
            Jan 4 '14 at 13:29













          • Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

            – Michael Kay
            Jan 4 '14 at 17:53






          • 1





            Example of Michael's solution: xsltransform.net/948Fn5c

            – Joep
            Jan 4 '14 at 18:28

















          Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

          – tytan
          Jan 4 '14 at 13:29







          Could You give me a hint? I tried 2nd way: <xsl:value-of select="sum(exsl:node-set(translate(translate(ROW/LINES/*/L_AMOUNT,' ',''),',','.')))"/> But it returns just first node. Maybe recursive template would be easier? Just hint!

          – tytan
          Jan 4 '14 at 13:29















          Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

          – Michael Kay
          Jan 4 '14 at 17:53





          Well, I gave you a hint but it clearly wasn't enough. To use the exslt:node-set approach you need to do a transformation of the source tree to a new tree (in an xsl:variable) in which the numbers appear in their converted form as element values. Something like <xsl:variable name="x"><xsl:for-each select="//L_AMOUNT"><n><xsl:value-of select="translate...."/></n></xsl:for-each></xsl:variable> and then sum(exslt:node-set($x)/n).

          – Michael Kay
          Jan 4 '14 at 17:53




          1




          1





          Example of Michael's solution: xsltransform.net/948Fn5c

          – Joep
          Jan 4 '14 at 18:28





          Example of Michael's solution: xsltransform.net/948Fn5c

          – Joep
          Jan 4 '14 at 18:28




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20915982%2fusing-sum-function-for-string-nodes-in-xslt%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine