xquery - 如何在不创建数据库的情况下访问 BaseX 中的文件?

标签 xquery basex

我需要根据 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/

相关文章:

XQuery:导入的变量是否延迟计算?

sql-server - 如何编写 XQuery 来访问存储在单独的 SQL Server 表中的 XML?

xquery - 使用 XQuery 进行递归的示例

emacs - 如何使用 emacs 在 Marklogic 数据库上运行 xquery 代码?

xquery - 使用Basex在XML中插入数据

xpath - XQuery 在 baseX 中返回错误

使用 XQuery 将 XML 转换为 CSV

xquery - 我怎样才能得到cts :values for elements with special attribute value

xquery - BaseX中如何记录任务开始时间和任务结束时间

javascript - BaseX:Javascript 函数在 XSLT 中不起作用