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

4 Answers 4

27

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

0
10

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.