Project

Profile

Help

Support #4998

closed

java sample for using complied xsl (sef) for transformation

Added by kantesh k almost 3 years ago. Updated about 2 years ago.

Status:
Closed
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:
Platforms:

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

Please register to edit this issue

Also available in: Atom PDF