Dear Luke,
I completely agree, serious database applications cannot exist without integrity and consistency checks. In our own projects, checks are realized in XQuery. Depending on the requirements, we choose one of the following alternatives:
1. If we need to ensure that every single incoming database entity is correct, we apply checks before each update. The resources are also updated via XQuery (see [1,2] for more information) if all checks are successful.
2. If we have control over the data that will be added to a database, and if we know that it’s correct as long as the application has no bugs, it is sufficient to check the database in regular periods (e.g., once every night). This allows us to use the full range of APIs for updating the database (although most of our applications are fully written in XQuery and RESTXQ [3]).
Some straightforward examples how your checks could look like:
Is there any way to ensure that when X.xml is added to the database that the object IDs that it is referring to actually exist in the database too?
let $doc := <mapping object_from_id=”1” object_to_id=”2” /> let $ids := db:open('your-db')//object/@id/data() where not($ids = $doc/@object_from_id and $ids = $doc/@object_to_id) return error((), 'Unknown id')
how can I ensure that when a new object xml file is added that it is not using an ID that already exists?
let $new-id := '12345' where db:open('your-db')//object/@id = $new-id return error((), 'Id has already been assigned')
You can organize the highest assigned id in the root node of your database document or (if you work with multiple documents per database) in a dedicated meta document.
Hope this helps Christian
[1] http://docs.basex.org/wiki/Database_Module [2] http://docs.basex.org/wiki/XQuery_Update [3] http://docs.basex.org/wiki/RESTXQ
On Thu, Dec 12, 2019 at 3:08 AM ERRINGTON Luke Luke.Errington@sydac.com wrote:
Hello,
We are evaluating moving from an RDBMS (Oracle), to BaseX as much of our source data originate in XML files and converting to tables in a relational schema is painful. In general BaseX looks great!
However, one thing that we lose is referential integrity, and the ability to validate data in one XML file that is referring to data in another. Are there any possibilities within BaseX or an additional module that can do this?
For example: • Can we validate using a schema that applies across a collection of documents, rather than just one? • Can we use Schematron (which looks cool) to apply its inteRnal XPaths to the entire collection of documents? • Or both? • Something else?
We could try using XLinks, but that would involve changing our XML data/structure, and my understanding is that BaseX doesn’t support (let alone validate) them, anyway.
A situation I have in mind is something like (very, very simplified):
A.xml
<object id=”1” name=”One”> </object>
B.xml
<object id=”2” name=”Two”> </object>
X.xml
<mapping object_from_id=”1” object_to_id=”2” />
Is there any way to ensure that when X.xml is added to the database that the object IDs that it is referring to actually exist in the database too?
I would also like to be able to ensure that all of the <object>s in the database have unique id attributes. A schema can do this within a file, but how can I ensure that when a new object xml file is added that it is not using an ID that already exists?
Thanks for any answers, Luke