title: Error in adding multiple namespace to the output XML
body: <p>I am trying to generate an XML output from the given input XML using XSLT. The output xml will have different namespaces added to it at various level of the xml tree. I am using XPath axes (descendant) to add the namespace to children nodes, but it outputs the grandchildern nodes next to children node. </p>
  
  <p><strong>My Input XML</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;request&gt;
     &lt;header&gt;
        &lt;sender&gt;
           &lt;User&gt;ServiceUser&lt;/User&gt;
           &lt;userid type="UserId"&gt;ServiceUserid&lt;/userid&gt;
        &lt;/sender&gt;
        &lt;receiver&gt;
           &lt;Country&gt;IND&lt;/Country&gt;
        &lt;/receiver&gt;     
        &lt;id&gt;123456&lt;/id&gt;
     &lt;/header&gt;
     &lt;Address type="physical"&gt;
        &lt;subaddress&gt;
           &lt;number&gt;2&lt;/number&gt;
        &lt;/subaddress&gt;
        &lt;doornumber&gt;13&lt;/doornumber&gt;
        &lt;streetName&gt;mgr road&lt;/streetName&gt;
        &lt;locality&gt;cityname&lt;/locality&gt;
        &lt;postalcode&gt;612453&lt;/postalcode&gt;
     &lt;/Address&gt;
  &lt;/request&gt;
  </code></pre>
  
  <p><strong>My XSLT</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
      &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aa="xyz.com/aa"
      xmlns:bb="xyz.com/bb" xmlns:addr="xyz.com/addr"  
      xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
       version="1.0"&gt;
  
         &lt;xsl:output encoding="utf-8" indent="yes" method="xml" omit-xml-declaration="yes" /&gt;  
         &lt;xsl:template match="/"&gt;
            &lt;soapenv:Envelope&gt;
               &lt;soapenv:Header /&gt;
               &lt;soapenv:Body&gt;
                  &lt;xsl:copy&gt;
                     &lt;xsl:apply-templates select="node()|@*" /&gt;
                  &lt;/xsl:copy&gt;
               &lt;/soapenv:Body&gt;
            &lt;/soapenv:Envelope&gt;
         &lt;/xsl:template&gt;
         &lt;!-- template to copy attributes --&gt;
         &lt;xsl:template match="@*"&gt;
            &lt;xsl:attribute name="{local-name()}"&gt;
               &lt;xsl:value-of select="." /&gt;
            &lt;/xsl:attribute&gt;
         &lt;/xsl:template&gt;
         &lt;!-- apply the 'event' namespace to the top node and its descendants --&gt;
         &lt;xsl:template match="//*"&gt;
            &lt;xsl:choose&gt;
               &lt;!--Add namespace for the root element--&gt;
               &lt;xsl:when test="local-name() = 'request'"&gt;
                  &lt;xsl:element name="aa:{local-name()}"&gt;
                     &lt;xsl:namespace name="aa" select="'xyz.com/aa'" /&gt;
                     &lt;xsl:copy-of select="namespace::*" /&gt;
                     &lt;xsl:apply-templates select="node()|@*" /&gt;
                  &lt;/xsl:element&gt;
               &lt;/xsl:when&gt;
               &lt;xsl:otherwise&gt;
                  &lt;xsl:element name="{local-name()}"&gt;
                     &lt;xsl:copy-of select="*" /&gt;
                  &lt;/xsl:element&gt;
               &lt;/xsl:otherwise&gt;
            &lt;/xsl:choose&gt;
         &lt;/xsl:template&gt;
         &lt;!-- template to copy the rest of the nodes --&gt;
         &lt;xsl:template match="*[ancestor-or-self::header]"&gt;
            &lt;xsl:element name="bb:{name()}"&gt;
               &lt;xsl:apply-templates select="node()|@*" /&gt;
            &lt;/xsl:element&gt;
         &lt;/xsl:template&gt;
         &lt;xsl:template match="//Address"&gt;
            &lt;xsl:element name="aa:{local-name()}"&gt;
               &lt;xsl:for-each select="//Address/descendant::*"&gt;
                  &lt;xsl:element name="addr:{name()}"&gt;
                     &lt;xsl:apply-templates select="node()|@*" /&gt;
                  &lt;/xsl:element&gt;
               &lt;/xsl:for-each&gt;
            &lt;/xsl:element&gt;
         &lt;/xsl:template&gt;
         &lt;xsl:template match="comment() | processing-instruction()"&gt;
            &lt;xsl:copy /&gt;
         &lt;/xsl:template&gt;
      &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p><strong>Required Output XML</strong></p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:aa="xyz.com/request" xmlns:bb="xyz.com/header"
  xmlns:addr="xyz.com/address"&gt;
      &lt;soapenv:Header/&gt;
      &lt;soapenv:Body&gt;
          &lt;aa:request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
              &lt;bb:header&gt;
                  &lt;bb:sender&gt;
                      &lt;bb:User&gt;ServiceUser&lt;/bb:User&gt;
                      &lt;bb:userid type="UserId"&gt;ServiceUserid&lt;/bb:userid&gt;
                  &lt;/bb:sender&gt;
                  &lt;bb:receiver&gt;
                      &lt;bb:Country&gt;IND&lt;/bb:Country&gt;
                  &lt;/bb:receiver&gt;               
                  &lt;bb:id&gt;123456&lt;/bb:id&gt;
              &lt;/bb:header&gt;
              &lt;aa:Address type="physical"&gt;
                  &lt;addr:subaddress&gt;
                      &lt;number&gt;2&lt;/number&gt;
                  &lt;/addr:subaddress&gt;
                  &lt;addr:doornumber&gt;13&lt;/addr:doornumber&gt;
                  &lt;addr:streetName&gt;mgr road&lt;/addr:streetName&gt;
                  &lt;addr:locality&gt;cityname&lt;/addr:locality&gt;
                  &lt;addr:postalcode&gt;612453&lt;/addr:postalcode&gt;
              &lt;/aa:Address&gt;
          &lt;/aa:request&gt;
      &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p><strong>Incorrect Ouput XML:</strong></p>
  
  <pre><code> &lt;soapenv:Envelope xmlns:aa="xyz.com/aa"  
    xmlns:bb="xyz.com/bb"  xmlns:addr="xyz.com/addr"  
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
         &lt;soapenv:Header/&gt;
         &lt;soapenv:Body&gt;
            &lt;aa:request&gt;
               &lt;bb:header&gt;
                 &lt;!-- Correct output here --&gt;
               &lt;/bb:header&gt;
               &lt;aa:Address&gt;
                  &lt;addr:subaddress&gt;
                     &lt;number/&gt; &lt;!-- this element is empty -should have value "2" --&gt;
                  &lt;/addr:subaddress&gt;
                  &lt;addr:number&gt;2&lt;/addr:number&gt; &lt;!-- new element added which is incorrect--&gt;
                  &lt;addr:doornumber&gt;13&lt;/addr:doornumber&gt;
                  &lt;addr:streetName&gt;mgr road&lt;/addr:streetName&gt;
                  &lt;addr:locality&gt;cityname&lt;/addr:locality&gt;
                  &lt;addr:postalcode&gt;612453&lt;/addr:postalcode&gt;
               &lt;/aa:Address&gt;
            &lt;/aa:request&gt;
         &lt;/soapenv:Body&gt;
      &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>Can you someone please help me with the correct xslt to produce the expected result. Thank you in advance.</p>