Search code examples
javaxmlxsltstax

Read result of XSL Transformation with StAX in a streaming mode


My use case is:

  • XML File as input
  • Needs to be transformed with XSLT (using Java 8 build-in XSLT processor)
  • Process the Result with a XMLStreamReader (using Java 8 build-in StAX implementation)

I want to do this in a "streaming mode" (so not writing the output of the XSL Transformation to a File and than parsing it by the XMLStreamReader).

Is this possible? If so, how? I can only find SAX-based examples.


Solution

  • Probably not possible. Most XSLT engines write the result "tree" in push mode, so you can skip the creating of a physical tree by accepting events as they occur, but if you want to get the result in pull mode you're going to need to run the transformation in one thread and read the results in another thread, using something like a BlockingQueue to communicate the events from on thread to another. Saxon has internal mechanisms to handle push-pull conflicts using multiple threads in this way, but only at item level, not at event level, and this isn't much use to you because the whole result tree is one item. So the basic answer is, the only way I can see to do what you want is to write a multi-threaded SAX-to-StAX converter, which is a tricky bit of Java coding.