Mixing non-updating and updating expressions in a single query
Friday, 25 February 2011 · 2 messages
CG
Christian Grün25 Feb 2011 17:18 #
Sony,
this query might do what you want:
let $node :=
copy $c :=
<entry>
<title>How to use updating and non-updating expressions together</title>
<child>child 1</child>
<child>child 2</child>
</entry>
modify (
for $child in $c//child
return replace value of node $child with "This is a child node"
)
return $c
return
insert node $node into doc('seconddocument')Due to the sometimes confusing flexibility of XQuery, this is just one way to do it. Note that you always need to specify complete FLWOR expressions; e.g., in your first query you forgot to add a "return" statement after the first "let" clause. The syntax would be simpler if Transform expressions (copy .. modify .. return) were part of the FLWOR expression, and it might be that future version of XQuery won't make a difference here anymore.
Christian
Quoted text · Sony Vijay · 40 lines
On Fri, Feb 25, 2011 at 5:07 PM, Sony Vijay <sony.vibh at gmail.com> wrote: > @Christian and Lukas: Many thanks for helping me with the transform > expression so far. > However, I am not sure if we can use both non-updating and updating > expressions in a single query ? Meaning, I am trying to modify one document > and insert the modified document into the second document. > Example: > > let $newchild := "This is a child node" > copy $c := <entry><title>How to use updating and non-updating expressions > together</title> > <child>child 1<child> > <child>child 2</child> > </entry> > modify ( > for $child in $c//child > return replace value of node $child with $newchild > ) > return insert node $c into doc('seconddocument') > > I also tried: > > let $newchild := "This is a child node" > copy $c := <entry><title>How to use updating and non-updating expressions > together</title> > <child>child 1<child> > <child>child 2</child> > </entry> > return > modify ( > for $child in $c//child > return replace value of node $child with $newchild > ) > insert node $c into doc('seconddocument') > > Of course, I can achieve this when I use two xqueries (one to return the > modified document and the other to insert it into a second document). But, > it would be great if I could perform both actions in a single query. > Thanks, > Sony
SV
Sony Vijay25 Feb 2011 18:39 #
Christian,
Wow. Thanks for the super fast reply.
Sony
On Fri, Feb 25, 2011 at 11:18 AM, Christian Grün
<christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 77 lines
> Sony, > > this query might do what you want: > > let $node := > copy $c := > <entry> > <title>How to use updating and non-updating expressions > together</title> > <child>child 1</child> > <child>child 2</child> > </entry> > modify ( > for $child in $c//child > return replace value of node $child with "This is a child node" > ) > return $c > > return > insert node $node into doc('seconddocument') > > > Due to the sometimes confusing flexibility of XQuery, this is just one > way to do it. Note that you always need to specify complete FLWOR > expressions; e.g., in your first query you forgot to add a "return" > statement after the first "let" clause. The syntax would be simpler if > Transform expressions (copy .. modify .. return) were part of the > FLWOR expression, and it might be that future version of XQuery won't > make a difference here anymore. > > Christian > ___________________________ > > > On Fri, Feb 25, 2011 at 5:07 PM, Sony Vijay <sony.vibh at gmail.com> wrote: > > @Christian and Lukas: Many thanks for helping me with the transform > > expression so far. > > However, I am not sure if we can use both non-updating and updating > > expressions in a single query ? Meaning, I am trying to modify one > document > > and insert the modified document into the second document. > > Example: > > > > let $newchild := "This is a child node" > > copy $c := <entry><title>How to use updating and non-updating expressions > > together</title> > > <child>child 1<child> > > <child>child 2</child> > > </entry> > > modify ( > > for $child in $c//child > > return replace value of node $child with $newchild > > ) > > return insert node $c into doc('seconddocument') > > > > I also tried: > > > > let $newchild := "This is a child node" > > copy $c := <entry><title>How to use updating and non-updating expressions > > together</title> > > <child>child 1<child> > > <child>child 2</child> > > </entry> > > return > > modify ( > > for $child in $c//child > > return replace value of node $child with $newchild > > ) > > insert node $c into doc('seconddocument') > > > > Of course, I can achieve this when I use two xqueries (one to return the > > modified document and the other to insert it into a second document). > But, > > it would be great if I could perform both actions in a single query. > > Thanks, > > Sony >
Written for BaseX 6. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery