By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

XPath, sum and number conversion

P: n/a
PAF
Hi,

I'm trying to evaluate a sum expression trougth .NET 2
System.Xml.XPath.XPathNavigator object.

My code is :

Dim document As New System.Xml.XmlDocument()
document.LoadXml("<?xml
version=""1.0""?><root><a>28.7</a><a>9.18</a><a>4.32</a></root>")
Dim navigator As System.Xml.XPath.XPathNavigator =
document.CreateNavigator()
navigator.Evaluate("sum(//a)")

My XML source is :

<?xml version="1.0"?>
<root>
<a>28.7</a>
<a>9.18</a>
<a>4.32</a>
</root>

When I evaluate this expression, the result is 42.19999999996, and
should be 42.2 ... So I read some news and the W3C recommendation about
conversion to numeric values in
http://www.w3.org/TR/xpath#section-Number-Functions .

There is a function called number() that effectively convert XML string
values to numeric values. But with .NET, it raise a
System.Xml.XPath.XPathException ...

Does MS-XML implements this number function ?? Any idee ?

Thanks a lot.

Best regards, Pascal

Nov 29 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
PAF wrote:
I'm trying to evaluate a sum expression trougth .NET 2
System.Xml.XPath.XPathNavigator object.

My code is :

Dim document As New System.Xml.XmlDocument()
document.LoadXml("<?xml
version=""1.0""?><root><a>28.7</a><a>9.18</a><a>4.32</a></root>")
Dim navigator As System.Xml.XPath.XPathNavigator =
document.CreateNavigator()
navigator.Evaluate("sum(//a)")

My XML source is :

<?xml version="1.0"?>
<root>
<a>28.7</a>
<a>9.18</a>
<a>4.32</a>
</root>

When I evaluate this expression, the result is 42.19999999996, and
should be 42.2 ...
The result is double number 42.2. 42.19999999996 is memory
representation of floating point number 42.2.
If you are interested take a look at "What Every Computer Scientist
Should Know About Floating-Point Arithmetic" at
http://docs.sun.com/source/806-3568/ncg_goldberg.html.
You probably looking into result in debugger? Try to output it instead.
So I read some news and the W3C recommendation about
conversion to numeric values in
http://www.w3.org/TR/xpath#section-Number-Functions .

There is a function called number() that effectively convert XML string
values to numeric values. But with .NET, it raise a
System.Xml.XPath.XPathException ...
1. You don't need number() function - sum() function returns number.
2. No exception,
sum = navigator.Evaluate("number(sum(//a))")
works for me, but there is no need to use number() function here.
--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Nov 29 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.