Project

Profile

Help

Simple XSLT streaming

Added by pinto pinto over 4 years ago

Hi

Trying to transform using streaming.

This code:

com.saxonica.config.StreamingTransformerFactory tFactory = (StreamingTransformerFactory) StreamingTransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheetFile));

transformer.transform(new StreamSource(xmlFile), new StreamResult(outputCsvFile));

gives: java.lang.ClassCastException: com.saxonica.config.EnterpriseTransformerFactory cannot be cast to com.saxonica.config.StreamingTransformerFactory

Changing to:

com.saxonica.config.EnterpriseTransformerFactory tFactory = (EnterpriseTransformerFactory) StreamingTransformerFactory.newInstance();

gives: The unnamed mode is streamable, but the input is not supplied as a stream

This code:

net.sf.saxon.s9api.Processor processor = new net.sf.saxon.s9api.Processor(true);

processor.setConfigurationProperty(Feature.STREAMABILITY, "standard");

XsltCompiler compiler = processor.newXsltCompiler();

XsltExecutable stylesheet = compiler.compile(new StreamSource(stylesheetFile));

Serializer out = processor.newSerializer(outputCsvFile);

Xslt30Transformer transformer = stylesheet.load30();

transformer.transform(new StreamSource(xmlFile), out);

gives: net.sf.saxon.s9api.SaxonApiException: Cannot use the transform() method when the initial mode is streamable

The XSLT is currently practcly empty, containg mainly the <xsl:mode streamable="yes" /> line.

Are there code samples for ding this correctly?


Replies (2)

RE: Simple XSLT streaming - Added by Michael Kay over 4 years ago

For your first two examples, please replace StreamingTransformerFactory.newInstance() by new StreamingTransformerFactory(). The class StreamingTransformerFactory can't be loaded using the standard JAXP mechanisms; this is deliberate, because it doesn't conform to the JAXP TransformerFactory contract (it doesn't set the global context item).

In your last example, please change transformer.transform(new StreamSource(xmlFile), out); to transformer.applyTemplates(new StreamSource(xmlFile), out). The Javadoc for the transform() method explains why.

You might find it useful to look at the various examples for both JAXP and s9api in the saxon-resources download.

RE: Simple XSLT streaming - Added by pinto pinto over 4 years ago

Thanks. I used your last example fix. Although it works, seems it's not streaming. The java.exe process memory is increasing in size relative to xml size.

This is the XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                          xmlns:fo="http://www.w3.org/1999/XSL/Format"

						  xmlns:bb="xx-zz-1.1"

						  xmlns:aa="urn:xx-yy-1.1">

<xsl:mode streamable="yes"/>

<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>

<xsl:template match="/">

<xsl:for-each select="aa:LevelOne/aa:LevelTwo">

	<xsl:iterate select="bb:LevelThree! copy-of(.)">

	<xsl:value-of select="concat(bb:fieldOne,',',bb:fieldTwo')"/>

	</xsl:iterate>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

The program emits no warnings.

    (1-2/2)

    Please register to reply