Stripping namespaces using XSLT

Ramkumar Menon
Director, Product Strategy

A common question often asked is "I have a namespace qualified XML document -
I need to remove all xmlns attributes from the same".

Here is an XSLT that you can use to strip all namespaces from your document.

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  

  2. exclude-result-prefixes="xsl">  


  4.  <xsl:template match="*">  

  5.     <xsl:element name="{local-name()}">  

  6.       <xsl:apply-templates select="@* | node()"/>  

  7.     </xsl:element>  

  8.   </xsl:template>  

  9.   <xsl:template match="@* | text()">  

  10.     <xsl:copy/>  

  11.   </xsl:template>  

  12. </xsl:stylesheet>

Join the discussion

Comments ( 3 )
  • Hany Harraz Monday, August 25, 2008
    what if I want to change a namspace
    lets say that what I have is
    < soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:q0="http://address" >
    < soapenv:Body>
    < q0:findByLocation>
    < city/>
    < state>NC< /state>
    < /q0:findByLocation>
    < /soapenv:Body>
    < /soapenv:Envelope>
    and I want to change namespace q0 to be "http://east.address"
    then how do i do that?
  • n gunaran Thursday, January 15, 2009
    hany harraz,
    I'm looking for a solution for a similar problem did you find a solution if so please share it.
  • best cash isas Friday, April 29, 2011
    Thanks so much for sharing all of the awesome info! I am looking forward to checking out more posts!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.