xquery - Is there a way to number records extracted . -


i need extract data relational database xml, , need assign number each record extract. wrote following xquery...

<myrecords> { let $i := 0 $territories in collection("northwind.dbo.territories")/territories let $i := $i + 1 return <territory rec_count="{$i}"> {$territories/territorydescription/text()} </territory> } </myrecords> 

... don't result expected:

<myrecords> <territory rec_count="1">westboro</territory> <territory rec_count="1">bedford</territory> <territory rec_count="1">georgetown</territory> … 

the record number isn't incremented. why?

xquery functional language without side effects — can't create notion of state in xquery might in java or c#, example. in cases, can simulate state using recursive function, solve problem there simpler solution: can use positional variable. here's xquery:

<myrecords> { $territories @ $i in collection("northwind.dbo.territories")/territories return <territory rec_count="{$i}"> {$territories/territorydescription/text()} </territory> } </myrecords> 

and here's (hoped-for) result:

<myrecords> <territory rec_count="1">westboro</territory> <territory rec_count="2">bedford</territory> <territory rec_count="3">georgetow</territory> … 

to add bit dimitre's answer, here's how you're trying do:

<myrecords> { $territories @ $i in collection("northwind.dbo.territories")/territories return <territory rec_count="{$i}"> {$territories/territorydescription/text()} </territory> } </myrecords> 

as dimitre noted, let inside not preserve value each iteration. @ clause of need.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

c++ - Accessing inactive union member and undefined behavior? -

php - Get uncommon values from two or more arrays -