如何使用 Saxon XPath 处理器而无需在 Java 中进行编码
我想我可以制作一些 XSL 样式表,然后将其用作带有参数选项的模板,以在命令行上使用 Saxon XSLT 处理器评估 XPath 表达式,例如:
<xsl:template match="/">
<xsl:copy-of select="saxon:evaluate($xpath-param)"/>
</xsl:template>
另外一种可能性是使用他们的 Java API: http://www.saxonica.com/documentation/xpath-api/intro.xml 但我不我不知道Java
有没有办法让 Saxon 从命令行计算 XPath 表达式?
如果可能的话,Shell 脚本也足够了
更新:
浏览 Saxon 文档,我发现了关于 XPathExample
示例。不幸的是我无法使用它
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以从命令行运行 Saxon (XQuery)。您可以通过使用
-q
指向具有 XPath/XQuery 的文件来完成此操作,也可以使用-qs
直接传递查询字符串。下面是使用
-qs
处理简单 XPath 的示例:input.xml
Saxon 命令行(我使用 Saxon9-HE 进行测试)
results.xml
注意:我可以通过将
-qs
更改为如下形式来使输出格式良好:-qs:"{/a/b[@id='x']} "
。有关更多命令行选项,请查看此处:< a href="http://www.saxonica.com/html/documentation/using-xquery/commandline.html" rel="noreferrer">http://www.saxonica.com/html/documentation/using-xquery/commandline.html
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
Saxon command line (I used Saxon9-HE to test with)
results.xml
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
另一种选择是在工具中使用 XPath,例如 oXygen。它的 XPath Builder View 是一个非常方便的界面,用于构建和测试 XPath 表达式。该工具有一个 Linux 版本,并且具有内置支持对于 Saxon 和其他处理器(Xerces、 LIBXML、XSV、MSXML4.0、MSXML .NET 和 SQC。)。
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.).
正如 @DanielHaley 所说,从命令行使用 XQuery 是更好的选择。从命令行提供 XPath 并不是很有用,因为 XPath 无法提供控制输出格式的方法。 XPath 是 XQuery 的子集,因此您可以选择使用 XQuery 接口来计算 XPath 表达式。
请注意,开源 Saxon 的当前版本是 Saxon-HE 9.4.0.2。您可以使用命令行上的 -t 选项找出您正在使用的版本。听起来好像您可能发现了与您的 Linux 发行版捆绑在一起的旧版本 (Saxon-B),并且 @prunge 向您指出了一个甚至更旧的版本 (Saxon 6.5),它仅支持 XSLT 1.0 和 XPath 1.0。
更新:截至 2019 年 1 月,Saxon 当前版本为 9.9.0.2
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