Course – LS – All

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

>> CHECK OUT THE COURSE

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.

As always, the code is available over on GitHub.

Course – LS – All

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

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!