0

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.

Community
  • 1
  • 1
Roberto
  • 526
  • 2
  • 10

2 Answers2

1

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.

Roberto
  • 526
  • 2
  • 10
0

You haven't said which version of Saxon you are using. There are unit tests for 9.5 that demonstrate successful operation with a StaxSource - I have just checked. It's a bit tricky internally because StaxSource isn't available in JDK 1.5 and the code has to run in 1.5, so it uses reflexion; but it should work.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Good point. I have been deploying to WSO2 ESB 4.8.1 so I didn't know offhand. I dug around and found a `Saxon9he.jar`. It's not clear what minor version but, seeing as it barfed when I tried using StAXSource, I'm going to hazard a guess and say it's not 9.5. What you say about JDK 1.5 is interesting though. I'm using JDK 1.7, and reverting isn't an option. The good news is the PullSource works just as well based on one of your responses on Nabble, so it's not a major issue. Thanks! – Roberto Apr 11 '14 at 10:19
  • You can find out what version you are using with -t on the command line, or do "java -cp saxon9he.jar net.sf.saxon.Version". – Michael Kay Apr 11 '14 at 14:54
  • Thanks Michael. I am using "SAXON 9.4.0.4 from Saxonica (build 062515)". – Roberto Apr 14 '14 at 09:02
  • Then I'd suggest moving forward. – Michael Kay Apr 14 '14 at 10:43