From 675d7732cd2051cae47ae543d77693675dbb9a9c Mon Sep 17 00:00:00 2001 From: Lukas Renggli Date: Mon, 18 Sep 2023 22:42:50 +0300 Subject: [PATCH] Fix the XPath `sum` function. --- lib/src/xpath/functions/number.dart | 6 +++--- test/xpath_test.dart | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/src/xpath/functions/number.dart b/lib/src/xpath/functions/number.dart index 2fe081f..b561b98 100644 --- a/lib/src/xpath/functions/number.dart +++ b/lib/src/xpath/functions/number.dart @@ -13,9 +13,9 @@ XPathValue number(XPathContext context, List arguments) { // number sum(node-set) XPathValue sum(XPathContext context, List 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)); } diff --git a/test/xpath_test.dart b/test/xpath_test.dart index d389f90..9045f38 100644 --- a/test/xpath_test.dart +++ b/test/xpath_test.dart @@ -461,6 +461,8 @@ void main() { }); test('sum', () { expectEvaluate(xml, 'sum(//text())', isNumber(6)); + final attr = XmlDocument.parse(''); + expectEvaluate(attr, 'sum(/r/e/@a)', isNumber(42)); }); test('floor', () { expectEvaluate(xml, 'floor(-1.5)', isNumber(-2));