0

I have the following code:

public static String converterXml(String json, XsltTransformer transformer) throws SaxonApiException {
    DocumentBuilder builder = processor.newDocumentBuilder();
    builder.setLineNumbering(true);
    builder.setDTDValidation(false);
    StringWriter writer = new StringWriter();
    Serializer out = processor.newSerializer(writer);
    out.setOutputProperty(Serializer.Property.INDENT, "yes");

    QName qname = new QName("json-input");
    XdmValue value = new XdmAtomicValue(json);
    transformer.setParameter(qname, value);

    transformer.setDestination(out);
    transformer.transform();

    return writer.toString();
}

By using Saxon HE v9.9 it doesn't work getting java.lang.NullPointerException:

Exception in thread "main" java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at net.sf.saxon.s9api.AbstractXsltTransformer.applyTemplatesToSource(AbstractXsltTransformer.java:336)
at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:338)

Instead of the class XdmAtomicValue I should use another class but I don't know which one.

UPDATED:

The complete code is:

Processor processor = new Processor(false);
XsltCompiler comp = processor.newXsltCompiler();
XsltTransformer transformer = comp.compile(new StreamSource(new File("/path/json-to-xml.xsl")));
String json = new String(Files.readAllBytes(Paths.get("path-json/invoice.json")));
String xml = converterXml(json, transformer);

I just need set up json as parameter, no one other file XML is necessary as an entry.

I'm using this solution to transform the JSON file(Martin's answer). By using Saxon HE 9.8 it works fine but using Saxon HE 9.9 it doesn't work.

1
  • You need to call transformer.setSource() to supply an input document. (The diagnostics could be better....) Commented Feb 11, 2019 at 22:37

1 Answer 1

0

INITIAL ANSWER

You need to call transformer.setSource() to supply an input document. (The diagnostics could be better....) –

REVISED ANSWER FOLLOWING DISCUSSION IN COMMENTS

It seems that the stylesheet is using <xsl:template name="xsl:initial-template"/> as the entry point for execution; although not documented in the Javadoc, it seems that in Saxon 9.8, XsltTransformer.transform() with no previous call on setSource() or setInitialTemplate() would successfully run a transformation starting at xsl:initial-template, whereas Saxon 9.9 does not.

I have raised an issue on this at https://saxonica.plan.io/issues/4137

Sign up to request clarification or add additional context in comments.

7 Comments

Could you explain me how to do it? That code works with Saxon HE 9.8
What is your transformation actually doing? The transform() method is defined to start at the initial named template if one is specified, otherwise to start by applying templates to the initial source. If you don't have an initial named template and you don't have an initial source, what are you expecting it to do? (There are plenty of examples of using the s9api interface in the saxon-resources download file at www.saxonica.com)
It's possible of course that the XsltTransformer is already initialized before calling the code you have shown us. Without a complete repro, there can be no complete answer...
I'm sorry, I don't explained well, I edited my question explaining better.
Showing the XSLT code (or a cut-down version to illustrate the issue) might help us to explain better.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.