[xquery-talk] Add value to result before a for loop

David Carlisle davidc at nag.co.uk
Wed Jul 18 06:04:29 PDT 2012

On 18/07/2012 13:49, Gary Larsen wrote:
> Hi,
> I can’t seem to find valid syntax for output of a value before a for
> loop.  In this example I want to output a header line ($hdr) to the results.
> Thanks for your help,
> Gary
> let$doc :=
> <doc>
> <item><id>1</id><name>item 1</name></item>
> <item><id>2</id><name>item 2</name></item>
> </doc>
> let$hdr :=concat('Id', codepoints-to-string(9), 'Name',
> codepoints-to-string(10))
> for$i in$doc/item return
> concat($i/id, codepoints-to-string(9), $i/name, codepoints-to-string(10))

for is just an expression so you could for example use
concat($hdr,for $i in ......)

of course in this simple example you don't need the variable at all as 
$hdr is already just a call to concat you could return the single expression

concat('Id', codepoints-to-string(9), 'Name',
for $i in$doc/item return
concat($i/id, codepoints-to-string(9), $i/name, codepoints-to-string(10)

or more simply

('Id	Name',$doc/item/concat(id,'	',name)),'

the let and for clauses aren't really needed,


google plus: https:/profiles.google.com/d.p.carlisle

The Numerical Algorithms Group Ltd is a company registered in England
and Wales with company number 1249803. The registered office is:
Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom.

This e-mail has been scanned for all viruses by Star. The service is
powered by MessageLabs. 

More information about the talk mailing list