简体   繁体   English

使用XQuery引用BaseX DB中的特定文档

[英]Refer to a specific document in a BaseX DB using XQuery

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? 如果我将两个文档添加到BaseX DB中,假设normal.xmlnormal2.xml ,有没有办法分别引用每个文档?

I know about the doc() function, but it looks at the filesystem for their location, rather than in the database itself. 我知道doc()函数,但是它查看文件系统中它们的位置,而不是数据库本身。

For instance, if I query with this: doc("normal.xml")/name , then I will get an error about normal.xml not being found. 例如,如果我使用以下查询: doc("normal.xml")/name ,那么我将收到关于normal.xml的错误。

I also tried: basex:db("my-db-name")/doc("normal.xml")/name and received the same error. 我也尝试过: basex:db("my-db-name")/doc("normal.xml")/name并收到相同的错误。

Any ideas? 有任何想法吗?

I ended up asking on the BaseX mailing list and the answer I received is as follows: 我最终在BaseX邮件列表上询问,得到的答案如下:

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数据模型 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. XQuery在数据库/集合中没有正式的文件概念(如果我错了,Christian可能会纠正我),因此collection()命令返回给定数据库的任何(document-)节点序列。 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. 与“ collection('db / path-to / document.xml')”类似的内容仍应符合当前的W3C建议。 As the subsequent document-uri() usually works well enough this is not too high on our priority list. 由于随后的document-uri()通常可以很好地工作,因此在我们的优先级列表中并不太高。

Thanks for the help though. 谢谢您的帮助。

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

The doc() function is all you need assuming you know. 假设您已经知道doc()函数就是您所需要的。 Assuming you have created and opened a database named 'mydb,' if you add a document with the following command: 假设您已经创建并打开了一个名为“ mydb”的数据库,如果使用以下命令添加文档:

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. “添加到”将忽略您提供的文件名,并使用您在URI中指定的名称。 Be warned though: there is no guarantee of uniqueness of 'your_name' when you run that command. 但是请注意:运行该命令时不能保证“ your_name”的唯一性。 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. 而且,如果它不是唯一的,则(doc)函数将抛出BaseX错误(不是XQuery标准的一部分),表示它匹配多个文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM