 <p>If you change</p> <pre><code>&lt;xsl:template match="order"&gt; &lt;order job_id="{job_id}" site_code="{site_code}" replace="{replace}"&gt; &lt;xsl:apply-templates/&gt; &lt;/order&gt; &lt;/xsl:template&gt; </code></pre> <p>to</p> <pre><code>&lt;xsl:template match="order"&gt; &lt;order job_id="{job_id}" site_code="{site_code}" replace="{replace}"&gt; &lt;xsl:apply-templates select="node()[not(self::job_id|self::site_code|self::replace)"/&gt; &lt;xsl:copy-of select="following-sibling::master_version[1]"/&gt; &lt;/order&gt; &lt;/xsl:template&gt; </code></pre> <p>and then add <code>&lt;xsl:template match="master_version"/&gt;</code> you get the nesting, assuming it should be done for the following sibling <code>master_version</code> element and not based on some reference value.</p>