0

Modify XML Element Content in Java using XSLT

This post demonstrates how to modify some parts of the XML content in Java using XSLT. The example herein is rather straight-forward but provides basic working codes before moving to more advanced stuff.

Requirements

Stuff used.

  • Java 8 (JDK)
  • IntelliJ IDEA – optional
  • Spring Boot – optional
  • Windows 10 – optional
  • XSLT 1.0

Purpose

Modify some element text in input.xml using Java based on rules and expressions declared in translation.xsl. The modified XML content is written to output.xml.

We are to prefix the heading value with CONFIDENTIAL: and append Please do not print! to the body value.

Sample XML and XSL Files

These are the XML files to be used here – input.xml and translation.xsl

Translation.xsl contains rules and expressions on how to go about replacing stuff in the input.xml and produce the output.xml file with some Java codes.

Target Output XML

The output.xml will have this content:

Java Codes

Download the Codes

https://github.com/Turreta/Replace-XML-Contents-in-Java-using-XSLT

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert