Chris V.
Chris V.

Reputation: 1143

Put each XQuery result on a new line

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! :)

Upvotes: 17

Views: 21659

Answers (4)

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

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.

Upvotes: 5

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

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> 

Upvotes: 10

Christian Gr&#252;n
Christian Gr&#252;n

Reputation: 6229

...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.

Upvotes: 28

David Graf
David Graf

Reputation: 1172

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/>

Upvotes: 4

Related Questions