From 4551a5413e7588bb0462be76fc0c79f9e7d45df6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Kingsley=E2=9C=A8?=
<64941442+KingsleyAmankwah@users.noreply.github.com>
Date: Wed, 16 Aug 2023 16:38:36 +0000
Subject: [PATCH] XSLT Processing In Java (#14507)
* Jaxb vs Jaxp
* apache libraries added to profile
* XSLT processing in Java
* XSLT Processing In Java
* XSLT Processing modified
* XSLT Processing modified
* XSLT Processing
* pom clean up
* ReadMe clean up
* pom clean up
* Clean up
---
apache-libraries-2/README.md | 0
apache-libraries-2/pom.xml | 29 ++++++++++++
.../java/com/baeldung/xslt/XSLTProcessor.java | 18 +++++++
.../resources/avroHttpRequest-schema.avsc | 47 +++++++++++++++++++
.../src/main/resources/log4j2.xml | 18 +++++++
.../src/main/resources/logback.xml | 13 +++++
.../baeldung/xslt/XSLTProcessorUnitTest.java | 29 ++++++++++++
.../src/test/resources/input.xml | 11 +++++
.../src/test/resources/output.html | 3 ++
.../src/test/resources/stylesheet.xslt | 22 +++++++++
pom.xml | 2 +
11 files changed, 192 insertions(+)
create mode 100644 apache-libraries-2/README.md
create mode 100644 apache-libraries-2/pom.xml
create mode 100644 apache-libraries-2/src/main/java/com/baeldung/xslt/XSLTProcessor.java
create mode 100644 apache-libraries-2/src/main/resources/avroHttpRequest-schema.avsc
create mode 100644 apache-libraries-2/src/main/resources/log4j2.xml
create mode 100644 apache-libraries-2/src/main/resources/logback.xml
create mode 100644 apache-libraries-2/src/test/java/com/baeldung/xslt/XSLTProcessorUnitTest.java
create mode 100644 apache-libraries-2/src/test/resources/input.xml
create mode 100644 apache-libraries-2/src/test/resources/output.html
create mode 100644 apache-libraries-2/src/test/resources/stylesheet.xslt
diff --git a/apache-libraries-2/README.md b/apache-libraries-2/README.md
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/apache-libraries-2/pom.xml b/apache-libraries-2/pom.xml
new file mode 100644
index 0000000000..618ff4a188
--- /dev/null
+++ b/apache-libraries-2/pom.xml
@@ -0,0 +1,29 @@
+
+
+ 4.0.0
+ apache-libraries-2
+ 0.0.1-SNAPSHOT
+ apache-libraries-2
+
+
+ com.baeldung
+ parent-modules
+ 1.0.0-SNAPSHOT
+
+
+
+
+ javax.validation
+ validation-api
+ ${javax.validation.validation-api.version}
+
+
+
+
+
+ 2.0.1.Final
+
+
+
\ No newline at end of file
diff --git a/apache-libraries-2/src/main/java/com/baeldung/xslt/XSLTProcessor.java b/apache-libraries-2/src/main/java/com/baeldung/xslt/XSLTProcessor.java
new file mode 100644
index 0000000000..6bc0023485
--- /dev/null
+++ b/apache-libraries-2/src/main/java/com/baeldung/xslt/XSLTProcessor.java
@@ -0,0 +1,18 @@
+package com.baeldung.xslt;
+
+import javax.xml.transform.*;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+import java.io.File;
+
+public class XSLTProcessor {
+ public static void transformXMLUsingXSLT(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
+ Source xmlSource = new StreamSource(new File(inputXMLPath));
+ Source xsltSource = new StreamSource(new File(xsltPath));
+ Result output = new StreamResult(new File(outputHTMLPath));
+
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ Transformer transformer = transformerFactory.newTransformer(xsltSource);
+ transformer.transform(xmlSource, output);
+ }
+}
diff --git a/apache-libraries-2/src/main/resources/avroHttpRequest-schema.avsc b/apache-libraries-2/src/main/resources/avroHttpRequest-schema.avsc
new file mode 100644
index 0000000000..18179a9cde
--- /dev/null
+++ b/apache-libraries-2/src/main/resources/avroHttpRequest-schema.avsc
@@ -0,0 +1,47 @@
+{
+ "type":"record",
+ "name":"AvroHttpRequest",
+ "namespace":"com.baeldung.avro.model",
+ "fields":[
+ {
+ "name":"requestTime",
+ "type":"long"
+ },
+ {
+ "name":"clientIdentifier",
+ "type":{
+ "type":"record",
+ "name":"ClientIdentifier",
+ "fields":[
+ {
+ "name":"hostName",
+ "type":"string"
+ },
+ {
+ "name":"ipAddress",
+ "type":"string"
+ }
+ ]
+ }
+ },
+ {
+ "name":"employeeNames",
+ "type":{
+ "type":"array",
+ "items":"string"
+ },
+ "default":null
+ },
+ {
+ "name":"active",
+ "type":{
+ "type":"enum",
+ "name":"Active",
+ "symbols":[
+ "YES",
+ "NO"
+ ]
+ }
+ }
+ ]
+}
\ No newline at end of file
diff --git a/apache-libraries-2/src/main/resources/log4j2.xml b/apache-libraries-2/src/main/resources/log4j2.xml
new file mode 100644
index 0000000000..d1ea5173fa
--- /dev/null
+++ b/apache-libraries-2/src/main/resources/log4j2.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apache-libraries-2/src/main/resources/logback.xml b/apache-libraries-2/src/main/resources/logback.xml
new file mode 100644
index 0000000000..7d900d8ea8
--- /dev/null
+++ b/apache-libraries-2/src/main/resources/logback.xml
@@ -0,0 +1,13 @@
+
+
+
+
+ %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/apache-libraries-2/src/test/java/com/baeldung/xslt/XSLTProcessorUnitTest.java b/apache-libraries-2/src/test/java/com/baeldung/xslt/XSLTProcessorUnitTest.java
new file mode 100644
index 0000000000..cbfbf78c87
--- /dev/null
+++ b/apache-libraries-2/src/test/java/com/baeldung/xslt/XSLTProcessorUnitTest.java
@@ -0,0 +1,29 @@
+package com.baeldung.xslt;
+
+import org.junit.jupiter.api.Test;
+
+import javax.xml.transform.TransformerException;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class XSLTProcessorUnitTest {
+
+ @Test
+ public void givenValidInputAndStylesheet_whenTransformingXML_thenOutputHTMLCreated() throws TransformerException, IOException {
+ // Given
+ String inputXMLPath = "src/test/resources/input.xml";
+ String xsltPath = "src/test/resources/stylesheet.xslt";
+ String outputHTMLPath = "src/test/resources/output.html";
+
+
+ XSLTProcessor.transformXMLUsingXSLT(inputXMLPath, xsltPath, outputHTMLPath);
+
+
+ Path outputFile = Paths.get(outputHTMLPath);
+ assertTrue(Files.exists(outputFile));
+ }
+}
diff --git a/apache-libraries-2/src/test/resources/input.xml b/apache-libraries-2/src/test/resources/input.xml
new file mode 100644
index 0000000000..e283680337
--- /dev/null
+++ b/apache-libraries-2/src/test/resources/input.xml
@@ -0,0 +1,11 @@
+
+
+
+ John Doe
+ 30
+
+
+ Jane Smith
+ 25
+
+
diff --git a/apache-libraries-2/src/test/resources/output.html b/apache-libraries-2/src/test/resources/output.html
new file mode 100644
index 0000000000..b75e73ca15
--- /dev/null
+++ b/apache-libraries-2/src/test/resources/output.html
@@ -0,0 +1,3 @@
+
+
Male person: John Doe, Age: 30
+ Female person: Jane Smith, Age: 25
diff --git a/apache-libraries-2/src/test/resources/stylesheet.xslt b/apache-libraries-2/src/test/resources/stylesheet.xslt
new file mode 100644
index 0000000000..9f07852a2a
--- /dev/null
+++ b/apache-libraries-2/src/test/resources/stylesheet.xslt
@@ -0,0 +1,22 @@
+
+
+
+
+
+ Male person:
+
+ , Age:
+
+
+
+
+
+
+ Female person:
+
+ , Age:
+
+
+
+
+
diff --git a/pom.xml b/pom.xml
index de95855f49..fd5257877a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -758,6 +758,7 @@
algorithms-modules
apache-libraries
+ apache-libraries-2
apache-poi
apache-velocity
di-modules
@@ -1025,6 +1026,7 @@
algorithms-modules
apache-libraries
+ apache-libraries-2
apache-poi
apache-velocity
di-modules