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

后端 未结 3 1054
时光说笑
时光说笑 2021-01-14 08:52

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:

相关标签:
3条回答
  • 2021-01-14 09:29

    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.).

    enter image description here

    0 讨论(0)
  • 2021-01-14 09:29

    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

    0 讨论(0)
  • 2021-01-14 09:35

    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

    0 讨论(0)
提交回复
热议问题