首页 > 解决方案 > Saxon-He 9.8 s9Api Transform 函数抛出:net.sf.saxon.trans.XPathException: Failed to read input file exception

问题描述

我试图使用 java SaxonHE9-8-0-12J s9api 执行 XSLT2.0 转换。我可以看到正在创建的 html 文件,但它抛出异常:“net.sf.saxon.trans.XPathException:无法读取输入文件”。

我想将最终转换后的 XML 作为字符串返回。

下面是我试图执行的代码:

public String runXSLT(String xsltFile, String strInputXML, boolean cacheEnabled,boolean traceEnabled) throws SaxonApiException, ValidityException, ParsingException {

        Processor proc = new Processor(false);
        XsltCompiler comp = proc.newXsltCompiler();
        XsltExecutable exp = comp.compile(new StreamSource(new File(xsltFile))); //compiled form of stylesheet
        StringReader strRdr = new StringReader(strInputXML);
        StreamSource strSrc = new StreamSource(strRdr);

        DocumentBuilder docBuild = proc.newDocumentBuilder();
        XdmNode source = docBuild.build(strSrc);
        XsltTransformer trans = exp.load();
        trans.setInitialContextNode(source);
        File f = new File("C://Data//transformedXML.html");
        Serializer out = proc.newSerializer();
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");
        out.setOutputFile(f);
        trans.setDestination(out);
        trans.transform();
        return trans.toString();

    }

请帮忙。提前致谢。

标签: javatransformationsaxon

解决方案


推荐阅读