How to use Saxon XPath processor w/o coding in Java

theta picture theta · Jan 25, 2012 · Viewed 7.2k times · Source

I guess I could make some XSL stylesheet, then use it as a template with parameter option to evaluate XPath expression with Saxon XSLT processor on command line, like:

<xsl:template match="/">
  <xsl:copy-of select="saxon:evaluate($xpath-param)"/>
</xsl:template>

Also other possibility is to use their Java API: http://www.saxonica.com/documentation/xpath-api/intro.xml but I don't know Java

Is there any way to make Saxon evaluate XPath expression from command line?
Shell script would be sufficient, too, if possible

Update:
Browsing Saxon documentation, I found out about XPathExample sample. Unfortunately I can't make use of it

Answer

Daniel Haley picture Daniel Haley · Jan 25, 2012

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