This post shows how to remove an XML element based on some parameters in Java using XSLT. We mean parameters for the Transformer object and let XSLT work its magic during the transformation. Therefore, three significant events will happen. First, we define a parameter and its value in Java using the Transformer object. Second, write an XSLT and use that parameter and its value to exclude an XML element conditionally. Finally, use the Transformer object with the XSLT.
Requirements
Before we go on, here are the things we used for this post.
- Java 8 (JDK)
- IntelliJ IDEA – optional
- Spring Boot – optional
- Windows 10 – optional
- XSLT 1.0
XML whose element we want to remove in Java
Consider the following XML document that has a collection of CDs. Each CD element contains its details like title, artist, company (label record), etc. For this post, we want to remove all COMPANY XML elements using Java and XSLT during the transformation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="UTF-8"?> <CATALOG xmlns="https://turreta.com/blog/default-ns" xmlns:artist="https://turreta.com/blog/artist-ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <CD> <TITLE>Empire Burlesque</TITLE> <artist:ARTIST>Bob Dylan</artist:ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE xsi:type="WholeSalePrice">10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <artist:ARTIST>Bonnie Tyler</artist:ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE xsi:type="RetailSalePrice">9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG> |
After the transformation, he will have created the following new XML document without the COMPANY elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="UTF-8"?> <CATALOG xmlns="https://turreta.com/blog/default-ns" xmlns:artist="https://turreta.com/blog/artist-ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://arvato-systems.de/csdb-si csdb-si-messages.xsd "> <CD> <TITLE>EMPIRE BURLESQUE</TITLE> <artist:ARTIST>BOB DYLAN</artist:ARTIST> <COUNTRY>USA</COUNTRY> <PRICE xsi:type="whole-sale-price">10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>HIDE YOUR HEART</TITLE> <artist:ARTIST>Bonnie Tyler</artist:ARTIST> <COUNTRY>UK</COUNTRY> <PRICE xsi:type="retail-sale-price">9.90</PRICE> <YEAR>1988</YEAR> </CD> </CATALOG> |
Easy, right? Let us how we can do it in Java using XSLT.
Set Parameters For Transformer Object In Java
The codes below pass parameters to the Transformer object.
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 28 29 30 31 32 33 34 35 36 | package com.turreta.xml.xlst.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.xml.transform.*; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.File; @SpringBootApplication public class ComTurretaXmlXlstDemoApplication { public static void main(String[] args) { SpringApplication.run(ComTurretaXmlXlstDemoApplication.class, args); ClassLoader classLoader = ComTurretaXmlXlstDemoApplication.class.getClassLoader(); Source inputXml = new StreamSource(classLoader.getResource("input.xml").getFile()); Source xsl = new StreamSource(classLoader.getResource("transformation.xsl").getFile()); Result outputXml = new StreamResult( new File("C:\\Users\\abc\\Desktop\\test-data\\2\\output.xml")); try { Transformer transformer = TransformerFactory.newInstance().newTransformer(xsl); transformer.setParameter("removeCompany", true); transformer.transform(inputXml, outputXml); } catch (TransformerException e) { e.getMessage(); } } } |
XSLT To Remove XML Element With Java codes
For our post, we have the following code snipper in the transformation.xsl file. For our XSLT to work, we created a template to match the COMPANY XML element we want to remove. Then, we use the choose and when operators. These will test the parameter we provided the Transformer object.
1 2 3 4 5 6 7 8 9 10 11 | ... <xsl:template match="/default:CATALOG/default:CD/default:COMPANY"> <xsl:choose> <xsl:when test="(removeCompany!='true')"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:when> </xsl:choose> </xsl:template> ... |
Moreover, we use a boolean expression that checks whether the parameter removeCompany is ‘true’ or not. If it is not “true”, we include the COMPANY XML element during the transformation. Otherwise, we exclude the COMPANY element.
Download the Codes
The codes for this post are available on GitHub.