Hello,

I need to just replace the XML namespace using XSLT. My input XML looks like as follows. I just want to replace "oldNameSpace" with "newNameSpace" and rest of XML want as it is..

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soapenv:Body>
		<impl:requestData
			xmlns:impl="oldNameSpace">
			<xmlString ChangeDate="2008-12-18T14:47:11.773+01:00"
				IdentificationNumber="WDDKJ5GBXAF000229"
				OrderNumber="08 295 70821"
				ProductionNumber="1700158">
				<ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00"
					UserID="kris" Version="1.1.1" />
			</xmlString>
		</impl:requestData>
	</soapenv:Body>
</soapenv:Envelope>

I tried with following XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:source="oldNameSpace"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"  >
	<xsl:output method="xml" encoding="iso-8859-1" indent="yes"       />
 
	<xsl:template match="source:*"   >
		<xsl:element name="{name(.)}" namespace="newNameSPace">
		<xsl:copy >
			<xsl:copy-of select="@*"   />
			</xsl:copy>
			<xsl:apply-templates />
		</xsl:element>
	</xsl:template>
 
	<!-- Just copy any other elements, attributes, etc. -->
	<xsl:template match="@*|node()">
		<xsl:copy   >
			<xsl:apply-templates select="@*|node()" />
		</xsl:copy>
	</xsl:template>
</xsl:stylesheet>

but it is not giving the desired results...it is giving the following results.

<?xml version="1.0" encoding="iso-8859-1"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:impl="oldNameSpace" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<soapenv:Body>
		<impl:requestData xmlns:impl="newNameSPace">
<impl:requestData xmlns:impl="oldNameSpace"/>
			<xmlString xmlns:impl="oldNameSpace" ChangeDate="2008-12-18T14:47:11.773+01:00" IdentificationNumber="WDDKJ5GBXAF000229" OrderNumber="08 295 70821" ProductionNumber="1700158">
				<ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00" UserID="kris" Version="1.1.1"/>
			</xmlString>
		</impl:requestData>
	</soapenv:Body>
</soapenv:Envelope>

Anyone can help in this ....
Thanks a lot.

---Krishna

There is no simple way to do what you want using XSLT because of the multiple namespace declarations in the root element. In fact, the simpliest way is to use a stream editor such as sed.

However, if you are willing to accept a slight modification of the file to eliminate redundant namespace declarations, try the following stylesheet

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:output method="xml" encoding="UTF-8" indent="yes" />

  <xsl:template match="@*|node()">
     <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
  </xsl:template>

  <xsl:template match="node()[name() = 'soapenv:Envelope']" >
     <xsl:element name="{name(.)}">
        <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
  </xsl:template>

  <xsl:template match="node()[starts-with(name(),'impl:')]">
     <xsl:element name="{name(.)}" namespace="newNameSpace">
       <xsl:apply-templates select="@*|node()"/>
     </xsl:element>
  </xsl:template>

</xsl:stylesheet>

This outputs

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope">
        <soapenv:Body xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <impl:requestData xmlns:impl="newNameSpace">
                        <xmlString ChangeDate="2008-12-18T14:47:11.773+01:00" IdentificationNumber="WDDKJ5GBXAF000229" OrderNumber="08 295 70821" ProductionNumber="1700158">
                                <ServiceTool ExecutionTime="2008-12-18T14:47:11.773+01:00" UserID="kris" Version="1.1.1"/>
                        </xmlString>
                </impl:requestData>
        </soapenv:Body>
</soapenv:Envelope>
This article has been dead for over six months. Start a new discussion instead.