I'm having trouble understanding document/collection semantics with xquery update.
If a create a document and add another document to the same path, that path becomes a collection with two document nodes. However it seems to be impossible to manipulate these document nodes individually.
See the following console output:
create db test
Database 'test' created in 137.25 ms.
open test
Database 'test' was opened in 2.15 ms.
add to test.xml <root1/>
Path "test.xml" added in 19.44 ms.
add to test.xml <root2/>
Path "test.xml" added in 2.57 ms.
list test
Input Path Type Content-Type Size --------------------------------------- test.xml xml application/xml 2 test.xml xml application/xml 2
2 Resources.
xquery collection('test/test.xml')
<root1/><root2/> Query executed in 30.84 ms.
xquery doc('test/test.xml')
Stopped at line 1, column 20: [BXDB0006] Database path 'test/test.xml' must point to a single document.
xquery import module namespace functx = "http://www.functx.com"; collection('test/test.xml') ! (document-uri(.), functx:node-kind(.), .)
test/test.xml document-node<root1/> test/test.xml document-node<root2/> Query executed in 29.54 ms.
xquery delete node collection('test/test.xml')[2]
Query executed in 1.18 ms.
xquery import module namespace functx = "http://www.functx.com"; collection('test/test.xml') ! (document-uri(.), functx:node-kind(.), .)
test/test.xml document-node<root1/> test/test.xml document-node<root2/> Query executed in 43.83 ms.
xquery delete node db:open('test','test.xml')[2]
Query executed in 2.15 ms.
xquery import module namespace functx = "http://www.functx.com"; collection('test/test.xml') ! (document-uri(.), functx:node-kind(.), .)
test/test.xml document-node<root1/> test/test.xml document-node<root2/> Query executed in 36.77 ms.
Note that it doesn't seem possible to delete one of the document nodes, even using db:replace()
xquery db:replace('test','test.xml',collection('test/test.xml')[1])
Stopped at line 1, column 60: [BXDB0006] Database path '%' must point to a single document.
I don't understand this error message, unless db:replace is internally using the path of the document-node rather than the document-node itself.
delete test.xml
2 resource(s) deleted in 36.35 ms.
But I can delete both items at the same path.
The way I discovered this is that I accidentally added a document to an existing path using db:add(). However, I was unable to correct the mistake and remove the documents without completely deleting the path and re-adding its members. I don't see any way to do this with a single xquery update PUL.
It seems to me that this is a bug. Either xquery update expressions like 'delete node' should work on document nodes, or adding document-nodes to a path which has a document-node should be illegal and raise an error when attempted.