<div dir="ltr">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>...<div>let $fields :=</div><div><form13FFile>{<br>  $data<br>}</form13FFile><br>...</div><div><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><div>replace node $data with <br>  <form13FFile>{<br>    $data<br>  }</form13FFile></div><div><br></div><div>but then you can't use the file:write machinery and need to rely on the underlying engine to do the edit in place.</div><div><br></div><div>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.</div><div><br></div><div>I hope it helps!<br><br>Kind regards,<br>Ghislain<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></div>