eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (cat=Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

1. Overview

XSLT, which stands for Extensible Stylesheet Language Transformations, is an XML-based language that we can use to transform one XML document into another text document. In this article, we’ll discuss everything that goes into XSLT processing, from setup to advanced techniques.

2. XSLT Fundamentals

XSLT operates based on the input XML document and an XSLT stylesheet. The XML document contains the data that needs to be transformed, while the stylesheet defines the rules for carrying out the transformation. The transformation process involves applying predefined templates from the stylesheet to parts of the input XML document.

An XSLT stylesheet is an XML document that lays down the guidelines and instructions for executing transformations. It consists of elements, including templates, match patterns, and transformation instructions. Templates define how different nodes in the XML document should be transformed, while match patterns determine which nodes should be matched by these templates.

XSLT stylesheets offer a range of functionalities, such, as logic, loops, variable assignments, and sorting capabilities. These features empower developers to convert XML data into output formats with flexibility.

3. Loading XML Input and XSLT Stylesheet

To perform an XSLT transformation in Java, let’s load the input XML document and the XSLT stylesheet:

Source xmlSource = new StreamSource("input.xml");
Source xsltSource = new StreamSource("stylesheet.xslt");

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);

StreamResult result = new StreamResult("output.html");
transformer.transform(xmlSource, result);
System.out.println("XSLT transformation completed successfully.");

Let’s break down the code in the above snippet. After loading our XML and XSLT documents, we create a TransformerFactory using the TransformerFactory.newInstance() method. This factory method’s purpose is to generate instances of Transformers, which are responsible for carrying out the transformation process.

Next, we load the XSLT stylesheet by using a StreamSource to represent the file containing the stylesheet. The TransformerFactory.newTransformer() method compiles this stylesheet into a Transformer instance, which enables us to perform the desired transformation.

Next, we utilize another StreamSource that represents the XML file to be transformed as we specify the input XML document. We create a StreamResult object to store the output of our XSLT transformation. In this case, we save the transformed result as a file named output.html.

Finally, we initiate the transformation process by calling our Transformer object’s transform() method with both input xmlSource and output result as parameters. The transform() method processes our input XML document and applies the rules defined in our XSLT stylesheet to generate an output, which the method stores in our StreamResult object.

3.1. The Role of Templates in XSLT Transformation

In addition to the aforementioned approach, we can enhance our XSLT transformations by utilizing templates. Precompiled representations of XSLT stylesheets, which offer performance advantages over repeatedly creating new Transformer instances for the same stylesheet, are provided by templates.

Here’s how we can employ Templates in our XSLT processing:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Source xsltSource = new StreamSource("stylesheet.xslt");
Templates templates = transformerFactory.newTemplates(xsltSource);        
Transformer transformer = templates.newTransformer();

Source xmlSource = new StreamSource("input.xml");
Result result = new StreamResult("output.html");

transformer.transform(xmlSource, result);

In this updated example, we continue to create a TransformerFactory instance using newInstance(). However, instead of directly creating a Transformer instance, we now use TransformerFactory.newTemplates(xsltSource) to compile advanced XSLT methods into Templates. These precompiled templates can then be used to create multiple Transformer instances.

This approach offers an advantage when we need to perform multiple transformations using the same stylesheet, as it avoids the overhead of recompiling the stylesheet. The call to templates.newTransformer() creates a new Transformer instance from the precompiled Templates, allowing us to execute the transformation as usual.

4. Configuring Transformation Parameters and Options

When performing XSLT transformations, there may be cases where we need to pass values to the stylesheet or modify how the transformation is carried out. We can achieve this in Java by using the javax.xml.transform.Transformer class.

We can use the Transformer.setParameter(String name, Object value) method to assign values to the Transformer‘s parameters. Here, the name refers to the parameter name specified in the XSLT stylesheet, and the value represents the desired value for that parameter.

Let’s see a quick example that demonstrates how to assign a parameter:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setParameter("companyName", "Baeldung Corporation");

In this example, we’ve set up the companyName parameter with a value of “Baeldung Corporation” using the setParameter() method. This enables us to provide data to the XSLT stylesheet during the transformation process.

To modify the settings for transforming options, such as activating indentation for output or controlling output escaping, we can then use methods like Transformer.setOutputProperty(String name, String value).

Let’s see another example that shows how to enable output indentation:

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer(xsltSource);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

In this example, we configure the output to be indented using the setOutputProperty() method with the OutputKeys.INDENT property set to “yes”.

As we’ve seen throughout this subsection, we can customize the transformation process and control the output format according to our specific requirements by configuring parameters and options.

5. Conclusion

In this article, we delved into the realm of XSLT processing using Java and explored its capabilities in converting XML documents to different formats. By understanding the basics of XSLT and becoming proficient in XPath expressions by exploring the wide range of Java APIs available, we unlock the potential to perform efficient and impactful transformations.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Summer Sale 2026 – NPI EA (cat= Baeldung)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

Course – Summer Sale 2026 – NPI (All)
announcement - icon

Yes, we're now running our only Summer Sale. All Courses are 30% off until 20th July, 2026:

>> EXPLORE ACCESS NOW

eBook Jackson – NPI EA – 3 (cat = Jackson)