title: How to insert an element into a previously created element in xslt?
body: <p>I have several records from the DB for a corresponding record in a file.</p>
  
  <p>Example
  Record no.    XML</p>
  
  <ol>
  <li><code>&lt;XML_FILE_HEADER file_name="sample.txt" /&gt;</code> </li>
  <li><code>&lt;XML_RECORD record_number="1" name="John Doe" Age="21"/&gt;</code> </li>
  <li><code>&lt;XML_RECORD record_number="2" name""Jessica Sanchez" Age="23"/&gt;</code></li>
  <li><code>&lt;XML_FILE_FOOTER total_records="2"/&gt;</code></li>
  </ol>
  
  <p>Now for each record I have an xslt template that would create the output file in xml.</p>
  
  <p>For record no 1:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt"&gt;
  &lt;xsl:output method="xml"/&gt;
  &lt;xsl:template match="XML_FILE_HEADER"&gt;
      &lt;xsl:element name="File"&gt;
      &lt;xsl:attribute name="FileName"&gt;&lt;xsl:value-of select="@file_name"/&gt;&lt;/xsl:attribute&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>For records 2 and 3:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt"&gt;
       &lt;xsl:output omit-xml-declaration="yes"/&gt;
       &lt;xsl:template match="XML_RECORD"&gt;
         &lt;xsl:element name="Record"&gt;
          &lt;xsl:attribute name="Name"&gt;&lt;xsl:value-of select="@name"/&gt;&lt;/xsl:attribute&gt;
          &lt;xsl:element name="Details"&gt;
          &lt;xsl:attribute name="Age"&gt;&lt;xsl:value-of select="@Age"/&gt;&lt;/xsl:attribute&gt;
          &lt;/xsl:element&gt;
        &lt;/xsl:element&gt;
       &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>For record 4:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt"&gt;
       &lt;xsl:output omit-xml-declaration="yes"/&gt;
       &lt;xsl:template match="XML_FILE_FOOTER"&gt;
         &lt;xsl:element name="Totals"&gt;
          &lt;xsl:attribute name="Total Records"&gt;&lt;xsl:value-of select="@total_records"/&gt;&lt;/xsl:attribute&gt;
        &lt;/xsl:element&gt;
       &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The problem with is is I would have an output of this after appending each record using the templates above:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;File FileName="sample.txt"&gt;&lt;/File&gt;
  &lt;Record Name="John Doe" Age="21"&gt;&lt;/Record&gt;
  &lt;Record Name="Jessica Sanchez" Age="22"&gt;&lt;/Record&gt;
  &lt;Totals Total Records="2"&gt;&lt;/Totals&gt;
  </code></pre>
  
  <p>How would I be able to insert the Record and Totals elements under File? so that it would have an output like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;File FileName="sample.txt"&gt;
  &lt;Record Name="John Doe" Age="21"&gt;&lt;/Record&gt;
  &lt;Record Name="Jessica Sanchez" Age="22"&gt;&lt;/Record&gt;
  &lt;Totals Total Records="2"&gt;&lt;/Totals&gt;
  &lt;/File&gt;
  </code></pre>
  
  <p>Any help would be very much appreciated. Thanks.</p>