在命令行上运行XQuery

8

听起来我需要一种XML处理器

看起来有一个叫做Saxon的程序可以用来运行XQuery

当我尝试这个网站的例子时,出现了以下结果

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

我不知道如何从我下载的Saxon包中设置Java包。

我想知道是否有比这个Java包更容易的选项来能够从命令行运行XQuery?我想做的只是使用像“Select where type="something"”这样的命令处理一个XML文件。


请查看Saxon文档的入门部分:http://www.saxonica.com/html/documentation/about/gettingstarted/gettingstartedjava.html。 - chrisis
1
如果你稍微整理一下你的问题,鉴于下面所描述的项目的多样性,这实际上可能是一个非常有用的问题。我建议添加生成错误的精确命令,并可能改进您想要使用的伪命令。 - Joe Wicentowski
1
标题不能复制。仅凭标题,我完全同意@joewiz的观点。 - Thufir
java -cp Saxon-HE.jar net.sf.saxon.Query {1} - klaus
4个回答

13

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

12

我的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']"

使用 Xidel 时,-e 缩写为 --xpath--xquery,取决于后面的参数(如果您不想创建新的 XML 文档,则 XPath 基本上与 XQuery 相同)。而如果您需要 "' 取决于您是从 bash 还是 cmd 控制台调用它。


太棒了,Xidel!我无意中发现了这篇文章,查看了你的链接,它非常完美。谢谢! - Robert Geifman
很遗憾 xidel 不支持 XQuery update :/ BaseX 则可以直接支持(我日常使用 xidel,但在这种情况下,它并不是最好的工具)。请参考 https://unix.stackexchange.com/a/561018/12574 和 https://dev59.com/fLD2pogBymzxlkE8S_0sxidel-remove-nodes-with-xquery-in-a-shell - Gilles Quénot

4

听起来你不熟悉如何从命令行运行Java应用程序。有两种方法:学习如何做,或者找到一种不使用命令行运行XQuery的方法来避免问题。让我们尝试帮助你解决这两个问题,然后你可以选择。

(A) 使用命令行

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

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

你不知怎么找到了这个非常旧的页面。
一旦你完成前四个步骤,你就可以开始了。你已经完成了第一个步骤(安装Java),我们知道这一点,因为否则你不会看到这个错误信息。你告诉我们你已经完成了第二个步骤(下载软件)。目前还不清楚你是否完成了第三个步骤(解压缩)。而你显然正在尝试第四个步骤——从命令行运行XQuery,但是它失败了。这个消息告诉我们它失败了,因为Java正在运行,但是找不到classpath上的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。它非常适合做简单的事情,但我认为它已经有一段时间没有更新了。


一些非官方提示,可简化在bash / macOS中使用Saxon执行XQuery的命令行操作:https://gist.github.com/joewiz/f44a29c61ae23b16b478dfabe0fefbac。 - Joe Wicentowski
我在这里将你的链接添加到了代码片段中。 - Joe Wicentowski

1

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

请参阅使用命令行客户端执行查询,这些内容来自于eXist文档

3
我担心一个在安装和运行Saxon上遇到麻烦的用户,安装和运行eXist时可能会遇到更大的问题,尽管它是一款优秀的产品... - Michael Kay
我不太确定 - 新手 eXist 用户不必考虑类路径,因为包含的 shell 脚本(在这种情况下是 bin/client.sh)会处理它。我在您的帖子评论中链接的要点中的 saxonxq 命令为 Saxon 添加了这种新手友好的简单性。 - Joe Wicentowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接