unexpected behaviour
Thursday, 27 November 2014 · 4 messages
RS
Rob Stapper27 Nov 2014 11:37 #
Hi,
I'm playing with the new XQuery 3.1 functions amongst which the JSON-serilization.
I would expect all three snippets below to result in the same output but all three give a different result.
Snippet 1:
=======
declare option output:method 'json';serialize( <a>test</a>
)
Snippet 2:
=======
serialize( <a>test</a>
, map{ 'method' : 'json'}
)
Snippet 3:
=======
declare option output:method 'json';serialize( <a>test</a>
, map{ 'method' : 'json'}
)
If I do the same with XML-serilization than all three results are the same.
Are my expectations correct in this?
- Rob
---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
CG
Christian Grün27 Nov 2014 11:52 #
Hi Rob
the behavior is correct:
Quoted text · Rob Stapper · 2 lines
> declare option output:method 'json'; > serialize( <a>test</a> )
The result is:
"<a>test</a>"
…as specified in [1].
Quoted text · Rob Stapper · 3 lines
> serialize( <a>test</a> > , map{ 'method' : 'json'} > )
The result is:
"<a>test</a>"
The node will first be serialized to a string, and the query result (i.e., this string) will be serialized according to the XML
serialization rules. You can switch to plain text serialization as follows:
declare option output:method 'text';
serialize( <a>test</a>,
map{ 'method' : 'json'} )Quoted text · Rob Stapper · 4 lines
> declare option output:method 'json'; > serialize( <a>test</a> > , map{ 'method' : 'json'} > )
The result is:
"\"<a>test</a>\""
The node will first be serialized to a string, and the query result (i.e., this string) will be serialized according to the JSON serialization rules. In a JSON string, quotes will be backslashed.
Cheers,
Christian
[1] http://www.w3.org/TR/xslt-xquery-serialization-31/#JSON_JSON-NODE-OUTPUT-METHOD
RS
Rob Stapper27 Nov 2014 12:13 #
Hi Christian,
Thanx for the quick response. I get it.
In my case I want the result of the first snippet but then in a xquery-module. I can't use the: "declare option ....." in a module. Can you give me a suggestion how to realize this?
Thanx in advance,
Rob
-----Oorspronkelijk bericht-----
Quoted text · Christian Grün · 35 lines
Van: Christian Grün [mailto:christian.gruen at gmail.com] Verzonden: donderdag 27 november 2014 11:53 Aan: Rob Stapper CC: BaseX Onderwerp: Re: [basex-talk] unexpected behaviour Hi Rob the behavior is correct: > declare option output:method 'json'; > serialize( <a>test</a> ) The result is: "<a>test</a>" …as specified in [1]. > serialize( <a>test</a> > , map{ 'method' : 'json'} > ) The result is: "<a>test</a>" The node will first be serialized to a string, and the query result (i.e., this string) will be serialized according to the XML serialization rules. You can switch to plain text serialization as follows: declare option output:method 'text'; serialize( <a>test</a>, map{ 'method' : 'json'} ) > declare option output:method 'json'; > serialize( <a>test</a> > , map{ 'method' : 'json'} > ) The result is: "\"<a>test</a>\"" The node will first be serialized to a string, and the query result (i.e., this string) will be serialized according to the JSON serialization rules. In a JSON string, quotes will be backslashed. Cheers, Christian [1] http://www.w3.org/TR/xslt-xquery-serialization-31/#JSON_JSON-NODE-OUTPUT-METHOD --- Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware. http://www.avast.com
CG
Christian Grün27 Nov 2014 12:17 #
Quoted text · Rob Stapper · 1 line
> Can you give me a suggestion how to realize this?
It depends on how you retrieve your results. Do you use RESTXQ, BaseX as standalone, or any other API?
Christian
Written for BaseX 7.9. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery