Search code examples
xmlxsltsaxonstax

Which XSLT Processor supports both XSLT 2.0 and StAXSource?


I spent a good deal of time trying to find a TransformerFactory that would support StAXSource, because my input was an XMLStreamReader and StAXSource seemed like the best fit. Eventually I came across the following and it worked:

TransformerFactory.newInstance(
    "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl",
null);

Now I find myself trying to use grouping following this SO answer and had success using the xsl:for-each-group (pastebin). Then I found out that the above TransformerFactory did not support XSLT 2.0, and I can't work out how to apply Muenchian grouping (I wanted this input to be transformed into this output)

I want to be able to support XSLT 2.0, so I discarded the notion of struggling with Muenchian grouping. I found that SAXON supports XSLT 2.0

TransformerFactory factory = TransformerFactory.newInstance(
    "net.sf.saxon.TransformerFactoryImpl",
null);

Alas, it doesn't support StAXSource. Are there any XSLT Processors that support both XSLT 2.0 and StAXSource?

As an alternative, is there a way to transform a XMLStreamReader into something that SAXON can support (e.g. A StringReader for StreamSource)?

A last resort would be to figure out this Muenchian grouping and discount XSLT 2.0 altogether, but I don't like that idea.


Solution

  • As it turns out, not too long after I posed this question, I found the solution:

    SAXON is amazing, and actually does support reading from an XMLStreamReader, just not using StAXSource. I found a little thread that explains a lot about why StAXSource exists when it's not widely supported, and provided a workable alternative.

    So rather than using:

    Source source = new StAXSource(xmlStreamReader);
    

    I used the following:

    StaxBridge bridge = new StaxBridge();
    bridge.setXMLStreamReader(reader);
    Source source = new PullSource(bridge);
    

    And I have a XSLT Processor that can work with XMLStreamReader, supports both XSLT 1.0 and 2.0.