title: How to replace namespace prefix in element and attributes using xslt
body: <p>I have a source xml like following:</p>
  
  <pre><code>&lt;SampleRequest xmlns="http://sample.com/s"
         xmlns:s1="http://sample.com/s1"
         xmlns:s2="http://sample.com/s2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://sample.com/s schema.xsd"&gt;
  &lt;data&gt;
  &lt;s1:customer s1:firstName="Jim" s1:lastName="Ellison"/&gt;
  &lt;s2:address&gt;
      123 test street
  &lt;/s2:address&gt;
  &lt;/data&gt;
  &lt;/SampleRequest&gt;
  </code></pre>
  
  <p>I need to transform it to the following</p>
  
  <pre><code>&lt;SampleRequest xmlns="http://sample.com/t"
         xmlns:t1="http://sample.com/t1"
         xmlns:t2="http://sample.com/t2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://sample.com/t targetSchema.xsd"&gt;
  &lt;data&gt;
  &lt;t1:customer t1:firstName="Jim" t1:lastName="Ellison"/&gt;
  &lt;t2:address&gt;
      123 test street
  &lt;/t2:address&gt;
  &lt;/data&gt;
  &lt;/SampleRequest&gt;
  </code></pre>
  
  <p>both xmls has identical schema but different namespace.</p>
  
  <p>I was trying to use following xslt</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
              xmlns="http://sample.com/s"
              xmlns:s1="http://sample.com/s1"
              xmlns:s2="http://sample.com/s2"
              xmlns:t1="http://sample.com/t1"
              xmlns:t2="http://sample.com/t2"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="s1 s2"&gt;
  &lt;xsl:output method="xml" encoding="UTF-8" indent="yes"/&gt;
  &lt;xsl:strip-space elements="*"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;SampleRequest xmlns="http://sample.com/t"
         xmlns:t1="http://sample.com/t1"
         xmlns:t2="http://sample.com/t2"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
          &lt;xsl:attribute name="xsi:schemaLocation" namespace="http://www.w3.org/2001/XMLSchema-instance"&gt;
              http://sample.com/t targetSchema.xsd
          &lt;/xsl:attribute&gt;
          &lt;xsl:copy&gt;
               &lt;xsl:apply-templates select="@* | node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/SampleRequest&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match="s1:*"&gt; 
      &lt;xsl:element name="t1:{local-name()}" namespace="http://sample.com/t1"&gt;      
          &lt;xsl:apply-templates select="* | node()"/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="s2:*"&gt; 
      &lt;xsl:element name="t2:{local-name()}" namespace="http://sample.com/t2"&gt;
          &lt;xsl:apply-templates select="* | node()"/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>And I am getting following result</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;SampleRequest xmlns="http://sample.com/t"   xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t2="http://sample.com/t2" xmlns:t1="http://sample.com/t1" xsi:schemaLocation=" http://sample.com/t targetSchema.xsd"&gt;
  &lt;t1:customer/&gt;
  &lt;t2:address&gt;
      123 test street
  &lt;/t2:address&gt;
  &lt;/SampleRequest&gt;
  </code></pre>
  
  <p>It seems all attributes are missing.
  I am very new to XSLT, every bit help appreciated</p>