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
This commit is contained in:
parent
1d86639891
commit
4551a5413e
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-libraries-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-libraries-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation.validation-api.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<properties>
|
||||
<javax.validation.validation-api.version>2.0.1.Final</javax.validation.validation-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<Configuration status="WARN">
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
</Console>
|
||||
</Appenders>
|
||||
<Loggers>
|
||||
<Logger name="org.apache.meecrowave" level="warn">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Logger>
|
||||
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console"/>
|
||||
</Root>
|
||||
</Loggers>
|
||||
</Configuration>
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<root>
|
||||
<person gender="male">
|
||||
<name>John Doe</name>
|
||||
<age>30</age>
|
||||
</person>
|
||||
<person gender="female">
|
||||
<name>Jane Smith</name>
|
||||
<age>25</age>
|
||||
</person>
|
||||
</root>
|
|
@ -0,0 +1,3 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<p>Male person: John Doe, Age: 30</p>
|
||||
<p>Female person: Jane Smith, Age: 25</p>
|
|
@ -0,0 +1,22 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
|
||||
|
||||
<xsl:template match="person[@gender='male']">
|
||||
<xsl:element name="p">
|
||||
<xsl:text>Male person: </xsl:text>
|
||||
<xsl:value-of select="name"/>
|
||||
<xsl:text>, Age: </xsl:text>
|
||||
<xsl:value-of select="age"/>
|
||||
</xsl:element>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template match="person[@gender='female']">
|
||||
<xsl:element name="p">
|
||||
<xsl:text>Female person: </xsl:text>
|
||||
<xsl:value-of select="name"/>
|
||||
<xsl:text>, Age: </xsl:text>
|
||||
<xsl:value-of select="age"/>
|
||||
</xsl:element>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
2
pom.xml
2
pom.xml
|
@ -758,6 +758,7 @@
|
|||
|
||||
<module>algorithms-modules</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-libraries-2</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>di-modules</module>
|
||||
|
@ -1025,6 +1026,7 @@
|
|||
|
||||
<module>algorithms-modules</module>
|
||||
<module>apache-libraries</module>
|
||||
<module>apache-libraries-2</module>
|
||||
<module>apache-poi</module>
|
||||
<module>apache-velocity</module>
|
||||
<module>di-modules</module>
|
||||
|
|
Loading…
Reference in New Issue