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.
[wp_ad_camp_1]
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.
[wp_ad_camp_2]
Sample XML and XSL Files
These are the XML files to be used here – input.xml
and translation.xsl
1 2 3 4 5 6 7 8 9 10 11 12 13 | <email> <to-list> <to>toby.lim@xyz.com</to> <to>poh.leong@xyz.com</to> </to-list> <cc-list> <cc>abc@xyz</cc> <cc>wto@xyz</cc> </cc-list> <from>patricia.lee@xyz.com</from> <heading>Production Release Rescheduled</heading> <body>FYI</body> </email> |
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.
[wp_ad_camp_3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:param name="headingPrefix"/> <xsl:param name="bodyDisclaimer"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="*[local-name()='heading']"> <xsl:copy> <xsl:value-of select="concat($headingPrefix, ':', text())"/> </xsl:copy> </xsl:template> <xsl:template match="*[local-name()='body']"> <xsl:copy> <xsl:value-of select="concat(text(), ' ', $bodyDisclaimer)"/> </xsl:copy> </xsl:template> </xsl:stylesheet> |
Target Output XML
The output.xml
will have this content:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="UTF-8"?><email> <to-list> <to>toby.lim@xyz.com</to> <to>poh.leong@xyz.com</to> </to-list> <cc-list> <cc>abc@xyz</cc> <cc>wto@xyz</cc> </cc-list> <from>patricia.lee@xyz.com</from> <heading>CONFIDENTIAL:Production Release Rescheduled</heading> <body>FYI Please do not print!</body> </email> |
Java Codes
[wp_ad_camp_4]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.turreta.xslt; ... @SpringBootApplication public class ComTurretaXsltApplication { public static void main(String[] args) { SpringApplication.run(ComTurretaXsltApplication.class, args); Source inputXml = new StreamSource(new File("C:\\Users\\user111abc\\Desktop\\test-data\\input.xml")); Source xsl = new StreamSource(new File("C:\\Users\\user111abc\\Desktop\\test-data\\translation.xsl")); Result outputXml = new StreamResult(new File("C:\\user111abc\\sang018\\Desktop\\test-data\\output.xml")); try { Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl); transformer.setParameter("headingPrefix", "CONFIDENTIAL"); transformer.setParameter("bodyDisclaimer", "Please do not print!"); transformer.transform(inputXml, outputXml); } catch (TransformerException e) { e.getMessage(); } } } |
[wp_ad_camp_5]
Download the Codes
https://github.com/Turreta/Replace-XML-Contents-in-Java-using-XSLT