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

Alex Muir alex.g.muir at gmail.com
Wed Jul 6 02:27:48 PDT 2016


Hi Ghislain,

That worked!

Cheers!


Regards
Alex
tech.jahtoe.com
bafila.jahtoe.com

On Wed, Jul 6, 2016 at 9:22 AM, Ghislain Fourny <g at 28.io> wrote:

> Hi Alex,
>
> Me again. I read through your expected result too fast and missed the b4
> part :-)
>
> So indeed you need a copy modify:
>
> ...
> 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>
> ...
>
> I hope I got it right this time!
>
> Kind regards,
> Ghislain
>
>
> On Wed, Jul 6, 2016 at 11:19 AM, Ghislain Fourny <g at 28.io> wrote:
> >
> > 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/9eb0bf3e/attachment.html>


More information about the talk mailing list