I suspected that XQuery's database-indifference had something to do with this.
It's not clear to me what clause of the XQuery Update specification you link disallows deleting a document node. Is it this one?
"If any node in $tlist has no parent, it is removed from $tlist (and is thus ignored in the following step)."
If so, could this be resolved by having a document-node() have some kind of db:database-node node type as a parent? It would also allow us to use db:*() functions by passing around database nodes directly instead of as string names. It would also allow us to determine the database of a path without using string manipulation. I currently have to use these functions a lot:
declare function local:uri-db-and-path($docuri as xs:string) as xs:string+ { let $pathsep := '/' return (fn:substring-before($docuri, $pathsep), fn:substring-after($docuri, $pathsep)) }; declare function local:doc-db-and-path($doc as document-node()) as xs:string+ { local:uri-db-and-path(document-uri($doc)) };
I think the github issue you link to rather underestimates how broken this is. This is really a bug and not a nice-to-have. As I said, I added a path to an existing document by accident. As it is right now, you can either replace the document with db:replace() (which isn't what I wanted to do), or silently add a new document to the same path with db:add(), but there's no way to add to a path and get an error if a document exists at the path (what I wanted). A mistaken db:add() leaves the database in a state that is *very difficult to detect and fix* (I think *impossible* to fix atomically with a single Pending Update List, which can be a problem in a multi user environment), and it is made all the more difficult because of the XQuery Update limitation on document-nodes.
I also don't understand why that db:replace() expression doesn't work. Reminder:
xquery db:replace('test','test.xml', <root/>)
Stopped at line 1, column 60: [BXDB0006] Database path '%' must point to a single document.
So it seems db:replace() needs a destination path that resolves to a single document to work. I'm not sure why this would be since the destination path will be replaced anyway.
Thank you very much for your prompt reply and enlightening links.