XSLT帮助在java中运行

发布于 2024-09-28 17:30:22 字数 1185 浏览 10 评论 0原文

我是 XSLT 世界的新手,我基本上是尝试从 HERE 运行 JSON 转换

但是如果我使用这种方法:

TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource("src\\json\\xml-to-json.xsl"));
        transformer.transform(new StreamSource("src\\json\\xmltest.xml"), new StreamResult(new FileOutputStream("birds.out")));

        System.out.println(result);

我收到以下错误:

SystemId Unknown; Line #59; Column #127; Could not find function: if
SystemId Unknown; Line #59; Column #127; Extra illegal tokens: 'then', 'http://json.org/', ':', 'create-node', '(', '$', 'input', ',', 'false', '(', ')', ')', 'else', 'http://json.org/', ':', 'create-simple-node', '(', '$', 'input', ')'
SystemId Unknown; Line #59; Column #127; function token not found.

如果我使用 Saxon,由于许可,我宁愿使用 inbuit ,但只需调用它就可以工作的 main:

String[] args = new String[2];
args[0]="d:\\xmltest.xml";
args[1]="d:\\xml-to-json.xsl";
net.sf.saxon.Transform.main(args);

但我不确定如何正确编码(不调用 main)用Java编写,这样我就可以存储结果。

干杯

Im new to the XSLT world, I am basically trying to run the JSON convertion from HERE

However if I use this method:

TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer(new StreamSource("src\\json\\xml-to-json.xsl"));
        transformer.transform(new StreamSource("src\\json\\xmltest.xml"), new StreamResult(new FileOutputStream("birds.out")));

        System.out.println(result);

I get the following error:

SystemId Unknown; Line #59; Column #127; Could not find function: if
SystemId Unknown; Line #59; Column #127; Extra illegal tokens: 'then', 'http://json.org/', ':', 'create-node', '(', '

If I use Saxon, I would rather use the inbuit one due to licensing, but just call the main it works:

String[] args = new String[2];
args[0]="d:\\xmltest.xml";
args[1]="d:\\xml-to-json.xsl";
net.sf.saxon.Transform.main(args);

But im not sure how to actually code that correctly (not calling main) in Java so I can store the results.

Cheers

, 'input', ',', 'false', '(', ')', ')', 'else', 'http://json.org/', ':', 'create-simple-node', '(', '

If I use Saxon, I would rather use the inbuit one due to licensing, but just call the main it works:


But im not sure how to actually code that correctly (not calling main) in Java so I can store the results.

Cheers

, 'input', ')' SystemId Unknown; Line #59; Column #127; function token not found.

If I use Saxon, I would rather use the inbuit one due to licensing, but just call the main it works:

But im not sure how to actually code that correctly (not calling main) in Java so I can store the results.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情换悲伤 2024-10-05 17:30:22

这里有两个问题。 1)为什么第一次尝试会失败并出现错误? 2)如何从 Saxon 获得结果?

1) 第一次尝试失败,因为您使用的是 XSLT 1.0 处理器。正如您引用的页面所说,

XSLTJSON 是一个 XSLT 2.0 样式表
将任意 XML 转换为 JavaScript
对象表示法 (JSON)。 ...如果你这样做
没有 XSLT 2.0 处理器,您
可以使用 XSLTJSON Lite,这是一个
用于转换 XML 的 XSLT 1.0 样式表
转换为 JSONML 格式。

因此,如果您使用 XSLTJSON Lite,您的问题应该得到解决。

2) 如果您仍然想尝试 Saxon,此页面看起来有用的。它不是直接调用 Saxon,而是

 // set the TransformFactory to use the Saxon TransformerFactoryImpl method
 System.setProperty("javax.xml.transform.TransformerFactory",
                    "net.sf.saxon.TransformerFactoryImpl");

 TransformerFactory tfactory = TransformerFactory.newInstance();

There are two questions here. 1) Why is the first attempt failing with errors? and 2) how to get results from Saxon?

1) The first attempt fails because you're using an XSLT 1.0 processor. As the page you referenced says,

XSLTJSON is an XSLT 2.0 stylesheet to
transform arbitrary XML to JavaScript
Object Notation (JSON). ... If you do
not have an XSLT 2.0 processor, you
can use XSLTJSON Lite, which is an
XSLT 1.0 stylesheet to transforms XML
to the JSONML format.

So if you use XSLTJSON Lite, your problems should be solved.

2) If you still want to try Saxon, this page looks useful. Rather than calling Saxon directly, it uses

 // set the TransformFactory to use the Saxon TransformerFactoryImpl method
 System.setProperty("javax.xml.transform.TransformerFactory",
                    "net.sf.saxon.TransformerFactoryImpl");

before the

 TransformerFactory tfactory = TransformerFactory.newInstance();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文