Project

Profile

Help

Bug #3120

closed

Streaming not working from JAXP Transformation API

Added by Alex Jitianu about 7 years ago. Updated almost 7 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2017-01-30
Due date:
% Done:

100%

Estimated time:
Legacy ID:
Applies to branch:
9.7
Fix Committed on Branch:
9.7, trunk
Fixed in Maintenance Release:
Platforms:

Description

Hi,

I'm trying to use Saxon through its API to execute a "streaming" transformation, like in this example:

import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import net.sf.saxon.TransformerFactoryImpl;

import org.xml.sax.InputSource;

public class Streaming {

  private static String xsl = 
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 
      "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"\n" + 
      "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"\n" + 
      "    exclude-result-prefixes=\"xs\"\n" + 
      "    version=\"3.0\">\n"
      + "<xsl:mode streamable=\"true\"/>\n" + 
      "    <xsl:template match=\"*:person\"><xsl:value-of select=\"@id\"/></xsl:template>\n" + 
      "</xsl:stylesheet>";
  private static String xml = "<person id='test'/>";

  public static void main(String[] args) throws TransformerException {
    TransformerFactory factory = TransformerFactoryImpl.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(xsl)));
    
    InputSource inputSource = new InputSource(new StringReader(xml));
    SAXSource xmlSource = new SAXSource(inputSource);
    StringWriter writer = new StringWriter();
    transformer.transform(xmlSource, new StreamResult(writer));
    
    System.out.println(writer.toString());
  }
}

The following warning appears in the console:

  Requested initial mode  is streamable, but the supplied input is not streamed

which is a hint that the transformation is not ran in "streaming mode". From what I can tell something strange occurs inside net.sf.saxon.s9api.XsltTransformer.transform() :

               if (initialSource instanceof NodeInfo) {
                    controller.setGlobalContextItem((NodeInfo)initialSource);
                } else if (initialSource instanceof DOMSource) {
                    NodeInfo node = controller.prepareInputTree(initialSource);
                    controller.setGlobalContextItem(node);
                    initialSource = node;
                } else {
                    boolean close = (initialSource instanceof AugmentedSource && ((AugmentedSource)initialSource).isPleaseCloseAfterUse());
                    NodeInfo node = controller.makeSourceTree(initialSource, close, getSchemaValidationMode().getNumber());
                    controller.setGlobalContextItem(node);
                    initialSource = node;
                }

When a SAXSource passes through this code it will be transformed into a NodeInfo and because of that, inside net.sf.saxon.Controller.transform(Source, Receiver) a decision is taken to go into net.sf.saxon.Controller.transformDocument() where that warning is generated. Thank you for taking the time to look into this.

Alex

Please register to edit this issue

Also available in: Atom PDF