Skip to content

Commit

Permalink
Fix the XPath sum function.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Sep 18, 2023
1 parent 8cc76e5 commit 675d773
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/src/xpath/functions/number.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ XPathValue number(XPathContext context, List<XPathExpression> arguments) {
// number sum(node-set)
XPathValue sum(XPathContext context, List<XPathExpression> arguments) {
XPathEvaluationException.checkArgumentCount('sum', arguments, 1);
final nodes = arguments[0](context).nodes;
return XPathNumber(nodes
.map((node) => num.tryParse(node.toXmlString()) ?? 0)
return XPathNumber(arguments[0](context)
.nodes
.map((node) => num.tryParse(XPathNodeSet([node]).string) ?? 0)
.fold(0, (a, b) => a + b));
}

Expand Down
2 changes: 2 additions & 0 deletions test/xpath_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ void main() {
});
test('sum', () {
expectEvaluate(xml, 'sum(//text())', isNumber(6));
final attr = XmlDocument.parse('<r><e a="36"/><e a="6"/></r>');
expectEvaluate(attr, 'sum(/r/e/@a)', isNumber(42));
});
test('floor', () {
expectEvaluate(xml, 'floor(-1.5)', isNumber(-2));
Expand Down

0 comments on commit 675d773

Please sign in to comment.