XSLT Processing Update (#14693)

This commit is contained in:
Kingsley✨ 2023-09-02 05:27:34 +00:00 committed by GitHub
parent b7c65ace34
commit 29c9c5dba9
2 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package com.baeldung.xsltProcessing;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class XSLTProcessorWithParametersAndOption {
public static void transformXMLWithParametersAndOption(
String inputXMLPath,
String xsltPath,
String outputHTMLPath,
String companyName,
boolean enableIndentation
) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new File(xsltPath));
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setParameter("companyName", companyName);
if (enableIndentation) {
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
}
Source xmlSource = new StreamSource(new File(inputXMLPath));
Result outputResult = new StreamResult(new File(outputHTMLPath));
transformer.transform(xmlSource, outputResult);
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.xsltProcessing;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
public class XSLTProcessorWithTemplate {
public static void transformXMLUsingTemplate(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource(new File(xsltPath));
Templates templates = transformerFactory.newTemplates(xsltSource);
Transformer transformer = templates.newTransformer();
Source xmlSource = new StreamSource(new File(inputXMLPath));
Result outputResult = new StreamResult(new File(outputHTMLPath));
transformer.transform(xmlSource, outputResult);
}
}