Project

Profile

Help

Support #4998

java sample for using complied xsl (sef) for transformation

Added by kantesh k 25 days ago. Updated 24 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Sprint/Milestone:
-
Start date:
2021-05-24
Due date:
% Done:

0%

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

Description

Hi,

I am trying to find sample java program to generate SEF file from xslt and apply the same for transformation, i could not find sample in resource files given. I used XsltExecutable.export and it generated file but if i use the same file for transformation i am getting empty content as output. I use xsl:include and document in xslt and i resolved them using URI resolver, also direct transformation without using compiled xslt is working fine i am using below code snippet, please let me know correct approach, i have Saxon ee (trial version).

1. generate SEF

*public static String getCompiledXslt(String xsl, Map<String, String> formatterMap) throws SaxonApiException, IOException {

	try(ByteArrayOutputStream destination = new ByteArrayOutputStream()){
	Processor processor = new Processor(true);
	XsltCompiler compiler = processor.newXsltCompiler();
	compiler.setURIResolver(new CigURIResolver(formatterMap));
	XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(xsl)));
	
	//FileOutputStream output=new FileOutputStream(new File("/Users/i845192/git/transformer-service/src/main/java/com/sap/cig/transformerservice/service/xslt/saxon.xsl"));
	stylesheet.export(destination);
	return destination.toString();
	}catch(RuntimeException ex) {
		throw ex;
	}
}*

2. use the same SEF for transformation:

*public static void transform(String standardXsl, Map<String, String> formatterMap, String sourceXMl) throws SaxonApiException {

    Processor processor = new Processor(true);
    XsltCompiler compiler = processor.newXsltCompiler();
    if (formatterMap != null) {
    	compiler.setURIResolver(new CigURIResolver(formatterMap));
	}
    
    
    XsltExecutable stylesheet = compiler.compile(new StreamSource(new StringReader(standardXsl)));
    Serializer out = processor.newSerializer(new File("out4.xml"));
    out.setOutputProperty(Serializer.Property.METHOD, "xml");
    out.setOutputProperty(Serializer.Property.INDENT, "yes");
     
    Xslt30Transformer trans = stylesheet.load30();
    if (formatterMap != null) {
    trans.setURIResolver(new CigURIResolver(formatterMap));
    }
    
    trans.transform(new StreamSource(new StringReader(sourceXMl)), out);

    System.out.println("Output written to out.xml");

}

Regards Kantesh

History

#1 Updated by kantesh k 25 days ago

update:

used below code to generate sef , now it is working fine, i have question like do we need to pass the URI resolver while compilation and while using the compiled xslt( sef)?, or am i doing wrong?. Is it recommended to compile the related XSLT as well?, in our case we including format xsl file and a lookup XML file.

Processor processor = new Processor(true); XsltCompiler compiler = processor.newXsltCompiler(); compiler.setURIResolver(new CigURIResolver(formatterMap)); //compiler.setJustInTimeCompilation(false); compiler.compilePackage(new StreamSource(new StringReader(xsl))).save(new File("saxon.sef"));*

#2 Updated by kantesh k 25 days ago

I need to store the compiled xslt in memory not in file, so i cannot use XsltCompiler.compilePackage, so is there any otherway we can get the compiled xslt(sef) to stream?

Please register to edit this issue

Also available in: Atom PDF