3
votes

I'm trying to find a simple cli command to do xquery from linux shell. What I'd like is to avoid external dependencies that I need to install (using fedora 18).

What I already found is saxon and xqilla that I can install through yum. I can use them if nothing else but they are not in the base system. What I hoped to find is something like the java internal class for doing xslt processing (com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform).

One other idea I have is use XSLT to transform the document to the desired xquery result but one-liners would not be possible.

example of what I can do with xqilla (but searching for another utility):

$ xqilla -i TEST-com.ecample.testcase.MyTestCase.xml <(echo '/testsuite/@failures + /testsuite/@errors')

UPDATE: not exactly the question I had but I settled on perl XML::Twig being installed by default and giving me the flexibility to implement complicated logic with perl. That also led to a nice speed boost because there is no need to fork a process for each XML file.

4
do you know about xmlstarlet? Good luck.shellter
Have you looked at rolling your own, utilizing javax.xml.XPath?rmlan
Good question. xqilla is the best I've found that's open-source and not Java. It seems not very well-known either. Outside Java it might be the only lib with any Xpath2 support.ormaaj
@shellter, one more solution, thanks. Good also for other tasks related to XML in shell. But not in base distribution, neither java. If you file as a separate answer, I'll give +1 because it is useful.akostadinov

4 Answers

3
votes

For XQuery 1 there is my Xidel. Works like this:

 $ xidel TEST-com.ecample.testcase.MyTestCase.xml -e 'xquery version "1.0"; /testsuite/@failures + /testsuite/@errors'

Completely dependency free, but not in the base system, not java, and it has no support for XML schemas...

3
votes

Edit: I just noticed that you said you already found Saxon. I'll leave my answer anyway just in case the example helps anyone in the future.


You can run Saxon (XQuery) from the command line. You can do this by pointing to a file that has the XPath/XQuery using -q or you can pass the query string directly using -qs.

Here's an example of using -qs to process a simple XPath:

input.xml

<a>
  <b id="x"/>
  <b id="z"/>
  <b id="x"/>
</a>

Saxon command line (I used Saxon9-HE to test with)

java -cp "saxon9he.jar" net.sf.saxon.Query -s:"input.xml" -qs:"/a/b[@id='x']" -o:"results.xml"

results.xml

<b id="x"/><b id="x"/>

Note: I could've made my output well-formed by changing the -qs to something like this:

-qs:"<results>{/a/b[@id='x']}</results>"

For more command line options, look here: http://www.saxonica.com/html/documentation/using-xquery/commandline.html

1
votes

There is no XQuery processor bundled in the JDK, so you will have to install one separately. It's not clear why you see that as a problem.

1
votes

Unfortunately (or if you don't appreciate XML, Fortunately), XML support if very sporadic in Unix and Linux systems.

You can install the well regarded XML Starlet system

Otherwise, it always helps to search for xmllint. But note that different OS-based versions seem to have different feature sets, some of which go way beyond linting XML. But, it's not always installed to be visible in the basic PATH, so a

 find / -name 'xmllint*' -ls 2>/dev/null  > xmllintList.txt &

may, after a possible hour+ search uncover a whole directory of xml tools you didn't know you have!

IHTH.