This is similar question with few differences :
Split XML file into multiple files based on a threshold value
My root element is called stores, and elements to split are called store. And I want to do this from java with xsl or without, here is my java code which I used to trigger the xsl :
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();
}
}
I’ve tried both solutions in this question, but they both produce erros, one is :
ERROR: ‘Unsupported XSL element ‘http://www.w3.org/1999/XSL/Transform:for-each-group”
javax.xml.transform.TransformerException: java.lang.RuntimeException: Unsupported XSL element ‘http://www.w3.org/1999/XSL/Transform:for-each-group‘
Other is that style sheet can’t be compiled.
What I try to accomplish ? Pass argument to java class how many stores do I want in one file, and split it into n parts.
I believe that
for-each-groupis an XSLT 2.0 construct. The default XSLT processors in java do not yet support XSLT 2.0.You either need to stick with XSLT 1.0, or find a XSLT processor that does 2.0, and use that instead (for example, Saxon).