Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to a specific document in a BaseX DB using XQuery

Tags:

xml

xquery

basex

If I add two documents to a BaseX DB, let's say normal.xml and normal2.xml, is there a way to refer to each one individually?

I know about the doc() function, but it looks at the filesystem for their location, rather than in the database itself.

For instance, if I query with this: doc("normal.xml")/name, then I will get an error about normal.xml not being found.

I also tried: basex:db("my-db-name")/doc("normal.xml")/name and received the same error.

Any ideas?

like image 698
Ryan Avatar asked Jul 29 '10 14:07

Ryan


1 Answers

I ended up asking on the BaseX mailing list and the answer I received is as follows:

for $doc in collection('test-db')
    where matches(document-uri($doc), 'example.xml')
return $doc

I also inquired as to why there was no direct way to reference the document one would want to extract data from in constant time, and the response was:

This is due to the XQuery data model. XQuery has no formal notion of files inside a database/collection (Christian might correct me if I am wrong), so the collection() command returns any sequence of (document-)nodes for the given database. It is up to the user to pick those nodes he wants to process.

We are currently looking for ideas on navigating collections more intuitively; something similar to "collection('db/path-to/document.xml')" that should still conform to the current W3C recommendation. As the subsequent document-uri() usually works well enough this is not too high on our priority list.

Thanks for the help though.

like image 140
Ryan Avatar answered Nov 09 '22 11:11

Ryan