title: Move elements to different place with XSLT
body: <p>I have successfully created an XSLT template to copy some elements, changing the names of some elements, from one XML file to another.</p>
  
  <p>I can't figure out, though, how to take elements and move them to a different part of the XML structure.</p>
  
  <p>I want to convert this XML:</p>
  
  <pre><code>&lt;Hosts&gt;
      &lt;Clusters&gt;
          &lt;Cluster&gt;
              &lt;Nodes&gt;
                  &lt;WindowsHost/&gt;
              &lt;/Nodes&gt;
          &lt;/Cluster&gt;
      &lt;/Clusters&gt;
  &lt;/Hosts&gt;
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>&lt;Hosts&gt;
      &lt;WindowsHosts&gt;
          &lt;WindowsHost/&gt;
      &lt;/WindowsHosts&gt;
  &lt;/Hosts&gt;
  </code></pre>
  
  <p>My current working XSLT contains:</p>
  
  <pre><code>&lt;xsl:template match="/"&gt;
      &lt;xsl:apply-templates select="*"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="*"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="*"&gt;
      &lt;xsl:apply-templates select="*"/&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>...and then repeating templates like:</p>
  
  <pre><code>&lt;xsl:template match="/Hosts/Clusters/Cluster/Nodes/WindowsHost"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/Hosts/Clusters/Cluster/Nodes/WindowsHost/SomeElement"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>and so on.  Each element to be copied has its own template, since not all elements are copied and some element names are changed.  But I have only succeeded in changing the element name, not the complete XPath.</p>
  
  <p>Any help would be appreciated.</p>
  
  <p>Paul</p>
  
  <hr>
  
  <p>Your response is much appreciated.  But I think my example was too simple, in an attempt to be clear.</p>
  
  <p>My existing XSLT has a template for every element needed to be copied from one XML to the other, so there are lots of templates (almost 1000) and the beginning of the XSLT file makes sure nothing happens to any element by default.  Long story short, I tried the techniques in your answer with no success, probably because the situation is not exactly as I showed you.</p>
  
  <p>There are three scenarios for elements being copied:</p>
  
  <ol>
  <li>Copy as is</li>
  <li>Copy but change the element name</li>
  <li>Copy to a different location in the schema</li>
  </ol>
  
  <p>The existing XSLT file works with both #1 and #2. It's #3 I can't get to work. Here is more of the XSLT file</p>
  
  <pre><code>&lt;xsl:template match="/"&gt;
      &lt;xsl:apply-templates select="*"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/*"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="*"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="*"&gt;
      &lt;xsl:apply-templates select="*"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/SAN/ClientProfile"&gt;
  &lt;!-- copy element as is (working) --&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/SAN/ClientProfile/Name"&gt;
  &lt;!-- copy element but change its name (working) --&gt;
      &lt;CompanyName&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/CompanyName&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/SAN/EQLHosts/WindowsClusters/Cluster/ClusterNodes/WindowsHost"&gt;
  &lt;!-- copy to different part of schema (not working) --&gt;
      &lt;WindowsHost&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/WindowsHost&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>So, I want to change <code>/SAN/EQLHosts/WindowsClusters/Cluster/ClusterNodes/WindowsHost</code> to <code>/SAN/EQLHosts/WindowsHosts/WindowsHost</code>.  All children of this element would be handled in the same way as the elements that are already being copied.</p>
  
  <p>I hope my clarifications are clear.  Please let me know if this information changes your answer or whether I am just being dense.</p>
  
  <p>Paul</p>