Reputation: 365
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?
Upvotes: 5
Views: 3814
Reputation: 41
The doc()
function is all you need assuming you know. Assuming you have created and opened a database named 'mydb,' if you add a document with the following command:
ADD TO your_name /some/path/on/filesystem.xml
You can always open the following document using:
XQUERY doc('mydb/your_name')
ADD TO will ignore the file name you gave it and use the name you specified in the URI. Be warned though: there is no guarantee of uniqueness of 'your_name' when you run that command. And if it's not unique, the (doc) function will throw a BaseX error (not part of XQuery standard) saying it matches more than one document.
Upvotes: 4
Reputation: 365
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.
Upvotes: 9
Reputation: 61
The following query might help you:
for $doc in collection('your-db-name')
let $path := base-uri($doc)
where ends-with($path, 'normal.xml')
return $doc/name
Upvotes: 6