Reputation: 1614
I get the following error:
XUST0001 element constructor: no updating expression allowed.
When trying to insert insert nodes test as first into $c
I feel like my code is following the examples I see online although clearly I've got something wrong.
How do I insert a node with a for
loop?
declare namespace db="http://basex.org/modules/db";
declare namespace file="http://expath.org/ns/file";
declare variable $form13FFileNumber as xs:string external;
let $data := db:open('13F')//data[contains(edgarSubmission/formData/coverPage/form13FFileNumber,$form13FFileNumber)]
let $fields :=
<form13FFile>
{
for $c in $data return
insert nodes <b4>test</b4> as first into $c
}
</form13FFile>
return file:write(concat('../OUT/' , $form13FFileNumber , '.xml'), $fields)
To be more clear my xml looks something like
<data>
<first_Child>text</first_child>
</data>
<data>
<first_Child>text</first_child>
</data>
and I would like it to adjust to
<form13FFile>
<data>
<b4>test</b4>
<first_Child>text</first_child>
</data>
<data>
<b4>test</b4>
<first_Child>text</first_child>
</data>
</form13FFile>
Upvotes: 0
Views: 764
Reputation: 7279
[I have already answered "offline", but for reference I'm also putting my suggestion here]
I think what you need is a copy-modify-return expression, which is how you create updated copies.
let $fields :=
<form13FFile>
{
for $c in $data
return
copy $new-node := $c
modify insert nodes <b4>test</b4> as first into $new-node
return $new-node
}
</form13FFile>
Upvotes: 1
Reputation: 38682
It seems you want to create a new result set: you don't need XQuery Update here at all! XQuery Update is great if you want to change existing documents, but not required at all when constructing new ones.
<form13FFile>
{
for $c in $data return
<b4>test</b4>
}
</form13FFile>
Or if you want to stick with the as first
semantics (I expect the static test element will change in future), reverse $data
before looping:
<form13FFile>
{
for $c in reverse($data) return
<b4>test</b4>
}
</form13FFile>
Upvotes: 2