Questions on conversion of Javascript and XDM values
Added by Martin Honnen almost 8 years ago
Reading through http://www.saxonica.com/saxon-js/documentation/index.html#!ixsl-extension/conversions I find that a Javascript @Date@ is converted to a XDM @xs:dateTime@ while the other way round a @xs:dateTime@ is converted to a Javascript @string@. Why that asymmetry, why is a @xs:dateTime@ not converted to a Javascript @Date@?
Furthermore the explanation for Javascript object to XDM conversion says:
Wrapped external object, which behaves like an XPath map. The properties of the object become key-value pairs within the map, where the key is the property name (as a string), and the value is obtained by applying these conversion rules recursively.
where I then would expect that a Javascript object with a @Date@ property @{ date : new Date() }@ is converted to an XDM map with an @xs:dateTime@ entry. However when I then pass that map back to Javascript I would expect the @xs:dateTime@ to be converted to a Javascript string.
Trying some samples in the Chrome browser's developer tools console I indeed get an XDM @xs:dateTime@ converted to a Javascript string:
var xdmDateTime = SaxonJS.XPath.evaluate('current-dateTime()'); typeof xdmDateTime
>"string"
However, when I pass in a Javascript object with a @Date@ property and pass it back to Javascript I don't get a @string@, I get a @Date@ back:
var objWithDateProp = SaxonJS.XPath.evaluate('$obj', document, { params : { 'obj' : { 'date' : new Date() }}}); typeof objWithDateProp.date
>"object"
objWithDateProp.date instanceof Date
>true
So there the Javascript @Date@ survives the round-trip Javascript->XDM->Javascript, but I wonder whether that is doing what the documentation says, where I would expect a string coming back from the XDM site.
In a simple @Date@ round-trip
var d = SaxonJS.XPath.evaluate('$date', document, { params : { 'date' : new Date() }}); typeof d
>"string"
I get a string so there seems to be an inconsistency.
Replies (1)
RE: Questions on conversion of Javascript and XDM values - Added by John Lumley almost 8 years ago
Thank you for this observation - we'll certainly look into this (after XML Prague!)
Nice to get such rapid exploration!
Please register to reply