Hi Christian,
Thank you for the response.
First, I did not realize that the fn:collection argument may try to locate the disk in the file system. That is good to know as we would probably want to use the db:open to eliminate this possibility.
I have not looked at the referenced wiki article below in quite some time. My confusion mainly arises from the documentation for the Database Module in the XQuery portal (http://docs.basex.org/wiki/Database_Module). Throughout this page, the examples provided for the functions seem to indicate that it is possible to provide a single name which maps to a single document-node. For example the db:delete function states:
- db:delete("DB", "docs/dir/doc.xml") deletes the document docs/dir/doc.xml in the database DB.
...when actually it deletes all nodes under the collection named "docs/dir/doc.xml". I think it is a subtle, but fairly important detail. I think the same principle applies to the add method where the example states:
- db:add("DB", document { <a/> }, "doc.xml") adds the document node to the database DB under the name doc.xml.
I read this (based on my view of a 1 to 1 mapping between a document name and a document node) as assigning this name to the document-node which is added, as opposed to adding the new document-node to the collection under the name "doc.xml". The difference is subtle here, but again important.
I suppose it can all be summarized by stating that all paths in the functions are just that, paths. They refer to collections in terms of the XQuery recommendation and never to documents. When I find more time, I can provide more detailed recommendations for the above wiki page.
This brings me to a new point. Given your clarification, I think the new helper functions are inconsistent with the other functions. For example, the db:is-xml documentation states that it "Checks if the specified resource exists and if it is an XML document". That being the case, I would think it would return false if my path argument actually contained two document-nodes. However, this is not the case. This is further confusing in that if I have a hierarchy in the path (e.g. parent_folder/child_doc.xml) and provide a higher level path in the hierarchy (e.g. parent_folder only) as the argument the function returns false. I assume the reason for this is clarified by the db:exists function which states; "Checks if the specified database or resource exists. false is returned if a database directory is specified". Since the higher level path is a directory, the db:is-xml function returns false because it does not exist (according to the db:exist function). However, it would seem that the above discussion established that all paths are just directories. In fact, it would seem from some quick tests that I am even able to store binary resource and XML under the same path (which I would expect with folders but not with documents). But this makes a lot of these helper functions a bit confusing.
I hope this is useful. I still think that having a true document-node to document mapping would be useful, as it would allow one to use the handy database module functions such as add, delete, rename, and replace confidently.
Jack
-----Original Message----- From: Christian GrĂ¼n [mailto:christian.gruen@gmail.com] Sent: Monday, February 06, 2012 10:08 PM To: J Gager Cc: basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Collections and Documents
Dear Jack,
it's true that the fn:doc() function cannot be used in BaseX to access documents in a database - except for the special case that the database is a disk-based representation of a single document. It is possible, however, to access single documents in a database by specifying a full path as fn:collection() argument or adding a second argument to the db:open() function [1]:
- collection("DB/doc.xml") - db:open("DB", "doc.xml")
While the first query will potentially try to also locate the disk in the local file system, the second one restricts the access to documents stored in databases.
As XQuery was never focused on databases, it is sometimes tricky to do justice to all the specification details, in particular because some of the features are implementation defined while others are not. Regarding the details on the document-uri() function (..thanks btw for your diligent lookup..), it would probably be more consistent to return an empty sequence.
If you feel that the Wiki article referenced below is somewhat incomplete, feel free to give us more feedback or (..even better..) feel invited to extend it by yourself ;)
Thanks for your feedback, hope this helps, Christian
PS: BaseX 7.1 is close...
[1] http://docs.basex.org/wiki/Databases#Access_Resources ___________________________
In reading the documentation and mail archives, it seems to me that BaseX does not support documents in terms of having a single URI referring to a single document node. For example, if I add a document using the db:add function:
db:add("DB", document { <a/> }, "doc.xml")
And subsequently attempt to retrieve it using the doc function:
fn:doc("doc.xml")
I get a FODC0002 error. This is discussed in the mail archives, and so far as I can tell this is because BaseX only works with collections. It does not really address the concept of a documents (excuse me if I am over simplifying the issue here). In other words, I am free to add more document nodes under the name "doc.xml" therefore it is technically a collection as opposed to a document. Is this correct?
A fairly early mailing list thread from 2010-07 addresses a similar and suggests an alternative approach where one might retrieve a document in a collection as follows:
for $doc in collection('DB')
where matches(document-uri($doc), 'doc.xml')
return $doc
But, doesn't this run afoul of the W3C recommendation which states for the fn:docucument-uri function that:
"In the case of a document node $D returned by the fn:doc function, or a document node at the root of a tree containing a node returned by the fn:collection function, it will always be true that either fn:document-uri($D) returns the empty sequence, or that the following expression is true: fn:doc(fn:document-uri($D)) is $D. It is implementation-defined whether this guarantee also holds for document nodes obtained by other means, for example a document node passed as the initial context node of a query or transformation."
In other words, if document-uri is returning 'doc.xml' for a node in collection('DB') then I should be able to get that same document node using the doc function.
I ask these questions because we are interested in being able to maintain XML artifacts as singular documents, and using the collection hierarchy to do things such as storing temporary and archived version of
these artifacts.
The approach we are taking right now assumes that there is only one document-node under each 'document'. I want to understand the limitations of the collection and document implementation in BaseX so that I don't make any wrong assumptions (as I think I already have).
Finally, assuming the interpretations are correct, are there plans to make any changes in the implementation to support something more in line with the hierarchy of db->collection->document? I have seen in previous threads that you were seeking feedback on this, and I am curious to know if you still are. Is there a major drawback you see to this model that perhaps I have not considered? Any feedback is
appreciated.
Thanks,
Jack Gager Metadata Technology