How to "stitch together" results into a single XML document with eXist-db?

70 views Asked by At

How do I create a "single" result of one notes element as the root node, with multiple child nodes of note elements?

Here's the query in eXide:

xquery version "3.0";
    
for $note in collection('/db/tmp')/note
return <notes>{$note}</notes>

where the output is:

nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ lynx http://localhost:8080/exist/rest/db/scripts/notes.xq --dump
<notes>
    <note>
bar
</note>
</notes>
<notes>
    <note>
foo
</note>
</notes>nicholas@mordor:~/flwor$ 
nicholas@mordor:~/flwor$ 

How can I get a single document, with a single root element of notes which Firefox won't choke on?

there are two files, foo.xml and bar.xml in the collection.

1

There are 1 answers

3
line-o On BEST ANSWER

XML must have exactly one root element. Also without knowing the structure in the data I would assume you want to get all notes regardless how deep they are nested. To achieve that use collection($col)//note

xquery version "3.1";
    
<notes>
{
  for $note in collection('/db/tmp')/note
  return $note
}
</notes>

or

xquery version "3.1";
    
<documents>
{
  for $doc in collection('/db/tmp')/document()
  return <notes>{$doc/note}</notes>
}
</documents>