如何将参数传递给Saxon-B处理的XSL?

时间:2012-03-02 14:39:03

标签: java saxon

我正在使用Saxon-B 9.1.0.8和XSL 2.0。

我有一个XSL样式表,其中有一个xsl:param元素:

<xsl:param name="lang"/>

我正在使用以下代码来完成文档的转换:

try {
    Source xmlSource = new StreamSource(new File(xmlFilename));
    Source xsltSource = new StreamSource(new File(xslFilename));

    // create the transformer
    Processor processor = new Processor(false);
    XsltCompiler xsltCompiler = processor.newXsltCompiler();
    XsltExecutable xslt = xsltCompiler.compile(xsltSource);
    XsltTransformer xsltTransformer = xslt.load();
    xsltTransformer.setSource(xmlSource);

    // configure output
    StringWriter sw = new StringWriter();
    Serializer serializer = new Serializer();
    serializer.setOutputWriter(sw);
    serializer.setOutputProperty(Serializer.Property.INDENT, "yes");
    xsltTransformer.setDestination(serializer);

    // do it!
    xsltTransformer.transform();
} catch (SaxonApiException e) {
    logger.error(e.getMessage(), e);
}

如何将此参数的值传递给变压器?

1 个答案:

答案 0 :(得分:1)

Saxon使用QNameXdmAtomicValue类传递参数:

QName langParam = new QName("lang");
xsltTransformer.setParameter(langParam, new XdmAtomicValue("default"));