<div dir="ltr">Hi Alex,<br><br>Me again. I read through your expected result too fast and missed the b4 part :-)<br><br>So indeed you need a copy modify:<br><br>...<div>let $fields :=<br><form13FFile><br>{<br>for $c in $data<br>return <br>  copy $new-node := $c<br>  modify insert nodes <b4>test</b4> as first into $new-node<br>  return $new-node  <br>}<br></form13FFile></div><div>...</div><div><br></div><div>I hope I got it right this time!<br><br>Kind regards,</div><div>Ghislain</div><div><br><br>On Wed, Jul 6, 2016 at 11:19 AM, Ghislain Fourny <<a href="mailto:g@28.io">g@28.io</a>> wrote:<br>><br>> Hi Alex,<br>><br>> You can either use a query, or updates. Your query mixes both, which is why you are getting an error, as updates can't be nested in expressions that produce items.<br>><br>> In your case though, since you need to modify the top-level, a query makes most sense, and the good news is it's really simple.<br>><br>> ...<br>> let $fields :=<br>> <form13FFile>{<br>>   $data<br>> }</form13FFile><br>> ...<br>><br>> Then you can replace the file with the results of the query as you do with file:write.<br>><br>> If you really want to update, you can do something like:<br>><br>> replace node $data with<br>>   <form13FFile>{<br>>     $data<br>>   }</form13FFile><br>><br>> but then you can't use the file:write machinery and need to rely on the underlying engine to do the edit in place.<br>><br>> In the more general case where you need to edit plenty of places in your document, you can use the copy-modify-return expression. It's very handy to create an updated copy.<br>><br>> I hope it helps!<br>><br>> Kind regards,<br>> Ghislain<br>><br>><br>><br>> On Wed, Jul 6, 2016 at 11:10 AM, Alex Muir <<a href="mailto:alex.g.muir@gmail.com">alex.g.muir@gmail.com</a>> wrote:<br>> ><br>> ><br>> > Greetings,<br>> ><br>> > Running in Basex I get the following error:<br>> ><br>> > XUST0001 element constructor: no updating expression allowed.<br>> ><br>> > When trying to insert insert nodes test as first into $c<br>> ><br>> > I feel like my code is following the examples I see online although clearly I've got something wrong.<br>> ><br>> > How do I insert a node with a for loop?<br>> ><br>> > declare namespace db="<a href="http://basex.org/modules/db">http://basex.org/modules/db</a>";<br>> > declare namespace file="<a href="http://expath.org/ns/file">http://expath.org/ns/file</a>";<br>> > declare variable $form13FFileNumber as xs:string external;<br>> ><br>> > let $data := db:open('13F')//data[contains(edgarSubmission/formData/coverPage/form13FFileNumber,$form13FFileNumber)]<br>> ><br>> > let $fields :=<br>> ><br>> > <form13FFile><br>> > {<br>> > for $c in $data return<br>> > insert nodes <b4>test</b4> as first into $c  <br>> > }<br>> > </form13FFile><br>> ><br>> > return file:write(concat('../OUT/' , $form13FFileNumber , '.xml'), $fields)<br>> ><br>> > To be more clear my xml looks something like<br>> ><br>> >    <data><br>> >      <first_Child>text</first_child><br>> >    </data><br>> >    <data><br>> >      <first_Child>text</first_child><br>> >    </data><br>> ><br>> > and I would like it to adjust to<br>> ><br>> > <form13FFile><br>> >     <data><br>> >      <b4>test</b4><br>> >      <first_Child>text</first_child><br>> >     </data><br>> >     <data><br>> >      <b4>test</b4><br>> >      <first_Child>text</first_child><br>> >     </data><br>> > </form13FFile><br>> ><br>> ><br>> ><br>> > Regards<br>> > Alex<br>> > <a href="http://tech.jahtoe.com">tech.jahtoe.com</a><br>> > <a href="http://bafila.jahtoe.com">bafila.jahtoe.com</a><br>> ><br>> > _______________________________________________<br>> > <a href="mailto:talk@x-query.com">talk@x-query.com</a><br>> > <a href="http://x-query.com/mailman/listinfo/talk">http://x-query.com/mailman/listinfo/talk</a></div></div>