Saxonica从XSLT生成SEF文件应用转换时export输出为空的解决方法
SEF文件内存存储及转换问题记录与解决方案
我正在寻找将SEF文件保存在内存中并用于转换的正确方案。
目前找到了两种生成SEF文件的方案:
- 使用
xsltpackage.save(File):运行正常,但该方法需要将内容写入文件,不符合存储到内存/数据库的需求 - 使用
XsltExecutable.export():可生成文件,但使用该.sef文件执行转换时,输出结果为空
我使用的XSLT中包含xsl:include和document语法,已通过URI resolver完成解析,所用依赖为Saxon ee(试用版),生成和转换的逻辑如下:
XsltExecutable.export() 实现代码
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))); stylesheet.export(destination); return destination.toString(); }catch(RuntimeException ex) { throw ex; } }
SEF文件执行转换代码
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");
使用上述export方法生成的SEF执行转换时输出为空,而XsltPackage.save()生成的SEF在相同代码下运行正常。
最终解决方案
问题已解决,只需将默认值为true的compiler.setJustInTimeCompilation(false);属性设置为false即可。
内容的提问来源于stack exchange,提问作者kantesh
相关产品推荐
相关产品推荐

