You cannot do that in a single statement. You either need to:
- use a multi-statement call (main statements separated by a semi-colon)
- do the update in-memory, and passing the updated content to the insert
Regarding 1:
The Application Developers Guide has a section about Semi-Colon as a Statement Separator, but in short it comes down to writing your query as follows:
xquery version "1.0-ml";
xdmp:document-insert("/fo.xml", <a>1</a>)
;
xquery version "1.0-ml";
xdmp:node-replace(fn:doc("/fo.xml")/a, <b>1</b>)
Regarding 2:
There are libraries that provide methods that operate very similar to the xdmp:node-* ones, but operate on content that has not yet been persisted to the database instead. You will need to download those first, and upload them to your modules database in order to use them. The best version I am aware of is https://github.com/ryanjdew/XQuery-XML-Memory-Operations. You can download and install that using MLPM. With that library you could write something like:
import module namespace mem = "http://maxdewpoint.blogspot.com/memory-operations" at "/ext/mlpm_modules/XQuery-XML-Memory-Operations/memory-operations.xqy";
let $a := <a>1</a>
let $b := <b>1</b>
let $updated-a := mem:copy($a) ! (
mem:replace(., $a, $b),
mem:execute(.)
)
return xdmp:document-insert("/foo.xml", $updated-a)
HTH!