我使用saxon(HE 9.9.1-6)将XML转换为HTML文件。使用Saxon是因为XSLT是版本2,并且默认的Java类失败。
XSLT包含两个语句以复制其他文件的内容:

<xsl:value-of select="unparsed-text('file.ext')"/>


只要Xslt和这些文件位于同一目录中并且xslt作为文件源给出,此方法就可以正常工作

Source xslt = new StreamSource(new File("c:/somedir/file.xsl"));


但是我的xslt位于资源目​​录中(此后应该装在jar中)。如果我在那种情况下使用它,则saxon找不到包含的文件,因为它看起来在我项目的根目录中:

Source xslt = new StreamSource(getClass().getClassLoader().getResourceAsStream("file.xsl"));


结果是:

Error evaluating (fn:unparsed-text(...)) in xsl:value-of/@select on line 22 column 66
FOUT1170: Failed to read input file: <project root directory>\included_file.css (File not found)


有什么办法可以为Saxon提供需要包含的文件的其他StreamSources?我什么都找不到。

理想情况下,我想要这样的东西:

transformer.addInput(new StreamSource(getClass().getClassLoader().getResourceAsStream("inputfile.css")));


我想出的唯一解决方案很丑陋:将xslt及其所需的文件从资源复制到一个临时目录,然后使用该xslt作为源进行转换。



范例程式码

我没有编写XSLT的知识,所以我只能提供非最小的示例文件。
可以在here中找到xslt及其两个必需的文件(css和js)。您需要的是三个“ xrechnung”。直接链接:xrechnung-html.xslxrechnung-viewer.cssxrechnung-viewer.js
请将它们放在资源目录中(以防万一,在Eclipse中:制作一个资源文件夹并将其添加为properties-> build path中的源目录)。

xml是上述项目的第一步使用其自己的示例文件生成的,我将其放在pastebin here
(最初直接包含在内,但出现字符限制错误)

最后,Java代码包括丑陋的解决方法:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Comparator;

import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

import org.xml.sax.SAXException;

import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.Serializer;
import net.sf.saxon.s9api.Xslt30Transformer;
import net.sf.saxon.s9api.XsltCompiler;
import net.sf.saxon.s9api.XsltExecutable;

public class SaxonProblem {
    public static void main(String[] args) throws IOException, SaxonApiException, SAXException {
        Path xml = Paths.get("path/to/the.xml");
        //working(xml);
        notWorking(xml);
    }

    public static void working(Path xmlFile) throws IOException, SaxonApiException, SAXException {
        Path dir = Files.createTempDirectory("saxon");
        System.out.println("Temp dir: " + dir.toString());
        Path xsltFile = dir.resolve("xrechnung-html.xsl");

        Files.copy(SaxonProblem.class.getClassLoader().getResourceAsStream("xrechnung-html.xsl"),
                xsltFile, StandardCopyOption.REPLACE_EXISTING);
        Files.copy(SaxonProblem.class.getClassLoader().getResourceAsStream("xrechnung-viewer.css"),
                dir.resolve("xrechnung-viewer.css"), StandardCopyOption.REPLACE_EXISTING);
        Files.copy(SaxonProblem.class.getClassLoader().getResourceAsStream("xrechnung-viewer.js"),
                dir.resolve("xrechnung-viewer.js"), StandardCopyOption.REPLACE_EXISTING);

        // for the sake of brevity, the html is made where the xml was
        Path html = xmlFile.resolveSibling(xmlFile.getFileName().toString() + ".html");
        Source xslt = new StreamSource(xsltFile.toFile());
        Source xml = new StreamSource(xmlFile.toFile());

        transformXml(xml, xslt, html);

        // cleanup
        Files.walk(dir).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
    }

    public static void notWorking(Path xmlFile) throws SaxonApiException, SAXException, IOException {
        // for the sake of brevity, the html is made where the xml was
        Path html = xmlFile.resolveSibling(xmlFile.getFileName().toString() + ".html");

        Source xslt = new StreamSource(SaxonProblem.class.getClassLoader().getResourceAsStream("xrechnung-html.xsl"));
        Source xml = new StreamSource(xmlFile.toFile());
        transformXml(xml, xslt, html);
    }

    public static void transformXml(Source xml, Source xslt, Path output) throws SaxonApiException, SAXException, IOException {
        Processor processor = new Processor(false);
        XsltCompiler compiler = processor.newXsltCompiler();
        XsltExecutable stylesheet = compiler.compile(xslt);
        Serializer out = processor.newSerializer(output.toFile());
        out.setOutputProperty(Serializer.Property.METHOD, "html");
        out.setOutputProperty(Serializer.Property.INDENT, "yes");
        Xslt30Transformer transformer = stylesheet.load30();
        transformer.transform(xml, out);
    }
}






感谢Martin Honnen的评论和Michael Kay的回答,我有了使用UnparsedTextURIResolver的解决方案。确实感觉更像是一种骇客,但它比我以前的解决方法有效并且更好:

Processor processor = new Processor(false);
UnparsedTextURIResolver defaultUtur = processor.getUnderlyingConfiguration().getUnparsedTextURIResolver();
processor.getUnderlyingConfiguration().setUnparsedTextURIResolver(new UnparsedTextURIResolver() {
    @Override
    public Reader resolve(URI arg0, String arg1, Configuration arg2) throws XPathException {
        if (arg0.toString().endsWith("myfilename.css")) {
            InputStream css = SaxonProblem.class.getClassLoader().getResourceAsStream("myfilename.css");
            return new InputStreamReader(css);
        }
        return defaultUtur.resolve(arg0, arg1, arg2);
    }
});
//[...]

最佳答案

一些建议:


将URI与classpath:方案一起使用(最近添加的内容,可能在使用URI的所有路径上均不受支持)
在配置中注册一个UnparsedTextResolver;撒克逊人将寻找资源的任务委派给该解析器
将包含目录的名称作为样式表的参数提供,并使用resolve-uri()函数获取绝对URI。

10-01 17:34
查看更多