On Sun, 2013-08-25 at 16:42 +0100, Thomas Daly wrote:
Thank you very much for your reply.
I assume that in your example commands, mydoc.xml already existed as a file on your hard drive.
Yes, that's right, I wanted to show the two modes in which the ADD command work: with a file and with an XML fragment.
In my case, the system looked for mydoc.xml rather than create it:
CREATE DB db1
Database 'db1' created in 14.41 ms.
ADD mydoc.xml
Resource "/home/tcd/mydoc.xml" not found.
Currently, it is not possible to use the ADD command to create an empty document.
Your 2nd ADD command worked, which I assume gives us an XML file empty apart from a root node <a/>:
ADD TO doc2.xml <a/>
Path "doc2.xml" added in 2.72 ms.
INFO STORAGE
PRE DIS SIZ ATS ID NS KIND CONTENT
0 1 2 1 0 0 DOC doc2.xml 1 1 1 1 1 0 ELEM a
Now I'd like to add a new node to the new document. I've tried two variants as follows:
XQUERY insert node <newnode/> into <a/>
Query executed in 2.09 ms.
This query does not update the database. What it does is to create an XML fragment <a/> in the main memory and insert the node <newnode/> in it. What you need is:
XQUERY insert node <newnode/> into /a
Check the XQuery Update Facility and section "3.7.1 Direct Element Constructors" of the XQuery spec [1] for more details.
XQUERY insert node <newnode/> into doc('doc2.xml')/a
Stopped at ., 1/33: [FODC0002] Resource '/home/tcd/doc2.xml' does not exist.
The path in the argument of the fn:doc() function must contain the database name, because the function can be used to access data from different databases within a single query. In your case it should look like this:
XQUERY insert node <newnode/> into doc('db1/doc2.xml')/a
Although the first XQUERY command appeared to work, if I export the XML file with the EXPORT command and examine it, unfortunately the new node is not present. The file just contains the <a/> node.
See my answer above: since your query did not change the database, the EXPORT command does not do what you expect.
Is there any variation of the EXPORT command that will simply echo the contents of the XML file to the terminal, rather than to a file? If not, that would be a useful addition.
That should do the job:
XQUERY doc('db1/doc2.xml')
Regards, Dimitar