jsonxmlxsltxslt-3.0xml-to-json

Why is xml-to-json function returning exponential notation?


All of my XSLT is working when using the xml-to-json() function except for this part,

<number key="send_at">
    <xsl:text>1615842000</xsl:text>
</number>

which returns

"send_at":1.615842E9

when I intend to get this result:

"send_at":1615842000

Solution

  • XPath 3.1 (current)

    Converting numbers to exponential notation is correct according to the spec (XPath and XQuery Functions and Operators 3.1):

    17.5.4 fn:xml-to-json

    Rules

    Nodes in the input tree are handled by applying the following rules, recursively.

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

    The fn:xml-to-json function does allow an $options argument, which provides a means for implementation-dependent extensions. Saxon 10.5 already leverages this mechanism to support passing through the value, without regard to JSON validity: 1

    map{'number-formatter':function($x){$x}}
    

    XPath 4 (upcoming)

    A formatter is an officially supported option to fn:xml-to-json planned for XPath 4: 2

    number-formatter: Determines how numeric values should be formatted.

    • Type: (function(xs:string) as xs:string)?
    • Default: ()

    1 Thanks to Michael Kay for noting this mechanism (and foreseeing its need (and already implementing it in Saxon 10.5)).
    2 Thanks to Martin Honnen for noting this XPath 4 update.