[xquery-talk] How do I insert a node as first element within a for loop?

Ghislain Fourny g at 28.io
Wed Jul 6 02:19:48 PDT 2016


Hi Alex,

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.

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.

...
let $fields :=
<form13FFile>{
  $data
}</form13FFile>
...

Then you can replace the file with the results of the query as you do with
file:write.

If you really want to update, you can do something like:

replace node $data with
  <form13FFile>{
    $data
  }</form13FFile>

but then you can't use the file:write machinery and need to rely on the
underlying engine to do the edit in place.

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.

I hope it helps!

Kind regards,
Ghislain


On Wed, Jul 6, 2016 at 11:10 AM, Alex Muir <alex.g.muir at gmail.com> wrote:
>
>
> Greetings,
>
> Running in Basex 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>
>
>
>
> Regards
> Alex
> tech.jahtoe.com
> bafila.jahtoe.com
>
> _______________________________________________
> talk at x-query.com
> http://x-query.com/mailman/listinfo/talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://x-query.com/pipermail/talk/attachments/20160706/2327ba41/attachment.html>


More information about the talk mailing list