title: How to eliminate xmlns="" entries produced by XSLT transform of one XML doc to another XML doc
body: <p>Ok, I've seen numerous variations on this question, but none exactly answer what I'm trying to solve and perhaps I'm just too dense to see how to apply one of the other answers to what I'm trying to do.</p>
  
  <p>I have some XML that looks something like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;message&gt;
    &lt;cmd id="api_info"&gt;
      &lt;api-version&gt;1.0&lt;/api-version&gt;
      &lt;api-build&gt;1.0.0.0&lt;/api-build&gt;
    &lt;/cmd&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>Now I have an XSLT transform that I'm applying to this XML.  The XSLT is similar to the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:fo="http://www.w3.org/1999/XSL/Format"
                  xmlns:xs="http://www.w3.org/2001/XMLSchema"
                  xmlns:fn="http://www.w3.org/2005/xpath-functions"
                  version="2.0"&gt;
  
      &lt;xsl:output method="xml" version="1.0" indent="yes"/&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:apply-templates select="message"/&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="message"&gt;
          &lt;xsl:element name="message" xmlns="http://www.companyname.com/schemas/product/Version001"&gt;
              &lt;xsl:apply-templates select="/message/cmd/@id"/&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="/message/cmd/@id"&gt;
          &lt;xsl:variable name="_commandType" select="/message/cmd/@id"/&gt;
          &lt;xsl:element name="messageHeader"&gt;
              &lt;xsl:element name="cmdType"&gt;
                  &lt;xsl:value-of select="$_commandType"/&gt;
              &lt;/xsl:element&gt;
          &lt;/xsl:element&gt;
  
          &lt;xsl:element name="messageBody"&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="$_commandType = 'api_info'"&gt;
                      &lt;xsl:element name="apiInfoBody"&gt;
                          &lt;xsl:element name="apiVersion"&gt;
                              &lt;xsl:value-of select="/message/cmd/api-version"/&gt;
                          &lt;/xsl:element&gt;
                          &lt;xsl:element name="apiBuild"&gt;
                              &lt;xsl:value-of select="/message/cmd/api-build"/&gt;
                          &lt;/xsl:element&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:when test="$_commandType = 'communicationError'"&gt;
                      &lt;xsl:element name="communicationErrorBody"&gt;
                          &lt;xsl:element name="errorCode"&gt;
                              &lt;xsl:value-of select="error-code"/&gt;
                          &lt;/xsl:element&gt;
                          &lt;xsl:element name="badCmd"&gt;
                              &lt;xsl:value-of select="bad-cmd"/&gt;
                          &lt;/xsl:element&gt;
                      &lt;/xsl:element&gt;
                  &lt;/xsl:when&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The output I get is basically what I want and looks like the following:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;message xmlns="http://www.companyname.com/schemas/product/Version001"&gt;
      &lt;messageHeader xmlns=""&gt;
          &lt;cmdType&gt;api_info&lt;/cmdType&gt;
      &lt;/messageHeader&gt;
      &lt;messageBody xmlns=""&gt;
          &lt;apiInfoBody&gt;
              &lt;apiVersion&gt;1.0&lt;/apiVersion&gt;
              &lt;apiBuild&gt;1.0.0.0&lt;/apiBuild&gt;
          &lt;/apiInfoBody&gt;
      &lt;/messageBody&gt;
  &lt;/message&gt;
  </code></pre>
  
  <p>But what I don't want are the <b>xmlns=""</b> attributes in the <b>&lt;messageHeader&gt;</b> and <b>&lt;messageBody&gt;</b> elements.</p>
  
  <p>Now I've found that if I explicitly specify the namespace in the XSLT for those elements, then the unwanted attribute gets pushed down one level to the children of those attributes.</p>
  
  <p>I could just go through my entire XSLT and explicitly add the <b>xmlns=""<a href="http://www.companyname.com/schemas/product/Version001" rel="noreferrer">http://www.companyname.com/schemas/product/Version001</a>"</b> attribute to each of my <b>xsl:element</b> definitions, but I know that there must be a more elegant way.  We programmers are far too lazy to not have a shortcut for this kind of nonsense.  If my XSLT didn't consist of something as simple as the shortened example, I be tempted to do it that way.  But I know there must be a better way.</p>
  
  <p>Does anyone know what I'm missing here?</p>
  
  <p>Thanks,</p>
  
  <p>AlarmTripper</p>