Hi, I'm new to BaseX. We are running a simple XQuery:
declare variable $documentguid as xs:string external; <utterances> { for $targetelements in /*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid] return <utterance> <guid>{data($targetelements/@xml:id)}</guid> <speaker>{data($targetelements/@who)}</speaker> <text>{data($targetelements/text())}</text> </utterance> } </utterances>
by calling http://dahdsst2:8984/rest/DF?run=interviewutterances.xq&documentguid=q-2...
All works well but our UI developers are not happy with the JSONML as a serialization format. I changed the output method to 'json' and this returns "[BXJS0002] JSON serialization: <json> expected as root node." So then I changed the <utterances> root element to <json> and then I get: [BXJS0002] JSON serialization: Element <json> must be typed as "object" or "array".
I'm a bit lost at this point. I've consulted the documentation but there is not very much detail about the specific steps to go about serialising an XQuery output as JSON.
The standard JSON format I'm trying to output is like so: { "utterances": { "utterance": [ { "guid": "q-2f0de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "interviewer", "text": "I'd like to start, if I may, by asking you your birth date." }, { "guid": "q-300de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "subject", "text": "November 9th, 1902." }, { "guid": "q-310de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "interviewer", "text": "Could you tell me how many children there were in your family?" } ] } }
Any tips on how to go about this in BaseX would be much appreciated, Regards,
___________________________________ DARREN BELL Data and Services Developer ___________________________________ T +44(0) 1226 272144 E dbell@essex.ac.uk W ukdataservice.ac.uk ___________________________________ UK Data Service UK Data Archive University of Essex ___________________________________ Legal Disclaimer: Any views expressed by the sender of this message are not necessarily those of the UK Data Service or the UK Data Archive. This email and any files with it are confidential and intended solely for the use of the individual(s) or entity to whom they are addressed.
Hello Darren,
did you take a look at the documentation at https://docs.basex.org/wiki/JSON#Examples, especially Example 4 might be helpful. The conversation rules are described at https://docs.basex.org/wiki/JSON#JSON_Functions
So basically you will have to define what type a node will be in the resulting JSON. Something like the following should work (I did not test this):
<json objects="json utterances" arrays="uterrance"> <utterances> { for $targetelements in /*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid] return <utterance> <guid>{data($targetelements/@xml:id)}</guid> <speaker>{data($targetelements/@who)}</speaker> <text>{data($targetelements/text())}</text> </utterance> } </utterances> </json>
Cheers, Dirk
On 09/03/2013 06:18 PM, Bell, Darren S wrote:
Hi, I'm new to BaseX. We are running a simple XQuery:
declare variable $documentguid as xs:string external; <utterances> { for $targetelements in /*:TEI/*:text/*:body/*:u[/*:TEI/*:teiHeader/*:fileDesc/*:publicationStmt/*:idno[@type='qudexdocument']=$documentguid] return
<utterance> <guid>{data($targetelements/@xml:id)}</guid> <speaker>{data($targetelements/@who)}</speaker> <text>{data($targetelements/text())}</text> </utterance> } </utterances>
by calling http://dahdsst2:8984/rest/DF?run=interviewutterances.xq&documentguid=q-2...
All works well but our UI developers are not happy with the JSONML as a serialization format. I changed the output method to 'json' and this returns "[BXJS0002] JSON serialization: <json> expected as root node." So then I changed the <utterances> root element to <json> and then I get: [BXJS0002] JSON serialization: Element <json> must be typed as "object" or "array".
I'm a bit lost at this point. I've consulted the documentation but there is not very much detail about the specific steps to go about serialising an XQuery output as JSON.
The standard JSON format I'm trying to output is like so: { "utterances": { "utterance": [ { "guid": "q-2f0de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "interviewer", "text": "I'd like to start, if I may, by asking you your birth date." }, { "guid": "q-300de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "subject", "text": "November 9th, 1902." }, { "guid": "q-310de4c5-7dfb-e211-b501-000bdb5cc6d5", "speaker": "interviewer", "text": "Could you tell me how many children there were in your family?" } ] } }
Any tips on how to go about this in BaseX would be much appreciated, Regards,
DARREN BELL Data and Services Developer ___________________________________ T +44(0) 1226 272144 E dbell@essex.ac.uk W ukdataservice.ac.uk ___________________________________ UK Data Service UK Data Archive University of Essex ___________________________________ Legal Disclaimer: Any views expressed by the sender of this message are not necessarily those of the UK Data Service or the UK Data Archive. This email and any files with it are confidential and intended solely for the use of the individual(s) or entity to whom they are addressed.
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
basex-talk@mailman.uni-konstanz.de