GNOME Bugzilla – Bug 709361
xsltproc improperly casting strings to numbers
Last modified: 2017-06-08 12:46:40 UTC
Processing a fairly simple construct, xsltproc is converting a string like "8.45" to "8.449999999999999". Input XML: <?xml version="1.0" encoding="UTF-8"?> <STUFF xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <field_string>8.45</field_string> </STUFF> XSLT file: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output method="xml" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <STUFF> <xsl:for-each select="STUFF/field_string"> <field_decimal> <xsl:value-of select="string(number(string(.)))"/> </field_decimal> </xsl:for-each> </STUFF> </xsl:template> </xsl:stylesheet> output from xsltproc (version 1.1.28): creating dictionary for stylesheet reusing dictionary from test.xslt for stylesheet xsltParseStylesheetProcess : found stylesheet exclude result prefix xs xsltPrecomputeStylesheet: removing ignorable blank node xsltCompilePattern : parsing '/' xsltCompilePattern : parsed /, default priority 0.500000 added pattern : '/' priority 0.500000 parsed 1 templates Resolving attribute sets references Creating sub-dictionary from stylesheet for transformation Registered 0 modules reusing transformation dict for output Registering global variables Registering global variables from test.xslt xsltProcessOneNode: applying template '/' for / xsltApplySequenceConstructor: copy node STUFF xsltForEach: select STUFF/field_string xsltForEach: select evaluates to 1 nodes xsltApplySequenceConstructor: copy node field_decimal xsltValueOf: select string(number(string(.))) xsltCopyTextString: copy text 8.449999999999999 xsltValueOf: result '8.449999999999999' freeing transformation dictionary <?xml version="1.0" encoding="UTF-8"?> <STUFF> <field_decimal>8.449999999999999</field_decimal> </STUFF> freeing dictionary from stylesheet I have confirmed this behavior differs from Microsoft's XSLT processor, Altova Mapforce, and Saxon HE. They all output <field_decimal>8.45</field_decimal>
This can be reproduced by simply running: $ xmllint --xpath 'string(8.45)' dummy.xml 8.449999999999999 Since it's an issue with the XPath engine, I'm changing Product to libxml2.
*** This bug has been marked as a duplicate of bug 629325 ***