运行 命令行上的 XQuery

Running XQuery on the command line

听起来我需要某种类型的 XML 处理器

看起来有一个名为 Saxon 的程序,我可以用它来 运行 XQuery

当我尝试 this websites 示例时,我得到

Error: Could not find or load main class net.sf.saxon.Query

我不知道如何设置我下载的 saxon package 中的 java 包。

我想知道除了这个 java 包之外,是否还有更简单的选项能够从命令行 运行 XQuery?我想要做的就是使用 "Select where type="something"" 等命令处理 xml 文件。

听起来您不熟悉命令行中的 运行 Java 应用程序。有两种前进的方法:学习如何去做,或者通过找到 运行 XQuery 而不使用命令行的方法来避免问题。让我们尝试在这两个方面帮助您,您可以选择。

(A) 使用命令行

这里有关于安装和运行 Saxon 的基本信息:

http://www.saxonica.com/documentation/index.html#!about/gettingstarted/gettingstartedjava

您以某种方式找到了此页面的一个非常旧的版本。

完成前 4 个步骤后,您就开始做生意了。您已完成第 (1) 步(安装 Java)- 我们知道这一点,否则您不会收到此错误消息。您告诉我们您已完成第 (2) 步(下载软件)。不清楚您是否执行了第 (3) 步(解压缩)。您显然是在从命令行尝试步骤 (4) - 运行 XQuery,但它失败了。该消息告诉我们它失败了​​,因为 Java 是 运行,但在类路径上找不到 Saxon(这是 Java 寻找 Saxon 入口点的地方,net.sf.saxon.Query

如果这对您没有帮助,请准确告诉我们您做了什么:在何处找到了哪些文件,在命令行中输入了哪些内容,以及收到了哪些消息。

完成该阶段后,

中提供了有关 XQuery 命令行的更多信息

http://www.saxonica.com/documentation/index.html#!using-xquery/commandline

(B) 避免命令行的工具

Saxon 本身不为 运行 XSLT 或 XQuery 提供任何图形用户界面,但有很多工具可以提供,包括商业和开源工具,其中大多数都以 Saxon 作为基础 XSLT/XQuery 引擎(或至少提供 Saxon 作为选项)。他们可能并不总是支持最新版本,但最初这可能不是问题。

这些工具中最好的是商业软件:与 Saxonica 密切合作以集成产品(例如通过添加调试功能)的供应商提供的两种产品是 oXygen 和 Stylus Studio。功能较少但更便宜的产品是 Editix。其中大部分可能有免费的评估许可证,因此请尝试一下。

据我所知,唯一的 Saxon 开源 GUI 是 Andrew Welch 的 KernowforSaxon。用来做简单的事情很棒,但我想它已经有一段时间没有更新了。

BaseX 提供 XQuery 3.0 支持,并与封装调用 Java 的脚本打包在一起,包括设置类路径:

$ basex --help
BaseX 8.5.3 [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

eXist 提供 XQuery 3.1 支持,并与用于执行查询和从命令行访问数据库的脚本打包在一起:

$ bin/client.sh -s

Using locale: en_US.UTF-8
eXist version 3.3.0-SNAPSHOT (00c8bb256), Copyright (C) 2001-2017 The eXist-db Project
eXist-db comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; for details read the license file.

Connecting to database...
Connected :-)

type help or ? for help.

exist:/db>help
--- general commands ---
ls                   list collection contents
cd [collection|..]   change current collection
put [file pattern]   upload file or directory to the database
putgz [file pattern] upload possibly gzip compressed file or directory to the database
putzip [file pattern] upload the contents of a ZIP archive to the database
edit [resource] open the resource for editing
mkcol collection     create new sub-collection in current collection
rm document          remove document from current collection
rmcol collection     remove collection
set [key=value]      set property. Calling set without 
                     argument shows current settings.

--- search commands ---
find xpath-expr      execute the given XPath expression.
show [position]      display query result value at position.

--- user management (may require dba rights) ---
users                list existing users.
adduser username     create a new user.
passwd username      change password for user. 
chown user group [resource]
                     change resource ownership. chown without
                     resource changes ownership of the current
                     collection.
chmod [resource] permissions
                     change resource permissions. Format:
                     [user|group|other]=[+|-][read|write|execute].
                     chmod without resource changes permissions for
                     the current collection.
lock resource        put a write lock on the specified resource.
unlock resource      remove a write lock from the specified resource.
svn                  subversion command-line client.
threads              threads debug information.
quit                 quit the program

另见 Using the Command-line Client and Executing Queries from the eXist documentation

我的 Xidel 是一个命令行 XQuery 3.0 工具。

 xidel your-file.xml --xquery 'your-xquery'

但是 "select where" 不是 XQuery。 XQuery 看起来像 for ... in .. where .. return//*[..where..],例如

xidel your-file.xml -e "//*[@type = 'something']"

With Xidel -e--xpath--xquery 的缩写,具体取决于以下参数(如果您不尝试创建新的 [,XPath 与 XQuery 几乎相同=28=] 文档),如果你需要 "' 取决于你是从 bash 还是 cmd 控制台调用它