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:


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 {

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=""





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"
	<xsl:template match="node()">
			<xsl:apply-templates select="node() | @*" />
	<xsl:template match="*">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="node() | @*" />
	<xsl:template match="@*">
			<xsl:apply-templates select="node() | @*" />

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:Header />
				<ns2:name>Liton Sarkar</ns2:name>
					<ns2:street>Sukanta Nagar</ns2:street>

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();


	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>
				<name>Liton Sarkar</name>
					<street>Sukanta Nagar</street>

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

Source Code


Leave a Reply

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