15

Is it possible to put each XQuery result on a new line? I thought I remembered there was an attribute to set at the beginning of the document, but can't remember. :/

I have this .xq file:

(: declare default element namespace "http://www.w3.org/2001/XMLSchema"; :)
for $x at $i in doc("zoo.xml")//animal
return <li>{$i}. {$x/name/text()}</li> 

I get correct results, but all of them are on a single line, when I really want something like:

<li>1. Zeus</li>
<li>2. Fred</li>
...

Is this possible? Thanks in advance for your answers! :)

Chris V.
  • 1,123
  • 5
  • 22
  • 50

4 Answers4

26

...you can add a newline string as second part of the return clause:

(: declare default element namespace "http://www.w3.org/2001/XMLSchema"; :)
for $x at $i in doc("zoo.xml")//animal
return (<li>{$i}. {$x/name/text()}</li>, '&#xa;')

The way how results are serialized also depends on the specific XQuery processor.

Sled
  • 18,541
  • 27
  • 119
  • 168
Christian Grün
  • 6,012
  • 18
  • 34
9

XQuery 3.0 provides a new serialization option item-separator, which can be specified in the query prolog:

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:item-separator "&#xa;";

for $x at $i in doc("zoo.xml")//animal
return <li>{$i}. {$x/name/text()}</li> 
Christian Grün
  • 6,012
  • 18
  • 34
5

The XQuery 3.1 Serialization specification provides the new "adaptive" serialization mode, which outputs each XQuery result on a new line.

Some XQuery processors use this mode as new default.

Christian Grün
  • 6,012
  • 18
  • 34
4

The XQuery engine you are using might have an option to indent the output. E.g. in Zorba, indent option works like this:

zorba -i -q 'for $x in 1 to 10 return <a/>'
<?xml version="1.0" encoding="UTF-8"?>
<a/>
<a/>
<a/>
<a/>
<a/>
<a/>
<a/>
<a/>
<a/>
<a/>
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
David Graf
  • 1,152
  • 2
  • 13
  • 24