Apply a template between two specific nodes in XSLT
===

I had a problem in xsl:for-each-group and it was very nicely solved in [here][1]. Now I have some other problem. I have like this as input.

    <?xml version="1.0" encoding="UTF-8"?>
    
        <body>
           <p name ="section">this is section</p>
           <p name="h-title" other="main">Introduction</p>
           <p name="h1-title " other="other-h1">XSLT and XQuery</p>
           <p name="h2-title" other=" other-h2">XSLT</p>
           <p name="">
              <p1 name="bold"> XSLT is used to write stylesheets.</p1>
           </p>
           <p name="h2-title " name="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
           <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
           <p name ="section">this is section</p>
           <p name="h1-title " other="other-h1">XSLT and XQuery</p>
           <p name="h2-title " other=" other-h2">XSLT</p>
           <p name ="section">this is section</section>
           <p name="h1-title " other="other-h1">XSLT and XQuery</p>
           <p name="h2-title " other=" other-h2">XSLT</p>
        </body>

**Now my wanted output is this**

    <?xml version="1.0" encoding="UTF-8"?>
    <body>
       <p name="h-title " other="main">Introduction</p>
       <section>
       <p name ="section">this is section</p>
       <h1>
          <p name="h1-title " other="other-h1"> XSLT and XQuery </p>
          <h2>
             <p name="h2-title " other="other-h2">XSLT</p>
             <p name="">
                <p1 name="bold">XSLT is used to write stylesheets.
                </p1>
             </p>
          </h2>
          <h2>
             <p name="h2-title " other="other-h2"> XQuery is used to query XMLdatabases    
             </p>
             <p name="">
                <p name="bold"> XQuery is used to query XML databases.</p>
             </p>
             <h3>
                <p name="h3-title " name="other-h3">XQuery and stylesheets</p>
                <p name="">
                <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </p>
            </h3>
          </h2>
    </h1>
    </section>
    <section>
    <p name ="section">this is section</p>
    <h1>
                <p name="h1-title " other="other-h1">XSLT and XQuery</p>
           <h2>   
                <p name="h2"-title other=" other-h2">XSLT</p>
           </h2>
    </h1>
    </section>
    <section>
    <p name ="section">this is section</p>
    <h1>
                <p name="h1-title " other="other-h1">XSLT and XQuery</p>
           <h2>   
                <p name="h2"-title other=" other-h2">XSLT</p>
           </h2>
    </h1>
    </section>
    </body>

**My used stylesheet(not working properly)**

        <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:mf="http://example.com/mf"
          exclude-result-prefixes="xs mf">
        
        <xsl:param name="prefix" as="xs:string" select="'h'"/>
        <xsl:param name="suffix" as="xs:string" select="'-title'"/>
        
        <xsl:output method="html" version="4.0" indent="yes"/>
        
        <xsl:function name="mf:group" as="node()*">
          <xsl:param name="items" as="node()*"/>
          <xsl:param name="level" as="xs:integer"/>
          <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix,$level, $suffix)]">
            <xsl:choose>
              <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
                <xsl:apply-templates select="current-group()"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:element name="h{$level}">
                  <xsl:apply-templates select="."/>
                  <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
                </xsl:element>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:for-each-group>
        </xsl:function>
        
        <xsl:template match="@* | node()">
          <xsl:copy>
            <xsl:apply-templates select="@* , node()"/>
          </xsl:copy>
        </xsl:template>
        
        <xsl:template match="body" name ="myTemplate">
          <xsl:copy>
            <xsl:sequence select="mf:group(*, 1)"/>
          </xsl:copy>
        </xsl:template>
    
       <xsl:template match="body[@name='section']">
            <section>
                <xsl:call-template name="myTemplate"/>
            </section>
        </xsl:template>    
        </xsl:stylesheet>

But this is not correct. Things like `<p name ="section">this is section</p>` appear widely. not only that, there are few others like this. If someone please show me how to handle with `section` I will be able to do that for others too. Please tell me how to do this correctly. 


  [1]: http://stackoverflow.com/questions/11585371/xslfor-each-group-help-needed



