> Is there a better way to do this?
Your query could probably be shortened as follows:
let $new := db:open($newName) update {
.//* ! (insert node attribute UID { generate-id() } into .)
}
...
It does the same as your original query. But maybe you were asking for
a better solution to compare documents against each other?