title: How to include namespace in XSL transformation in XML
body: <p>I am trying to transform an xml file to another xml file using JAVA. After transformation the new xml will have a set of sub elements and required namespaces. However I am not able to include the specific required namespaces. It is either all or none. Can someone help on this pls ?</p>
  
  <p>Input XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;ns2:root 
      xmlns="http://uri1.com" 
      xmlns:ns2="http://uri2.com"&gt;
      &lt;element1&gt;
          &lt;elementA1&gt;
              &lt;elementB1&gt;AA&lt;/elementB1&gt;            
          &lt;/elementA1&gt;
          &lt;elementA2&gt;12345&lt;/elementA2&gt;
          &lt;elementA3&gt;A 000&lt;/elementA3&gt;
       &lt;/element1&gt;
  &lt;/ns2:root&gt;
  </code></pre>
  
  <p>Expected Output XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;element1 xmlns="http://uri1.com" &gt;
          &lt;elementA1&gt;
              &lt;elementB1&gt;AA&lt;/elementB1&gt;            
          &lt;/elementA1&gt;
          &lt;elementA2&gt;12345&lt;/elementA2&gt;
          &lt;elementA3&gt;A 000&lt;/elementA3&gt;
       &lt;/element1&gt;
  </code></pre>
  
  <p>XSL file I am using</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
  
    &lt;!-- &lt;xsl:output method="xml" omit-xml-declaration="yes" encoding="utf-8"/&gt; --&gt;
  
    &lt;!-- For each element, create a new element with the same local-name (no namespace) --&gt;
    &lt;xsl:template match="*"&gt;
      &lt;xsl:element name="{local-name()}"&gt;
        &lt;xsl:copy-of select="@*"/&gt; 
        &lt;xsl:apply-templates/&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- Skip the root element, just process its children. --&gt;
    &lt;xsl:template match="/*"&gt;
      &lt;xsl:apply-templates/&gt;
    &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Current Output XML ( without namespaces )</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;element1&gt;
          &lt;elementA1&gt;
              &lt;elementB1&gt;AA&lt;/elementB1&gt;            
          &lt;/elementA1&gt;
          &lt;elementA2&gt;12345&lt;/elementA2&gt;
          &lt;elementA3&gt;A 000&lt;/elementA3&gt;
       &lt;/element1&gt;
  </code></pre>
  
  <p>Java code used to do XSL transformation</p>
  
  <pre><code>import java.io.StringReader;
  import java.io.StringWriter;
  
  import javax.xml.transform.Templates;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  
  public final class XsltTransformer {
  
    private Templates templates;
  
    public XsltTransformer(final String xslFilePath) {
      String xslt = FileUtils.getAsString(xslFilePath);
      try {
        this.templates = TransformerFactory.newInstance().newTemplates(new StreamSource(new StringReader(xslt)));
      } catch (Exception e) {
        throw new IllegalArgumentException("Error while creating XLST transformer with XSLT text [" + xslt + "]", e);
      }
    }
  
    public String applyTransformation(final String inputXml) {
      try {
        StreamSource in = new StreamSource(new StringReader(inputXml));
        StreamResult out = new StreamResult(new StringWriter());     
        templates.newTransformer().transform(in, out);
        return out.getWriter().toString();
      } catch (Exception e) {
        throw new IllegalArgumentException("Unable to transform input XML [" + inputXml + "]", e);
      }
    }
  
  }
  </code></pre>
  
  <p>Can someone help me on adding specific namespace as in the expected output XML??
  Thanks !</p>