Project

Profile

Help

Number formatting in XSLT 3.0 xml-to-json function

Added by Jeff Tennessen over 8 years ago

We are using the .NET API for Saxon 9.7.0.7 EE. When we use the following XSLT 3.0 template:



  
  
    
  

to convert this XML document:



  999999
  1000000

we get this result:


{
  "num1": 999999,
  "num2": 1000000.0
}

Note the added decimal place on num2. It appears that numbers one million or higher have the ".0" added. Is this the expected behavior? Is there any way to modify it or is this perhaps part of the standard?


Replies (3)

Please register to reply

RE: Number formatting in XSLT 3.0 xml-to-json function - Added by Martin Honnen over 8 years ago

When trying Saxon 9.7.0.7 EE Java or .NET from the command line then I get a different result:


{"num1":999999,"num2":1.0E6}

As for the spec, that function seems to be a moving target, the current version https://www.w3.org/XML/Group/qtspecs/specifications/xslt-30/html/Overview-diff.html#func-xml-to-json says for @number@ elements

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

which would explain the result I get I think.

Can you show how you use the API?

RE: Number formatting in XSLT 3.0 xml-to-json function - Added by Michael Kay over 8 years ago

Yes, I was going to reply that I would have expected "1.0E0" but I hadn't yet got around to writing a test to confirm this.

The WG did look at this fairly recently and confirmed that it wanted to use the formatting produced by casting xs:double to xs:string. The format in the JSON file really shouldn't matter assuming you are reading it with a conformant JSON parser.

RE: Number formatting in XSLT 3.0 xml-to-json function - Added by Jeff Tennessen over 8 years ago

Ach, my apologies. You guys are, of course, correct. It was a tidying routine that I'd forgotten I even added that was converting @1.0E6@ to @1000000.0@. You explained the crux of my question by letting me know that this behavior is per the spec. Many thanks!

    (1-3/3)

    Please register to reply