Java使用Saxon(s9api)转换XML:如何在资源中添加输入文件?

问题描述 投票:0回答:1

我使用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提供需要包含的文件的其他StreamSource?我什么都找不到。

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

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

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


示例代码

我不具备编写XSLT的知识,因此我只能提供非最小的示例文件。可以找到xslt及其两个必需的文件(css和js)here。您需要的是三个“ 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);
    }
}
java xml xslt transformation saxon
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.