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...
xslt xpath xslt-1.0
add a comment |
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
add a comment |
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
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
xslt xpath xslt-1.0
asked Jan 4 '14 at 2:10
tytantytan
1051210
1051210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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:
- write a recursive named template that is called once for each value and adds the next value
- construct a node-set using the
exslt:node-set()
extension function and applysum()
to the result - use Dimitre Novatchev's FXSL library
If you can, move to XSLT 2.0 where such problems become trivial.
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 thensum(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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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:
- write a recursive named template that is called once for each value and adds the next value
- construct a node-set using the
exslt:node-set()
extension function and applysum()
to the result - use Dimitre Novatchev's FXSL library
If you can, move to XSLT 2.0 where such problems become trivial.
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 thensum(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
add a comment |
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:
- write a recursive named template that is called once for each value and adds the next value
- construct a node-set using the
exslt:node-set()
extension function and applysum()
to the result - use Dimitre Novatchev's FXSL library
If you can, move to XSLT 2.0 where such problems become trivial.
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 thensum(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
add a comment |
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:
- write a recursive named template that is called once for each value and adds the next value
- construct a node-set using the
exslt:node-set()
extension function and applysum()
to the result - use Dimitre Novatchev's FXSL library
If you can, move to XSLT 2.0 where such problems become trivial.
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:
- write a recursive named template that is called once for each value and adds the next value
- construct a node-set using the
exslt:node-set()
extension function and applysum()
to the result - use Dimitre Novatchev's FXSL library
If you can, move to XSLT 2.0 where such problems become trivial.
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 thensum(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
add a comment |
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 thensum(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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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