我的转换代码片段如下:
EnterpriseConfiguration config = new EnterpriseConfiguration();
StreamResult xmlOutput = new StreamResult(new StringWriter());
Source xmlInput = new StreamSource(new StringReader(sourceMsg));
EnterpriseTransformerFactory factory = new EnterpriseTransformerFactory();
Transformer transformer = factory.newTransformer(new StreamSource(new File(xsltPath)));
System.out.println("config.isLicenseFound() " + config.isLicenseFound());
transformer.transform(xmlInput,xmlOutput);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
System.out.println("OUTPUT :::-> " + xmlOutput.getWriter().toString());
在我使用javax.xml.transform.Transformer
类的地方,我想知道是否有特定于Saxon的变压器?如果可以的话可以给我建议,提前谢谢。
这是我试图实现该功能的代码:
static HashMap<String, String> mapValues = new HashMap<String, String>();
public static HashMap<String, String> getMapValues() {
return mapValues;
}
public static void setMapValues(HashMap<String, String> mapValues) {
DecomposeXsl.mapValues = mapValues;
}
public String transformXsl() throws IOException, SaxonApiException
{
mapValues.put("John", "Jbl");
processor = new Processor(false);
xsltCompiler = processor.newXsltCompiler();
XPathCompiler xpathCompiler = new Processor(false).newXPathCompiler();
String pageFragment = null;
StreamSource xslStreamSource = new StreamSource(new File(xsltPath));
XsltExecutable xsltExecutable = xsltCompiler.compile(xslStreamSource);
xsltExecutable = xsltCompiler.compile(xslStreamSource);
pageXmlTransformer = xsltExecutable.load();
XsltTransformer transformer = null;
transformer = pageXmlTransformer;
String inputStr = null;
//StringReader inputStrReader = new StringReader(inputStr);
StreamSource xmlDoc = new StreamSource(new File(sourcePath));
Serializer serializer = new Serializer();
serializer.setOutputWriter(new StringWriter());
serializer.setOutputProperty(Serializer.Property.SAXON_STYLESHEET_VERSION, "2.0");
serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
serializer.setOutputProperty(Serializer.Property.MEDIA_TYPE, "text/html");
serializer.setOutputProperty(Serializer.Property.INDENT, "no");
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION, "yes");
serializer.setOutputProperty(Serializer.Property.ENCODING, "utf-8");
transformer.setSource(xmlDoc);
transformer.setDestination(serializer);
transformer.transform();
pageFragment = serializer.getOutputDestination().toString();
System.out.println("pageFragment "+pageFragment);
return pageFragment;
}
我得到的异常是:
XTDE1425: Cannot find a matching 2-argument function named {java:java.util.Map}get().
Reflexive calls to Java methods are not available under Saxon-HE
in built-in template rule
net.sf.saxon.s9api.SaxonApiException: Cannot find a matching 2-argument function named {java:java.util.Map}get(). Reflexive calls to Java methods are not available under Saxon-HE
这是我的 xsl 代码:
<xsl:param name="valMap" as="java:java.util.Map" required="no" select="controller:getMapValues()"/>
<newTag><xsl:value-of select="map:get($valMap, 'John')"/></newTag>
最佳答案
如果您告诉我们您需要知道的原因,那么回答会更容易。
您的问题的字面答案是肯定的,有一个特定于 Saxon 的类实现了 JAXP Transformer 接口(interface)。您可以通过显示 transformer.getClass().getName()
的值来了解它是什么。但通常不需要知道类名是什么,除非你想实现一些特殊效果。
在上面的代码中,创建 EnterpriseConfiguration
是多余的,因为当您创建 EnterpriseTransformerFactory
时,您无论如何都会得到一个。在工厂上调用 getConfiguration() 会更明智。如果您有两个不同的 Configuration 对象,那么当您在其中一个对象上设置属性并且它没有任何效果时,您可能会感到困惑。
对您的代码还有另一条评论:在进行转换后设置输出属性是没有意义的:到那时就太晚了。
关于java - 我们有撒克逊特定的 Transformer 类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40926172/