Project

Profile

Help

java.lang.ClassCastException: net.sf.saxon.jaxp.StreamingTransformerImpl cannot be cast to net.sf.saxon.jaxp.TransformerImpl when trying to use newXMLFilter() with StreamingTransformerFactory

Added by Martin Honnen almost 7 years ago

Based on https://stackoverflow.com/questions/44969384/how-to-remove-xml-namesapce-with-sax-parser/44969723?noredirect=1#comment76932345_44969723 I tried to "implement" a SAX XMLFilter as an XSLT 3.0 stylesheet and use it in a processing chain in a pure streaming way, however, I get an exception

java.lang.ClassCastException: net.sf.saxon.jaxp.StreamingTransformerImpl cannot be cast to net.sf.saxon.jaxp.TransformerImpl
	at net.sf.saxon.jaxp.SaxonTransformerFactory.newXMLFilter(SaxonTransformerFactory.java:467)
	at net.sf.saxon.jaxp.SaxonTransformerFactory.newXMLFilter(SaxonTransformerFactory.java:449)

The Java method is


     public static void main(String[] args) throws TransformerConfigurationException, TransformerException, ParserConfigurationException, SAXException {

        SAXParserFactory saxFact = SAXParserFactory.newInstance();
        saxFact.setNamespaceAware(true);
        XMLReader reader = saxFact.newSAXParser().getXMLReader();
        
        TransformerFactory transFact = new StreamingTransformerFactory();
        
        XMLFilter removeNamespaces = ((SAXTransformerFactory)transFact).newXMLFilter(new StreamSource("remove-namespaces.xsl"));
        removeNamespaces.setParent(reader);
        
        Transformer transformer = transFact.newTransformer(new StreamSource("upper-case-names.xsl"));
        
        transformer.transform(new SAXSource(removeNamespaces, new InputSource("input-namespaces1.xml")), new StreamResult(System.out));
        System.out.println();    
        
    }

the exception happens on the @XMLFilter removeNamespaces = ((SAXTransformerFactory)transFact).newXMLFilter(new StreamSource("remove-namespaces.xsl"));@ line.

When I use the same code with an @EnterpriseTransformerFactory@ instead (@TransformerFactory transFact = new EnterpriseTransformerFactory();@), it works fine and does what I want it to do as far as chaining the stylesheets, only indicating with warnings "is streamable, but the input is not supplied as a stream" that there is no streaming happening.

Is there any way to use a @StreamingTransformerFactory()@ to create an XMLFilter for streamed processing?


    (1-2/2)

    Please register to reply