Java 我需要一个简单的命令行程序来使用 XSL 样式表转换 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2526681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:50:51  来源:igfitidea点击:

I need a simple command line program to transform XML using an XSL Stylesheet

javapythonxmlmacosxslt

提问by

I am on OSX Snow Leopard (10.6.2) I can install anything I need to. I would preferably like a Python or Java solution. I have searched on Google and found lots of information on writing my own program to do this, but this is a just a quick and dirty experiment so I don't want to invest a lot of time on writing a bunch of code to do this, I am sure someone else has done this already.

我使用的是 OSX Snow Leopard (10.6.2) 我可以安装我需要的任何东西。我更喜欢 Python 或 Java 解决方案。我在谷歌上搜索并找到了很多关于编写我自己的程序来做到这一点的信息,但这只是一个快速而肮脏的实验,所以我不想投入大量时间来编写一堆代码来做到这一点,我相信其他人已经这样做了。

This is off-topic now, do not use this question as an example of why your recommendations request is on topic, it is not. I apologize, but my Google-Foo was failing me the day I asked this 4 years ago!

现在这是题外话,不要使用这个问题作为为什么你的推荐请求是主题的例子,它不是。我道歉,但我的 Google-Foo 在我 4 年前问这个问题的那天让我失望了!

采纳答案by nbeyer

Have you tried 'xsltproc'? It's probably already installed.

你试过' xsltproc'吗?它可能已经安装了。

http://xmlsoft.org/XSLT/xsltproc2.html

http://xmlsoft.org/XSLT/xsltproc2.html

回答by nosklo

Use lxml. It is very easy to use and very fast. It can work with XPath and includes a XSLT transformer.

使用lxml. 它非常易于使用且速度非常快。它可以与 XPath 一起使用并包含一个XSLT 转换器

回答by Jon

If I assume correctly that you want to supply a stylesheet and an XML document then Xalan-J has a command line feature you can use to do this:

如果我正确地假设您想要提供样式表和 XML 文档,那么 Xalan-J 有一个命令行功能,您可以使用它来执行此操作:

http://xml.apache.org/xalan-j/commandline.html

http://xml.apache.org/xalan-j/commandline.html

which can be invoked with:

可以通过以下方式调用:

java org.apache.xalan.xslt.Process -IN foo.xml -XSL foo.xsl -OUT foo.out

回答by Brian Agnew

Have you seen XMLStarlet(an XML command line toolset) ? It does lots of XML-related querying transformation, and in particular:

您见过XMLStarlet(一个 XML 命令行工具集)吗?它做了很多与 XML 相关的查询转换,特别是:

xml tr --help
XMLStarlet Toolkit: Transform XML document(s) using XSLT
Usage: xml tr [<options>] <xsl-file> {-p|-s <name>=<value>} [ <xml-file-or-uri> ... ]
where
   <xsl-file>      - main XSLT stylesheet for transformation
   <xml-file>      - input XML document file name (stdin is used if missing)
   <name>=<value>  - name and value of the parameter passed to XSLT processor
   -p              - parameter is XPATH expression ("'string'" to quote string)
   -s              - parameter is a string literal

Further documentation here.

进一步的文档在这里

回答by Will McCutchen

I'd recommend Saxon, which can be run from the command line like so:

我推荐Saxon,它可以像这样从命令行运行:

java -jar /path/to/saxon.jar xmlfile xslfile

回答by Thorbj?rn Ravn Andersen

I have used Saxon 6.5 for years for command line transformations. (Java, XSLT 1)

我多年来一直使用 Saxon 6.5 进行命令行转换。(Java,XSLT 1)

An excellent fallback if a native solution is not available.

如果本机解决方案不可用,这是一个很好的后备方案。

回答by akostadinov

see this questionfor another solution with com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform. But I noticed some weirdness with namespaces compared to xalan processing. Be aware behavior might be non standard.

有关com.sun.org.apache.xalan.internal.xsltc.cmdline.Transform 的另一个解决方案,请参阅此问题。但是我注意到与 xalan 处理相比,命名空间有些奇怪。请注意行为可能是非标准的。

Also you know, internal java classes are not good to rely on.

你也知道,内部 java 类不好依赖。