I want to copy a document and modify one of the nodes inside it, preferably in one transaction. It seems like the best way to do this is to clone the old document, use the in-mem-update library to modify it, then write it to a new URI. But what I have so far just writes the original document with no modifications.
doc1.xml:
<root>
<foo>bar</foo>
</root>
What I have so far:
declareUpdate();
const mem = require("/custom-modules/utils/in-mem-update.xqy");
let oldUri = "/test/doc1.xml";
let newUri = "/test/doc2.xml";
let oldDoc = cts.doc(oldUri);
let newDoc = fn.head(xdmp.unquote(JSON.parse(JSON.stringify(oldDoc))));
let nb = new NodeBuilder();
nb.startElement("foo");
nb.addText("baz");
nb.endElement();
mem.nodeReplace(fn.head(newDoc.xpath("/root/foo")), nb.toNode());
xdmp.documentInsert(newUri, newDoc);
Is there a better way to do this?