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

こ雲淡風輕ζ 提交于 2019-12-01 06:47:34

问题


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


回答1:


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




回答2:


Another option is to use XPath within a tool, such as oXygen. Its XPath Builder View is a very handy interface for building and testing XPath expressions. There is a Linux version of the tool, and it has built-in support for Saxon and other processors (Xerces, LIBXML, XSV, MSXML4.0, MSXML .NET and SQC.).




回答3:


As @DanielHaley says, using XQuery from the command line is a better bet. Providing XPath from the command line wouldn't be very useful because XPath offers no way to control the formatting of the output. XPath is a subset of XQuery, so you can use the XQuery interface to evaluate XPath expressions if you choose.

Note that the current version of open-source Saxon is Saxon-HE 9.4.0.2. You can find out which version you are using with the -t option on the command line. It sounds as if you might have found an old version (Saxon-B) bundled with your Linux distribution, and @prunge has pointed you to an even older version (Saxon 6.5) which only supports XSLT 1.0 and XPath 1.0.

UPDATE: As of January 2019, the current version of Saxon is 9.9.0.2



来源:https://stackoverflow.com/questions/8997709/how-to-use-saxon-xpath-processor-w-o-coding-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!