Running XQuery on the command line
Asked Answered
C

4

8

It sounds like I need some type of XML processor

It looks like theres a program called Saxon which I can use to run XQuery

When I try this websites example I get

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

I don't know how to set up the java package from the saxon package I downloaded.

Id like to know if there is an easier option to be able to run XQuery from the command line other than this java package? All I want to do is process an xml file with commands like "Select where type="something"".

Carhop answered 6/6, 2017 at 0:48 Comment(4)
See the getting started section of the Saxon documenation saxonica.com/html/documentation/about/gettingstarted/… .Updo
If you clean up your question a bit, this could actually be a very useful question, given the diversity of the projects described below. I'd suggest adding the precise command you ran that generated the error, and maybe improve the pseudo command you'd like to be able to use.Endemic
The title cannot be replicated. On the basis of the title alone I cannot agree more with @joewiz.Libyan
java -cp Saxon-HE.jar net.sf.saxon.Query {1}Clovis
N
13

My Xidel is a command-line XQuery 3.0 tool.

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

But "select where" is not XQuery. XQuery looks like for ... in .. where .. return or //*[..where..], e.g.

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

With Xidel -e is short for --xpath or --xquery depending on the following argument (XPath is pretty much the same as XQuery if you are not trying to create a new XML document), and if you need " or ' depends if you call it from the bash or cmd console

Nominative answered 6/6, 2017 at 17:25 Comment(2)
Great tool, Xidel! I accidantally stumbled on this post, checked your link out and it's perfect. Thanks!Crazyweed
Sad xidel doesn't support XQuery update :/ BaseX support it out of the box (I use xidel on a daily basis, but in this case, it's not the best tool), see unix.stackexchange.com/a/561018/12574 and #76086792Anathema
P
13

BaseX offers XQuery 3.0 support and is packaged with scripts that encapsulate calling Java, including setting up the classpath:

$ 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
Pisano answered 6/6, 2017 at 16:42 Comment(0)
N
13

My Xidel is a command-line XQuery 3.0 tool.

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

But "select where" is not XQuery. XQuery looks like for ... in .. where .. return or //*[..where..], e.g.

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

With Xidel -e is short for --xpath or --xquery depending on the following argument (XPath is pretty much the same as XQuery if you are not trying to create a new XML document), and if you need " or ' depends if you call it from the bash or cmd console

Nominative answered 6/6, 2017 at 17:25 Comment(2)
Great tool, Xidel! I accidantally stumbled on this post, checked your link out and it's perfect. Thanks!Crazyweed
Sad xidel doesn't support XQuery update :/ BaseX support it out of the box (I use xidel on a daily basis, but in this case, it's not the best tool), see unix.stackexchange.com/a/561018/12574 and #76086792Anathema
H
4

It sounds like you're not familiar with running Java applications from the command line. There are two ways forward: learn how to do it, or avoid the problem by finding a way of running XQuery without use of the command line. Let's try to help you with both, and you can choose.

(A) Using the command line

There's basic information on installing and runnning Saxon here:

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

You somehow found a very old version of this page.

Once you've got through the first 4 steps, you're in business. You've done step (1) (Installing Java) - we know that, because otherwise you wouldn't get this error message. You tell us you've done step (2) (downloading the software). It's not clear whether you did step (3) (unzipping it). And you're clearly attempting step (4) - running XQuery from the command line, but it's failing. The message tells us that it's failing because Java is running, but can't find Saxon on the classpath (which is the place where Java looks to find the Saxon entry point, net.sf.saxon.Query

If that doesn't help you, tell us exactly what you did: exactly what files are found where, exactly what you typed on the command line, and exactly what messages you got.

When you've got past that stage, there's more information about the XQuery command line at

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

(B) Tools that avoid the command line

Saxon doesn't itself provide any graphical user interfaces for running XSLT or XQuery, but there are plenty of tools that do, both commercial and open source, and most of them have Saxon as the underlying XSLT/XQuery engine (or at least offer Saxon as an option). They may not always support the latest version, but initially that's probably not a concern.

The best of these tools are commercial software: two products from vendors that work closely with Saxonica to integrate the product (e.g. by adding debugging capability) are oXygen and Stylus Studio. A cheaper product with less capability is Editix. Most of these probably have free evaluation licenses, so try them out.

The only open source GUI for Saxon that I know of is KernowforSaxon from Andrew Welch. It's great for doing simple things but I think it hasn't been updated for a while.

Hogtie answered 6/6, 2017 at 8:36 Comment(2)
Some unofficial hints for simplifying command line execution of XQuery with Saxon in bash / macOS: gist.github.com/joewiz/f44a29c61ae23b16b478dfabe0fefbac.Endemic
I added your links here to the gist.Endemic
E
1

eXist offers XQuery 3.1 support and is packaged with scripts for executing queries and accessing the database from the command line:

$ 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

See also Using the Command-line Client and Executing Queries from the eXist documentation.

Endemic answered 6/6, 2017 at 17:22 Comment(2)
I fear that a user who has trouble installing and running Saxon is going to have rather more trouble installing and running eXist, fine product though it is...Hogtie
I'm not so sure - newbie eXist users don't have to consider the classpath, since the included shell scripts (bin/client.sh in this case) handle that. The saxonxq command in the gist I linked to in the comment on your post adds this newbie-friendly simplicity to Saxon.Endemic

© 2022 - 2024 — McMap. All rights reserved.