【问题标题】:split XML file by elements按元素拆分 XML 文件
【发布时间】:2011-02-02 10:41:38
【问题描述】:

这是一个类似的问题,几乎没有区别:

Split XML file into multiple files based on a threshold value

我的根元素称为stores,要拆分的元素称为store。我想从带有或不带有 xsl 的 java 中执行此操作,这是我用来触发 xsl 的 java 代码:

public void transform(String transformator, File source, String destination) {
    try {    
        TransformerFactory tFactory = TransformerFactory.newInstance();

        Transformer transformer = tFactory.newTransformer(new StreamSource(transformatorLocation));

        try {
            transformer.transform(new StreamSource(new InputStreamReader(new FileInputStream(source), "UTF-8")), new StreamResult(new OutputStreamWriter(new FileOutputStream(destination), "UTF-8")));
        } catch (TransformerException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.err.println("File is missing");
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();
    }
}

我在这个问题上尝试了两种解决方案,但它们都产生错误,一个是:

错误:'不支持的 XSL 元素'http://www.w3.org/1999/XSL/Transform:for-each-group'' javax.xml.transform.TransformerException: java.lang.RuntimeException: 不支持的 XSL 元素 'http://www.w3.org/1999/XSL/Transform:for-each-group'

另外就是样式表无法编译。

我试图完成什么?将参数传递给 java 类,我想要一个文件中有多少个商店,并将其拆分为 n 个部分。

【问题讨论】:

    标签: java xml xslt


    【解决方案1】:

    我相信for-each-group 是一个 XSLT 2.0 构造。 java 中默认的 XSLT 处理器还不支持 XSLT 2.0。

    您要么需要坚持使用 XSLT 1.0,要么找到支持 2.0 的 XSLT 处理器,然后改用它(例如,Saxon)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多