Need update insert with position variable XQuery help

I’ve got one instance of doctype //TOC:

<TOC xmlns:ino=“http://namespaces.softwareag.com/tamino/response2” xmlns:xsi . . . . >










<Book . . . .>
<LevelOne . . . .>
<LevelTwo . . . .>
<LevelTwo . . . .>


I’m trying to insert a new element under the Book element. This is supported in the TOC schema. I would like the value of the Position element to be the Book element position number, IE:

<TOC. . . >
<Book. . .>
1
<Level . . . . .

<Book. . .>
2
<Level . . . . .

<Book. . .>
3
<Level . . . . .

. . . . .

The closest I’m getting to doing this is with the XQuery:

update
for $a in input()//TOC
do (insert (for $b at $c in $a/Book return {$c}) into $a/Book)

But the result I get is this:

<TOC. . . >
<Book. . .>
1
2
3
<Level . . . . .

<Book. . .>
1
2
3
<Level . . . . .

<Book. . .>
1
2
3
<Level . . . . .

. . . . .

I tried different XQueries but to no avail. Can someone give a suggestion on how I can get the desired result with XQuery?

Thanks :slight_smile:

Sorry about the quote! I could not delete it . . . .

you might want to try


update
for $a in input()/TOC
for $b at $pos in $a/Book do
insert <Position>{$pos}</Position> into $b

Regards

Harald

:stuck_out_tongue: Thanks so much!

Awesome solution!