Skip to content

Commit 29c9c5d

Browse files
XSLT Processing Update (#14693)
1 parent b7c65ac commit 29c9c5d

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.xsltProcessing;
2+
3+
import javax.xml.transform.*;
4+
import javax.xml.transform.stream.StreamResult;
5+
import javax.xml.transform.stream.StreamSource;
6+
import java.io.File;
7+
8+
public class XSLTProcessorWithParametersAndOption {
9+
public static void transformXMLWithParametersAndOption(
10+
String inputXMLPath,
11+
String xsltPath,
12+
String outputHTMLPath,
13+
String companyName,
14+
boolean enableIndentation
15+
) throws TransformerException {
16+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
17+
Source xsltSource = new StreamSource(new File(xsltPath));
18+
Transformer transformer = transformerFactory.newTransformer(xsltSource);
19+
20+
transformer.setParameter("companyName", companyName);
21+
22+
if (enableIndentation) {
23+
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
24+
}
25+
26+
Source xmlSource = new StreamSource(new File(inputXMLPath));
27+
Result outputResult = new StreamResult(new File(outputHTMLPath));
28+
29+
transformer.transform(xmlSource, outputResult);
30+
}
31+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.xsltProcessing;
2+
3+
import javax.xml.transform.*;
4+
import javax.xml.transform.stream.StreamResult;
5+
import javax.xml.transform.stream.StreamSource;
6+
import java.io.File;
7+
8+
public class XSLTProcessorWithTemplate {
9+
public static void transformXMLUsingTemplate(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
10+
TransformerFactory transformerFactory = TransformerFactory.newInstance();
11+
Source xsltSource = new StreamSource(new File(xsltPath));
12+
Templates templates = transformerFactory.newTemplates(xsltSource);
13+
14+
Transformer transformer = templates.newTransformer();
15+
16+
Source xmlSource = new StreamSource(new File(inputXMLPath));
17+
Result outputResult = new StreamResult(new File(outputHTMLPath));
18+
19+
transformer.transform(xmlSource, outputResult);
20+
}
21+
}

0 commit comments

Comments
 (0)