我需要根据 BaseX 命令行工具的 -i
选项提供的上下文来转换一些 XML 文件:
$ basex --help
BaseX 8.4.2 [Standalone]
Usage: basex [-bcdiIoqrRstuvVwxXz] [input]
[input] XQuery or command file, or query string
-b<pars> Bind external query variables
-c<input> Execute commands from file or string
-d Activate debugging mode
-i<input> Assign file or database to context
-I<input> Assign input string to context
-o<output> Write output to file
-q<expr> Execute XQuery expression
-r<num> Set number of query executions
-R Turn query execution on/off
-s<pars> Set serialization parameter(s)
-t[path] Run tests in file or directory
-u Write updates back to original files
-v/V Show (all) process info
-w Preserve whitespaces from input files
-x Show query plan
-X Show query plan before/after compilation
-z Skip output of results
我不想使用数据库,因为我需要显式创建数据库。我可以使用 basex -i context.xml test.xqy
将上下文与转换结合起来吗?在 test.xqy
文件中,我可以访问 context .xml
直接使用诸如 doc('context')
之类的东西?
还有其他 XQuery 处理器提供此功能吗?
最佳答案
在不创建数据库的情况下访问文件
I don't want to use a database, since I need to create a database explicitly. Could I use basex -i context.xml test.xqy to combine the context with the transformation, and inside the test.xqy file, could I access the context.xml directly using something like doc('context')?
对于即席查询,最简单的方法是在 doc(...)
函数中传递文档。在 BaseX 中,doc(...)
函数可用于访问数据库、本地文件甚至远程 URI,如 Databases/XML Documents 中所述。 BaseX 文档的章节。如果该名称与数据库名称不匹配,BaseX 将尝试打开具有该名称的文件。您还可以提供 doc(...) 函数的相对或绝对路径。
实际上,这会创建文件的内存数据库表示形式(这对于合理的性能是必要的,文本 XML 并不真正适合快速评估查询),但这是透明发生的(但需要文档适合在主存储器中)。
例如,要计算存储在工作目录中的 context.xml
文件中的元素数量,请运行
count(doc('context.xml')//*)
更新文件
I need to transform some XML files[...]
这对于以这种方式打开的 XML 文件也是可能的,但请务必set the WRITEBACK
option ,也可以使用 -u
参数:
-u Write updates back to original files
关于xquery - 如何在不创建数据库的情况下访问 BaseX 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822675/