3

I am transforming a xml into json using xml-to-json() function of xslt 3.0 using Saxon 9.8 HE. The problem I am getting is that my Number value is getting converted into exponent (scientific notation). I want output same what i pass in input xml.

xsltfiddle link https://xsltfiddle.liberty-development.net/94hvTyT

input xml

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
   <map key="Request">
     <number key="price">1234567</number>
   </map>
</map>

Note that this xml is also genrated using json-to-xml() funtion of xslt 3.0

XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:value-of select="xml-to-json(., map { 'indent' : true() })"/>
  </xsl:template>

</xsl:stylesheet>

OUTPUT

{ "Request" : 
    { "price" : 1.234567E6 } 
}

Desired Output

{ "Request" : 
    { "price" : 1234567 } 
}

Any solution/suggestions on it would be great help for me.

VSS
  • 196
  • 1
  • 8

1 Answers1

3

The result is as specified in https://www.w3.org/TR/xpath-functions/#func-xml-to-json which says

An element $E named number results in the output of the string result of xs:string(xs:double(fn:string($E)))

You would get the same result with other XPath/XQuery 3.1 implementations or XSLT 3.0 implementations.

So unless you are willing to change the <number key="price">1234567</number> to <string key="price">1234567</string> (could of course be done with an intermediary transformation step) I don't think you can prevent the exponential number format for large numbers with xml-to-json.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Thanks Martin for your reply. You are right, exponential number format can't be avoid as per specification. I replace the number with string with an intermediary step and got the same string result. – VSS Apr 23 '18 at 14:35