Skip to content

Instantly share code, notes, and snippets.

@joewiz
Last active April 7, 2022 16:50
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewiz/f44a29c61ae23b16b478dfabe0fefbac to your computer and use it in GitHub Desktop.
Save joewiz/f44a29c61ae23b16b478dfabe0fefbac to your computer and use it in GitHub Desktop.
Install saxon via homebrew for command line XQuery on macOS

Install saxon via homebrew to execute XQuery files on macOS

Note: The homebrew recipe for installing Saxon shown here was created and is maintained by the community. It is not Saxon's official method of installation. The official method can be found at http://www.saxonica.com/documentation/index.html#!about/gettingstarted/gettingstartedjava and http://www.saxonica.com/documentation/index.html#!using-xquery/commandline.

Install saxon

$ brew install saxon
$ brew info saxon
saxon: stable 9.7.0.4
XSLT and XQuery processor
http://saxon.sourceforge.net
/usr/local/Cellar/saxon/9.7.0.4 (14 files, 4.8M) *
  Built from source on 2016-04-14 at 09:40:59
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/saxon.rb

Create sample XQuery file

echo 'xquery version "3.1"; <message>hello world</message>' > hello-world.xq

Execute sample XQuery 3.1 file

$ java -cp /usr/local/Cellar/saxon/9.7.0.4/libexec/saxon9he.jar net.sf.saxon.Query -qversion:3.1 hello-world.xq
<?xml version="1.0" encoding="UTF-8"?><message>hello world</message>

Set up "saxonxq", a version-neutral alias for executing XQuery files

$ brew --prefix saxon
/usr/local/opt/saxon
$ alias saxonxq='java -cp "`brew --prefix saxon`/libexec/saxon9he.jar" net.sf.saxon.Query -qversion:3.1'
$ saxonxq
No query file name
Saxon-HE 9.7.0.4J from Saxonica
Usage: see http://www.saxonica.com/html/documentation/using-xquery/commandline.html
Format: net.sf.saxon.Query options params
Options available: -? -backup -catalog -config -cr -dtd -expand -explain -ext -init -l -mr -now -o -opt -outval -p -pipe -projection -q -qs -quit -qversion -r -repeat -s -sa -scmin -stream -strip -t -T -TJ -TP -traceout -tree -u -update -val -wrap -x -xi -xmlversion -xsd -xsdversion -xsiloc
Use -XYZ:? for details of option XYZ
Params: 
  param=value           Set query string parameter
  +param=filename       Set query document parameter
  ?param=expression     Set query parameter using XPath
  !param=value          Set serialization parameter
$ saxonxq hello-world.xq
<?xml version="1.0" encoding="UTF-8"?><message>hello world</message>

If you think you'll use the alias again, save it into your ~/.bash_profile.

@ottosmops
Copy link

Since Saxon 9.8 there is no option '-qversion' anymore: cf. https://www.saxonica.com/documentation9.8/index.html#!using-xquery

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment