Reputation: 3096
I have created an XQuery against http://www.w3schools.com/xsl/books.xml
xquery version "3.0";
for $x in collection("/db/books")//book
return
<book title="{$x/title}">
{$x//author}
</book>
If I evaluate it in eXistdb's eXide, I get reasonable output in the preview pane.
<book title="Everyday Italian">
<author>Giada De Laurentiis</author>
etc.
If I try to "run" it, I get the following error in the web browser:
This page contains the following errors:
error on line 4 at column 1: Extra content at the end of the document
Below is a rendering of the page up to the first error.
Giada De Laurentiis
I thought maybe I should serialize it as JSON. Based on a quick reading of http://exist-db.org/exist/apps/wiki/blogs/eXist/JSONSerializer, I added the following two lines after the xquery version line:
declare namespace json="http://www.json.org";
declare option exist:serialize "method=json media-type=text/javascript";
But I get the same acceptable xml preview result and same browser error.
How can I get my output in a web browser, either as XML or JSON?
I looked at https://stackoverflow.com/questions/35038779/json-serialization-with-exist-db-rest-api but didn't see how to use that as a starting point.
Upvotes: 0
Views: 1128
Reputation: 5294
I'm glad you figured out that the original issue was that the browser expects well-formed XML, whereas eXide is happy to show you arbitrary nodes.
On the topic of JSON serialization, briefly (I'm on my phone), see http://exist-db.org/exist/apps/wiki/blogs/eXist/XQuery31 in the section entitled "Serialization". Make sure you're running eXist 3.0 RC1.
Upvotes: 1
Reputation: 3096
A top level tag and some additional curly braces are required:
xquery version "3.0";
declare namespace json="http://www.json.org";
declare option exist:serialize "method=json media-type=text/javascript";
<result>
{for $x in collection("/db/books")//book
return
<book title="{$x/title}">
{$x//author}
</book>
}
</result>
Or, for well-formed XML serialization:
xquery version "3.0";
<result>
{for $x in collection("/db/books")//book
return
<book title="{$x/title}">
{$x//author}
</book>
}
</result>
Credit: http://edutechwiki.unige.ch/en/XQuery_tutorial_-_basics
Upvotes: 0