I'm trying to wrap a node with another new node using XQuery update and I'm stuck in several different errors let me give you a short example:
Initial XML is
<a id="test">
<title>title</title>
</a>
XQuery code is:
copy $x := db:open('testdb')/a[@id eq 'test']/title
modify replace node $x
with <b>{$x}</b>
return <ok/>
I'm using basex as the surrounding database and this is why I'm using the db:open function.
What I want to obtain in the end is
<a id="test">
<b>
<title>title</title>
</b>
</a>
But I receive an error:
[XUDY0009] Target has no parent: element title {...}.
Note: the result of query:
db:open('testdb')/a[@id eq 'test']/title
is
<title>title</title>
If I modify the query as follows
copy $x := db:open('testdb')/a[@id eq 'test']/title
modify replace node db:open('testdb')/a[@id eq 'test']/title
with <b>{$x}</b>
return <ok/>
then the error is
[XUDY0014] Node was not created by copy clause: element title {...}.
What is the proper way of doing this kind of update queries?