我们有特定的撒克逊变压器类吗?

时间:2016-12-02 06:31:49

标签: java xslt saxon

我的转化代码段如下:

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课程的地方,我想知道有没有特定于撒克逊的变形金刚?如果是,可以建议我,提前谢谢。

以下是我尝试实现该功能的代码:

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>

1 个答案:

答案 0 :(得分:1)

如果您告诉我们您需要了解的原因,那么回答会更容易。

您的问题的字面答案是肯定的,有一个特定于Saxon的类实现了JAXP Transformer接口。您可以通过显示transformer.getClass().getName()的值来了解它的含义。但是,除非你想达到一些特殊的效果,否则你通常不需要知道类名是什么。

在上面的代码中,创建EnterpriseConfiguration是多余的,因为在创建EnterpriseTransformerFactory时无论如何都会得到一个getConfiguration()。在工厂打电话String[]会更明智。如果您有两个不同的配置对象,当您在其中一个上设置属性并且它没有效果时,您可能会感到困惑。

还有一条关于你的代码的评论:在进行转换之后,没有必要设置输出属性:那时已经太晚了。