0

I was trying to convert one XML to another using XSLT 2.0, normally it works, but when file size is large like 1-2 MB I am getting error like java.util.concurrent.CompletionException: java.lang.StackOverflowError

Below Java code is using for the transformation

public static String doTransformation(String request, File xslTransformer)
        throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer(new StreamSource(xslTransformer));
    trans.setParameter("indent-elements", "yes");
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    InputStream inputStream = new ByteArrayInputStream(request.getBytes());
    trans.transform(new StreamSource(inputStream), new StreamResult(outputStream));
    return new String(outputStream.toByteArray());
}

Library used

<dependency>
      <groupId>xalan</groupId>
        <artifactId>xalan</artifactId>
     <version>2.7.1</version>
</dependency>

Any help would be appreciated.

2
  • 1
    Does the larger XML have more levels of nesting and does the XSLT have recursively called templates? Xalan has a command line interface as well I think so perhaps try running the samples from the command line first to check whether you can get an error message indicating which code/line in the XSLT causes the stack overflow. Commented Nov 2, 2020 at 14:55
  • Used string tokenize instead of split, which resolves the issue. Thank you. Commented Nov 3, 2020 at 13:30

2 Answers 2

1

First, and probably irrelevant, but if you're using Xalan then you're not using XSLT 2.0.

The Java code you've shown is irrelevant, it's the XSLT code that matters.

A Stack Overflow exception is going to occur if your code uses recursion and the recursion goes too deep. The depth of recursion is quite likely to depend on the data volumetrics.

You need to find where this recursion occurs in the XSLT code (it's probably a named template that calls itself), and then understand the logic to find a different way of doing it. It may be doing something very simple like a text replacement that could be coded a different way, especially if you are able to move to XSLT 2.0+.

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

2 Comments

Thank you Michael, you were correct, there was logic to split long string, this I have changed to str:tokenize which resolved the issue.
Please mark the answer as accepted (click the tick/check-mark) so people know the question has been answered.
1

StackOverflowError is not necessarily sign of an infinite recursion. It only says that the program while executing has ran out of memory for stack entries. That seems to confirm with your observations that smaller data files work, but larger don't.

What you need is to keep the program as is, but change the execution environment of it. Namely the stack size arguments to JVM. So that the program is given more memory for (each) stack while it's executing.

TLDR: Try java -xss 2048k $yourArguments or other values.

1 Comment

Used string tokenize instead of split, which resolves the issue. Thank you.

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.