title: Copy child nodes and change element names with XSL
body: <p>I have this source XML from a vendor:</p>
  <h2>Source XML</h2>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;src:view name=&quot;books&quot;
      xmlns=&quot;xyz.com/wow&quot;
      xmlns:src=&quot;xyz.com/fun&quot; 
      xmlns:xsi=&quot;w3.org/2001/XMLSchema-instance&quot;&gt;
      &lt;books&gt;
          &lt;title&gt;Searching for Answers&lt;/title&gt;
      &lt;/books&gt;
      &lt;books&gt;
          &lt;title&gt;Get Rich Quick&lt;/title&gt;   
      &lt;/books&gt;
      &lt;books&gt;
          &lt;title&gt;Negotiating with the Grim Reaper&lt;/title&gt; 
      &lt;/books&gt;    
  &lt;/src:view&gt;
  </code></pre>
  <p>I want to transform into a new document with root element <code>books</code>, then copy all <code>books</code> elements but change the name from <code>books</code> to <code>books_record</code>. Documents will be arbitrary,(<em>books</em> today, <em>widgets</em> tomorrow, etc), but the element name for the root's children is always available from <code>/@name</code>, that's the vendors convention.</p>
  <h2>Intended Output</h2>
  <pre><code>&lt;books&gt;
     &lt;books_record&gt;
        &lt;title&gt;Searching for Answers&lt;/title&gt;
     &lt;/books_record&gt;
     &lt;books_record&gt;
        &lt;title&gt;Get Rich Quick&lt;/title&gt;
     &lt;/books_record&gt;
     &lt;books_record&gt;
        &lt;title&gt;Negotiating with the Grim Reaper&lt;/title&gt;
     &lt;/books_record&gt;
  &lt;/books&gt;
  </code></pre>
  <p>I managed the get right the root element and copying of all child elements, but I'm not sure how to change all <code>&lt;books&gt;</code> to <code>&lt;books_record&gt;</code>. Also, is what I have so far efficient and valid? I know it works, just not 100% sure if it's the best approach.</p>
  <h2>XSL so far (Updated with help from <a href="https://stackoverflow.com/users/3016153/michael-hor257k">michael.hor257k</a>)</h2>
  <pre><code>&lt;xsl:transform xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
      exclude-result-prefixes=&quot;xsl&quot; version=&quot;2.0&quot;&gt;
      &lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;
          indent=&quot;yes&quot; /&gt;
      &lt;xsl:strip-space elements=&quot;*&quot; /&gt;
  
  &lt;xsl:template match=&quot;*&quot;&gt;
      &lt;xsl:element name=&quot;{local-name()}&quot;&gt;
          &lt;xsl:copy-of select=&quot;@*&quot; /&gt;
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;/*&quot;&gt;
      &lt;xsl:variable name=&quot;rootElementName&quot;&gt;
          &lt;xsl:value-of select=&quot;@name&quot; /&gt;
      &lt;/xsl:variable&gt;     
      
      &lt;xsl:element name=&quot;{$rootElementName}&quot;&gt;      
              &lt;xsl:apply-templates /&gt; 
      &lt;/xsl:element&gt;       
    
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match=&quot;/*/node()&quot;&gt;
      &lt;xsl:variable name=&quot;rootElementName&quot;&gt;
          &lt;xsl:value-of select=&quot;/*/@name&quot; /&gt;
      &lt;/xsl:variable&gt;
  
      &lt;xsl:element name=&quot;{$rootElementName}_record&quot;&gt;      
              &lt;xsl:apply-templates /&gt; 
      &lt;/xsl:element&gt;          
  &lt;/xsl:template&gt;
  &lt;/xsl:transform&gt;
  </code></pre>