0

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.

Community
  • 1
  • 1
Mark Miller
  • 3,011
  • 1
  • 14
  • 34

2 Answers2

1

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.

Joe Wicentowski
  • 5,159
  • 16
  • 26
  • Thanks. Do you know if there is any straightforward support for JSON in 2.2? Or do you know anything about the two venison co-existing? I have a collection around 20 GB and would prefer not to re-index. – Mark Miller Mar 17 '16 at 23:09
  • The article you first cited has the directions: use `declare option exist:serialize "method=json media-type=text/javascript";` in your prolog. With this, if you save your query to the database and call it from a browser, you should see JSON. In eXide, you may need to change from "XML Output" to "Direct Output" to see the JSON serialization applied. Alternatively, you can use `util:serialize()` and generate the JSON as a string directly, even without a serialization declaration: `util:serialize(1, "method=json media-type=text/javascript")` – Joe Wicentowski Mar 19 '16 at 00:44
  • To clarify, eXist 2.2 supports the `exist:serialize` declaration and the `util:serialize()` function - both eXist-specific code, as before XQuery 3.1 there was no standard means to serialize JSON. eXist 3.0-RC1 adds support for XQuery 3.1, but also maintains the older features for backward compatibility. – Joe Wicentowski Mar 19 '16 at 00:47
0

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

Mark Miller
  • 3,011
  • 1
  • 14
  • 34