Project

Profile

Help

Support #3608

closed

Executing 2nd transformation on output from the first Transformation

Added by Ansh Sahajpal almost 7 years ago. Updated about 6 years ago.

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

0%

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

Description

I want to run 2 XSL transformation where the 2nd transformation should be streamable and will work on the output from the 1st transformation.

The whole scenario is like this:-

We perform a transformation and get some output. Now from that output we want to filter out all the blank elements(i.e. elements like , and) from the xml, so for now we are considering to run the output xml through another XSL transformation which will strip out all the empty elements from the output of first transfromation and give us the final output xml.

I am able to link 2 transfromations using a TransformerHandler. The approach i followed is simple i set the result of first transformation to TransformerHandler which then performs the second transformation and gives me the desired result. I am facing issues when trying to follow the same approach using a StreamingTransformerHandler, i am having trouble initializing the StreamingTransformerHandler, here's the approach i am following:

String xsl2="<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform\">\n" +

                            		"    <xsl:output omit-xml-declaration=\"yes\" indent=\"yes\"/>\n" + 

                            		"    <xsl:mode streamable=\"yes\" on-no-match=\"shallow-copy\"/>\n" + 

                            		"    <xsl:strip-space elements=\"*\"/>\n" + 

                            		"    <xsl:template match=\"*[not(@*)]\">\n" + 

                            		"      <xsl:where-populated>\n" + 

                            		"        <xsl:copy>\n" + 

                            		"            <xsl:apply-templates select=\"@*\"/>\n" + 

                            		"            <xsl:apply-templates select=\"node()\"/>\n" + 

                            		"        </xsl:copy>\n" + 

                            		"      </xsl:where-populated>  \n" + 

                            		"    </xsl:template>\n" + 

                            		"    <xsl:template match=\"text()[number(.) = -1]\"/>\n" + 

                            		"</xsl:stylesheet>";

                            BufferedInputStream xsl2Stream = new BufferedInputStream(new ByteArrayInputStream(xsl2.getBytes()));

                            StreamSource src2 = new StreamSource(xsl2Stream);

                            Templates removeEmptyXSLTemplates= _transformerFactory.newTemplates(src2);

                            StreamingTransformerFactory stf = new StreamingTransformerFactory();

// SAXTransformerFactory stf2 = (SAXTransformerFactory)TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);

                            StreamingTransformerHandler sth = (StreamingTransformerHandler) stf.newTransformerHandler(removeEmptyXSLTemplates);

                            sth.setResult(_result);

                            _transformer.transform(new StreamSource(getSourceStream()), new SAXResult(sth));

If i change the sth from StreamingTransformerHandler to TransformerHandler the code works fine. The newTransformerHandler returns a TransformerHandler i assumed that we should be able to cast it into a StreamingTransformerHandler and hence tried this approach but that's not the case and hence i got the following exception:

net.sf.saxon.jaxp.TransformerImpl cannot be cast to net.sf.saxon.jaxp.StreamingTransformerImpl

So i am not able to execute my 2nd transformation with streaming.

If there's anyway to make 2nd transformation streamable without converting the output of first transformation to input source OR if there's a way with which i can achieve my desired results without having a 2nd transformation that will be the best case for that i am attaching a sample input and my desired output.


Files

input.xml (392 Bytes) input.xml input Ansh Sahajpal, 2018-01-09 07:20
output.xml (151 Bytes) output.xml desired ouput Ansh Sahajpal, 2018-01-09 07:20
FilterEmptyNodes (1).xsl (569 Bytes) FilterEmptyNodes (1).xsl xsl for 2nd transformation which needs to be in streaming Ansh Sahajpal, 2018-01-09 07:21

Related issues

Copied from Saxon - Support #3605: Executing 2nd transformation on output from the first TransformationClosed2018-01-09

Actions
Actions #1

Updated by Ansh Sahajpal almost 7 years ago

  • Copied from Support #3605: Executing 2nd transformation on output from the first Transformation added
Actions #2

Updated by Martin Honnen almost 7 years ago

Why are you trying to mix different types of TransformerFactory? Simply use StreamingTransformerFactory to create your Transformer and your TransformerHandler and I think it will work, even if the first stylesheet doesn't use streaming.

Actions #3

Updated by Michael Kay almost 7 years ago

I'm not entirely sure why you're getting this particular exception but the first thing to establish is precisely which Saxon version you are using. There have been a number of changes; this mechanism was introduced incrementally in maintenance releases in response to bugs 3120, 3237, and 3357.

Actions #4

Updated by Ansh Sahajpal almost 7 years ago

@Michael I am using saxon EE 9.8.0.6 jar, let me know your findings if this can be an issue from saxon or if i am doing something wrong at my end and, in the meantime i will try the suggestion provided by @Martin and see if that works.

Actions #5

Updated by Ansh Sahajpal almost 7 years ago

@Michael I was able to get this to work by method given by @Martin.

However now i am facing another issue, the final output i am getting is not indented. My first transformation gives me an indented output and the output of second transformation is not indented even though i have mentioned the indent property in the xsl as yes. If i run the xsl for 2nd transformation alone i.e. without doing 1st transformation, the output is indented.

Is there any workaround for this so that when i run both transformation the final output comes out to be indented.

Actions #6

Updated by Michael Kay about 6 years ago

  • Description updated (diff)
  • Status changed from New to Closed

Although we didn't get to a final resolution of all the points raised here, I don't think there is any value in keeping this issue open any longer. If there are outstanding questions, please start a new thread.

Please register to edit this issue

Also available in: Atom PDF