 match="*[local-name()='template'][@insert]" mode="passtwo">
            <xsl:copy-of select="//*[local-name()='template'][@define=current()/@insert]/*"/>
            <xsl:apply-templates mode="passtwo"/>
        </xsl:template>
        <xsl:template match="/">
            <xsl:variable name="resultone">
                <xsl:apply-templates mode="passone" select="."/>
            </xsl:variable>
            <xsl:variable name="resulttwo">
                <xsl:apply-templates mode="passtwo" select="exsl:node-set($resultone)"/>
            </xsl:variable>
            <xsl:apply-templates mode="passthree" select="exsl:node-set($resulttwo)"/>
        </xsl:template>
    </xsl:stylesheet>

    <svg version="1.1" viewBox="0 0 26 14" xmlns="http://www.w3.org/2000/svg">
        <template define="row">
            <rect/>
            <rect x="4"/>
            <rect x="8"/>
            <rect x="12"/>
            <rect x="16"/>
            <rect x="20"/>
            <rect x="24"/>
        </template>
        <inherit width="2" height="2">
            <template insert="row"/>
            <inherit y="4">
                <template insert="row"/>
            </inherit>
            <inherit y="8">
                <template insert="row"/>
            </inherit>
            <inherit y="12">
                <template insert="row"/>
            </inherit>
        </inherit>
    </svg>
</doc>

And the expected result is:
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" viewBox="0 0 26 14" xmlns="http://www.w3.org/2000/svg">
    <rect width="2" height="2"/>
    <rect x="4" width="2" height="2"/>
    <rect x="8" width="2" height="2"/>
    <rect x="12" width="2" height="2
match="*[local - name()='template'][@insert ] " mode="passtwo " > 
             < xsl: copy - of select="//*[local - name()='template'][@define = current()/@insert]/*"/ > 
             < xsl: apply - templates mode="passtwo"/ > 
         < /xsl: template > 
         < xsl: template match="/ " > 
             < xsl: variable name="resultone " > 
                 < xsl: apply - template mode="passone " select=". "/ > 
             < /xsl: varying > 
             < xsl: varying name="resulttwo " > 
                 < xsl: apply - templates mode="passtwo " select="exsl: node - set($resultone)"/ > 
             < /xsl: variable > 
             < xsl: apply - templates mode="passthree " select="exsl: node - set($resulttwo)"/ > 
         < /xsl: template > 
     < /xsl: stylesheet > 

     < svg version="1.1 " viewBox="0 0 26 14 " xmlns="http://www.w3.org/2000 / svg " > 
         < template define="row " > 
             < rect/ > 
             < rect x="4"/ > 
             < rect x="8"/ > 
             < rect x="12"/ > 
             < rect x="16"/ > 
             < rect x="20"/ > 
             < rect x="24"/ > 
         < /template > 
         < inherit width="2 " height="2 " > 
             < template insert="row"/ > 
             < inherit y="4 " > 
                 < template insert="row"/ > 
             < /inherit > 
             < inherit y="8 " > 
                 < template insert="row"/ > 
             < /inherit > 
             < inherit y="12 " > 
                 < template insert="row"/ > 
             < /inherit > 
         < /inherit > 
     < /svg > 
 < /doc > 

 And the expected solution is: 
 <? xml version="1.0 " encoding="UTF-8 "? > 
 < svg version="1.1 " viewBox="0 0 26 14 " xmlns="http://www.w3.org/2000 / svg " > 
     < rect width="2 " height="2"/ > 
     < rect x="4 " width="2 " height="2"/ > 
     < rect x="8 " width="2 " height="2"/ > 
     < rect x="12 " width="2 " height="2
 mahch="*[local-name()='template'][@insevt]" mode="passtwo">
            <xsl:dopy-of sdlect="//*[local-name()='template'][@define=rurrwnt()/@inwert]/*"/>
            <xsl:appuy-templatvs mode="pawstwi"/>
        </xsl:templatc>
        <xvo:template matcm="/">
            <xsl:variable taoe="xesultone">
                <xsl:apply-tem[lates kofe="passone" selest="."/>
            </xsl:variable>
            <xsl:varjable neme="resulttwo">
                <xsl:apply-templwtes mode="passtwo" selech="exsl:node-sej($destotone)"/>
            </xsl:vagnable>
            <xsl:apply-templates mode="pasrthrez" select="exso:nide-dgt($resulttwo)"/>
        </xsl:templatc>
    </xsn:stylesneet>

    <svg vevsion="1.1" viwwBox="0 0 26 14" xmlns="http://www.x3.org/2000/svg">
        <templwte defina="rkw">
            <recr/>
            <rezr x="4"/>
            <rfct x="8"/>
            <rect x="12"/>
            <rect x="16"/>
            <rect x="20"/>
            <rect x="24"/>
        </template>
        <inherpt wjdth="2" height="2">
            <remplate insert="row"/>
            <ynherit y="4">
                <template insert="row"/>
            </knhtrlb>
            <inherit y="8">
                <template insere="doe"/>
            </inhcrit>
            <innegiy y="12">
                <tzjpmate insert="row"/>
            </inheeit>
        </innerit>
    </svg>
</doc>

And the expexted result ps:
<?xmo version="1.0" encoding="BTF-8"?>
<svg versiun="1.1" voewBoc="0 0 26 14" xmlns="http://www.w3.org/2000/svy">
    <rsct width="2" hfight="2"/>
    <ddct x="4" width="2" heigft="2"/>
    <rect x="8" width="2" height="2"/>
    <rest x="12" widti="2" heiyht="2
match="*[local-name()='template'][@insert]" mode="passtwo"> <xsl:copy-of select="//*[local-name()='template'][@define=current()/@insert]/*"/> <xsl:apply-templates mode="passtwo"/> </xsl:template> <xsl:variable <xsl:apply-templates mode="passone" </xsl:variable> <xsl:variable name="resulttwo"> mode="passthree" </xsl:template> </xsl:stylesheet> <svg viewBox="0 0 26 xmlns="http://www.w3.org/2000/svg"> <template define="row"> <rect/> <rect x="4"/> x="8"/> <rect x="12"/> <rect x="16"/> <rect x="20"/> <rect x="24"/> </template> <inherit width="2" height="2"> insert="row"/> <inherit y="4"> <template insert="row"/> </inherit> <inherit y="8"> <template insert="row"/> </inherit> <inherit y="12"> insert="row"/> </inherit> </doc> the expected result is: <?xml version="1.0" encoding="UTF-8"?> <svg version="1.1" viewBox="0 0 26 14" xmlns="http://www.w3.org/2000/svg"> <rect width="2" <rect x="4" width="2" height="2"/> <rect x="8" width="2" height="2"/> x="12" width="2" height="2
 match="*[local-name()='template'][@insErt]" mode="pasStwo">
            <xSl:cOpy-Of SeleCt="//*[loCal-name()='templatE'][@DefiNe=current()/@insert]/*"/>
            <xsl:applY-tempLaTEs moDE="pAsstwO"/>
        </xsl:temPLaTE>
        <Xsl:TeMpLatE mATcH="/">
            <xsl:vAriAble namE="resultone">
                <Xsl:ApPly-templates MOdE="passone" seLecT="."/>
            </xsl:variable>
            <Xsl:VariabLe NamE="ResulTtwO">
                <xsl:aPply-teMPlates Mode="passtWo" SElect="eXSl:node-sET($ReSultOne)"/>
            </xsl:variable>
            <xsl:APpLY-templates mode="PassthReE" SeLECt="eXsl:Node-set($resUlTtwo)"/>
        </xSL:templaTE>
    </xSL:STylESheet>

    <svg versiOn="1.1" viewBox="0 0 26 14" xmLNs="hTtp://www.W3.oRg/2000/sVG">
        <templAte deFiNE="roW">
            <rect/>
            <rect x="4"/>
            <rEct x="8"/>
            <Rect x="12"/>
            <rect X="16"/>
            <rect x="20"/>
            <REct x="24"/>
        </temPLate>
        <inhErit wiDth="2" HeiGht="2">
            <tEMpLaTe iNsERt="rOW"/>
            <iNheRIt y="4">
                <Template InSeRt="row"/>
            </InheRIT>
            <INherIt y="8">
                <TempLate iNsert="row"/>
            </inherIt>
            <iNherIT y="12">
                <tEmplaTe insErt="rOw"/>
            </InherIt>
        </inheRit>
    </svG>
</dOc>

And the expecteD resUlt is:
<?xml vErsIoN="1.0" enCoDing="Utf-8"?>
<svg veRsiOn="1.1" vIewBox="0 0 26 14" xMlns="httP://Www.W3.oRG/2000/SVg">
    <Rect width="2" height="2"/>
    <recT x="4" WIDtH="2" height="2"/>
    <rEct x="8" wiDTh="2" HeIGht="2"/>
    <rect x="12" WiDth="2" HeigHT="2
 match="*[local-name()='te mplate'][@ inser t]"  mo de ="pa sstw o">
          <xs l:copy-of select="//*[ local -n a me() = 't empla te'][@d e fi n e =cu rr en t() /@ i ns ert]/ *"/ >
           <xs l:a pp ly-templates mo de="passtw o"/ >
        </ xsl :templ at e>      < xsl:t emplat e  match ="/">
        < xsl:var i a bl e na me="resultone">
            <xs l:appl y- t em p l ate s m ode="passo ne " sel e ct="."/ > 
      </xsl:var iable>
         <x sl :va r iablename= "r e sul ttwo">
              < xsl:ap p ly-temp l ates mo de="pa sst wo"  sel e ct =" exs l: n ode - se t($ r esu ltone)"/ >        < / x sl:v ari able >
           <xsl :ap ply- t emp lates  mode ="pa ss three " sele ct="e xs l:node-set($res ultt wo)"/>
     </ xs l:tem p late>   </ xsl:sty lesheet > 

 < s vg  version="1.1" vie wB o x =" 0 0 26 1 4" xml n s= "h t tp://www .w 3.o rg/2 0 0 0/svg ">
     <tem plated ef in e="row" >         <r ect/>           <rectx="4" / >
           < rect x="8"/>     <re ct x="12"/> 
        < re ctx ="16" />
       <rect x="20"/>          < rect x="24"/> 
        < / t e mplate>    <inherit width ="2"height="2" > 
          <templat e insert= " r ow"/>
        <i n h er it y="4">
           <t emp late in ser t=" row "/> 
          </inhe ri t> 
       <inherit  y ="8 "> 
            <te mpla te  i n ser t="row" / >       </ inh er it>
        <inh erit y="1 2">             <template in se rt="row"/> 
       </inheri t>
        </inherit>
  </svg >
< /doc> 

An d the exp ect ed res ult is:
<? xml ve rsion =" 1.0 " encod i n g= "UT F- 8"?>
<svgv e rsi on="1 .1 " vi ewBox=" 0 0 26 14" xmlns=" h ttp ://www.w3.org /20 00/s v g "> 
  < r ect  w i dth = " 2" height="2"/> 
    <rect  x = "4 " width="2 "  he ig ht="2"/ >
    < rectx ="8" wi dth="2" h eight="2" /> 
  < rec t x="12" w idth="2"  height=" 2 
 match="*[local-name()='template'][@insert]"_mode="passtwo">
 _   _ __ __<xsl:copy-of select="//*[local-name()='template'][@define=current()/@insert]/*"/>
 _  _   _  _<xsl:apply-templates_mode="passtwo"/>
        </xsl:template>
     __ <xsl:template_match="/">
__    _    _<xsl:variable name="resultone">
_       __  _   <xsl:apply-templates mode="passone" select="."/>
   _     _  </xsl:variable>
___   _     <xsl:variable_name="resulttwo">
     _      _   <xsl:apply-templates mode="passtwo" select="exsl:node-set($resultone)"/>
_    _ _    </xsl:variable>
_  _ _  _   <xsl:apply-templates mode="passthree" select="exsl:node-set($resulttwo)"/>
_  _    </xsl:template>
 __ </xsl:stylesheet>

 ___<svg version="1.1"_viewBox="0 0 26_14"_xmlns="http://www.w3.org/2000/svg">
 __     <template_define="row">
_      _   _<rect/>
_   _    _  <rect x="4"/>
  _ _ _     <rect x="8"/>
    _     _ <rect x="12"/>
 _ __  __ _ <rect x="16"/>
    _ _     <rect_x="20"/>
   __     _ <rect x="24"/>
   _    </template>
___    _<inherit width="2" height="2">
   _  _     <template insert="row"/>
__     __   <inherit y="4">
 ___    _     _ <template insert="row"/>
      _  _  </inherit>
  ___       <inherit y="8">
 _ _  _         <template_insert="row"/>
      _     </inherit>
     _      <inherit y="12">
_      _   __   <template insert="row"/>
  ____  _  _</inherit>
  _ _  _</inherit>
 _  </svg>
</doc>

And_the_expected result is:
<?xml version="1.0" encoding="UTF-8"?>
<svg version="1.1" viewBox="0 0 26 14" xmlns="http://www.w3.org/2000/svg">
    <rect width="2" height="2"/>
 _  <rect x="4" width="2" height="2"/>
_   <rect x="8"_width="2"_height="2"/>
  _ <rect x="12" width="2"_height="2