【问题标题】:How to loop XSLT transformation trough directories?如何通过目录循环 XSLT 转换?
【发布时间】:2020-07-01 08:09:24
【问题描述】:

我正在使用 XSLT 转换一些 XML 文件。

这里是我使用的脚本


import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;



public class XsltTransformer {
    public static void main(String[] args) throws IOException, URISyntaxException, TransformerException {
            String xmlFile = "/my/directory/file.xml"; //put path of input XML file between ""
            String xslFile = "/my/directory/file.xsl"; //put path of input XSL file between ""
            TransformerFactory factory = TransformerFactory.newInstance();
            Source xslt = new StreamSource(new File(xslFile));
            Transformer transformer = factory.newTransformer(xslt);
            Source text = new StreamSource(new File(xmlFile));
            transformer.transform(text, new StreamResult(new File("/my/directory/output.xml"))); //put path of newly created XML file between ""
    }
}

这样我可以处理一个文件并且它可以工作。

但是,我需要扩展代码来处理遍历目录的多个 xml 文件。

结构是:

DirA
 |
 |-->SubDirA1
 |       |
 |       |---->XMLFile
 |
DirB
 |
 |-->SubDirB1
 |       |
 |       |---->XMLFile
 |
...

我认为这个问题已经部分回答了here 但是,我从未按照建议使用过 ant。

【问题讨论】:

  • 如果你切换到 Saxon 9 或 10 并且只需要一个命令行程序,那么我认为你可以使用它的命令行选项来传递源目录和结果目录。如果你想在 Java 级别实现它,那么你需要使用它的文件/目录 api 来查找和加载任何输入文件并重复运行 XSLT,通常你只会使用 newTemplates 而不是 newTransformer 编译一次。

标签: java xml xslt


【解决方案1】:

您可以尝试遍历您的目录并处理将结果写入单独目录的每个文件。 您通常为此使用Files.walk

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.IOException;
import java.nio.file.*;

public class XsltTransformer {
    private final Path input;
    private final Path output;
    private final Transformer transformer;

    XsltTransformer(Path input, Path output, Transformer transformer) {
        this.input = input;this.output = output;this.transformer = transformer;
    }

    public static void main(String[] args) throws Exception {
        final Transformer transformer = TransformerFactory
            .newInstance()
            .newTransformer(new StreamSource(Paths.get("/my/directory/file.xsl").toFile()));
        new XsltTransformer(
            Paths.get("/my/directory"),
            Paths.get("/my/output"),
            transformer
        ).run();
    }

    private Path transform(Path file) {
        final StreamSource resource = new StreamSource(file.toFile());
        final Path output = this.resolveOutput(file);
        final Result result = new StreamResult(
            output.toFile()
        );
        try {
            this.transformer.transform(resource, result);
            return output;
        } catch (TransformerException ex) {
            throw new IllegalStateException(ex);
        }
    }

    private Path resolveOutput(Path file) {
        return this.output.resolve(this.input.relativize(file));
    }

    public void run() throws IOException {
        Files.walk(this.input)
            .filter(file -> file.getFileName().endsWith(".xml"))
            .map(this::transform)
            .forEach(System.out::println);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 2014-08-13
    • 2017-07-19
    相关资源
    最近更新 更多