Put each XQuery result on a new line
Asked Answered
C

4

16

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

Cineraria answered 7/2, 2012 at 19:12 Comment(0)
L
28

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

Ln answered 7/2, 2012 at 19:32 Comment(0)
L
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> 
Ln answered 14/8, 2013 at 14:59 Comment(0)
L
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.

Ln answered 6/3, 2015 at 20:30 Comment(0)
C
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/>
Cheesy answered 8/2, 2012 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.