开发者

XSLT help running in java

开发者 https://www.devze.com 2023-01-22 04:30 出处:网络
Im new to the XSLT world, I am basically trying to run the JSON convertion from HERE However if I use this method:

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', '(', '$', 'input', ',', 'false', '(', ')', ')', 'else', 'http://json.org/', ':', 'create-simple-node', '(', '$', '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:

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


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号