How To Remove Namespace from XML Using XSLT

Removing Namespace

In this tutorial I will show you how to remove namespace from XML using XSLT. Removing namespaces from an XML document is not recommended and is in essence comparable to removing namespaces from a programming framework or library. It may risk name clashes and lose the ability to differentiate between distinct elements. But if you need absolutely to remove for some reason then consider to remove namespace from XML using XSLT.

Related Posts:

Prerequisites

Java 8/12/19, Knowledge of XML

Project Setup

You can create either gradle based or maven based project in Eclipse. The name of the project is java-remove-namespace-from-xml-using-xslt.

If you are using gradle based project then you can use below build.gradle script.

plugins {
    id 'java-library'
}

sourceCompatibility = 12
targetCompatibility = 12

repositories {
    jcenter()
}

dependencies {
}

If you are using maven based build tool then you can use below pom.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>java-remove-namespace-from-xml-using-xslt</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>

	<dependencies>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

Remove Namespace From XML

Now you will see the actual example how to remove namespace from XML using XSLT.

You need to create the XSLT file called Xslt2Xml.xsl and put it under classpath src/main/resources/xslt directory. Here is the standard format of XSLT as shown in the below structure to keep everything inside the tag.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:template match="node()">
		<xsl:copy>
			<xsl:apply-templates select="node() | @*" />
		</xsl:copy>
	</xsl:template>
	<xsl:template match="*">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="node() | @*" />
		</xsl:element>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:copy>
			<xsl:apply-templates select="node() | @*" />
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

You need also an XML file to test your application. So I am also keeping the sample input XML file under src/main/resources/xml folder. The name of the input XML file is input.xml. The content of the input XML file is given below:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope
	xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Header />
	<SOAP-ENV:Body>
		<ns2:getUserDetailsResponse
			xmlns:ns2="https://roytuts.com/UserService">
			<ns2:users>
				<ns2:id>4</ns2:id>
				<ns2:name>Liton Sarkar</ns2:name>
				<ns2:email>liton.sarkar@email.com</ns2:email>
				<ns2:address>
					<ns2:street>Sukanta Nagar</ns2:street>
					<ns2:city>Kolkata</ns2:city>
					<ns2:state>WB</ns2:state>
					<ns2:zip>700098</ns2:zip>
					<ns2:country>India</ns2:country>
					<ns2:addressType>COMMUNICATION</ns2:addressType>
				</ns2:address>
			</ns2:users>
		</ns2:getUserDetailsResponse>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I am writing below Java class to transform the XML file to remove namespace from XML using XSLT file. I have put both XML and XSLT files under classpath and I will finally transform to the XML string as an output. This is also called XML to XML transformation but the final XML does not contain any namespace.

public class NamespaceRemover {

	public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException,
			TransformerFactoryConfigurationError, TransformerException {

		NamespaceRemover remover = new NamespaceRemover();

		String resp = remover.remove();

		System.out.println(resp);
	}

	public String remove() throws SAXException, IOException, ParserConfigurationException,
			TransformerFactoryConfigurationError, TransformerException {
		InputStream xmlData = getClass().getClassLoader().getResourceAsStream("xml/input.xml");
		URL xsltURL = getClass().getClassLoader().getResource("xslt/Xslt2Xml.xsl");
		Document xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xmlData);
		Source stylesource = new StreamSource(xsltURL.openStream(), xsltURL.toExternalForm());
		Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource);
		StringWriter stringWriter = new StringWriter();
		transformer.transform(new DOMSource(xmlDocument), new StreamResult(stringWriter));
		return stringWriter.toString();
	}
}

Testing the Namespace Remover Application

Run the above class, you will see the XML output without any namespace.

After removing namespace from above xml file, the final output is given below. You will find this output in the Eclipse console. If you want to write this output to some XML file then you can check the tutorial Transforming XML to HTML using XSLT, where how I had written HTML output to a file.

<?xml version="1.0" encoding="UTF-8"?><Envelope>
	<Header/>
	<Body>
		<getUserDetailsResponse>
			<users>
				<id>4</id>
				<name>Liton Sarkar</name>
				<email>liton.sarkar@email.com</email>
				<address>
					<street>Sukanta Nagar</street>
					<city>Kolkata</city>
					<state>WB</state>
					<zip>700098</zip>
					<country>India</country>
					<addressType>COMMUNICATION</addressType>
				</address>
			</users>
		</getUserDetailsResponse>
	</Body>
</Envelope>

That’s all about removing namespace from XML file using XSLT in Java.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *