2
votes

Am new to XQuery. I have written below query which is working perfectly and producing results in csv format from an xml stored in Marklogic.

xquery version "0.9-ml"
let $data := someQuery
return 
for $i in $data

return
fn:string-join((

$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()

),","
)

Output:

abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,

I have a new requirement to add static headers before the data. And the expected output will be like

Expected Output:

HEAD1, HEAD2, HEAD3,
abc,def,adc,
dff,eef,ddf,
.
.
.
.
fff,eed,ddg,

Just adding HEAD1, HEAD2, HEAD3, in the top row. The headers are not part of the XML. They should be part of query in simple Strings with static data and can be modified at anytime in query itself. I have tried to add the below code segment to the query. But the query is not running in Marklogic query console.

concat("HEAD1,"HEAD2","HEAD3"),

Any solution is highly appreciated. Thanks in advance.

1

1 Answers

3
votes

Add it before the FLWOR expression:

xquery version "1.0-ml";

string-join(("HEAD1","HEAD2","HEAD3"),","),
let $data := someQuery
return 
for $i in $data

return
fn:string-join((

$i/PATHTOFILED_1/text(),
$i/PATHTOFILED_2/text(),
.
.
.
.
$i/PATHTOFILED_N/text()

),","
)

BTW, I really wouldn't use to 0.9-ml dialect unless you have a very compelling reason to do so. It works, but it is really only still there for compatibility.