using xsl:for-each-group
===

Here is my requirement. My sample input document is like below. (I have added white lines to make it clear)

    <body>
           <p name="h-title" other="main">Introduction</p>
           <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>
    
    
           <p name=""> XSLT is used to write stylesheets.</p>
    	<p name="section-title" other=" other-section">XSLT</p>
    	<p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    	<p name=""> Some text.</p>
    	<p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
    	<p name="h1-title" other=" other-h1">XSLT</p>
    	<p name=""> Some text.</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-title" other=" other-section">XSLT</p>
    	<p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    	<p name=""> Some text.</p>
    	<p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
    	<p name="h1-title" other=" other-h1">XSLT</p>
    	<p name=""> Some text.</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 ="summary-title">this is summary</p>
           <p name="summary-desc " other="other-summarydesc">the summary</p>
        </body>

Now my wanted output is this.

    <body>
           <p name="h-title" other="main">Introduction</p>
           <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>
    
    
           <p name=""> XSLT is used to write stylesheets.</p>
    
        <body-contents>
        	<p name="section-title" other=" other-section">XSLT</p>
        	<p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        	<p name=""> Some text.</p>
        	<p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </p>
        	<h1>
        	<p name="h1-title" other=" other-h1">XSLT</p>
        	<p name=""> Some text.</p>
        	<h2>
               <p name="h2-title " name="other-h2">XQuery</p>
               <p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </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>
        </body-contents>
        <body-contents>
        	<p name="section-title" other=" other-section">XSLT</p>
        	<p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        	<p name=""> Some text.</p>
        	<p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </p>
        	<h1>
        	<p name="h1-title" other=" other-h1">XSLT</p>
        	<p name=""> Some text.</p>
        	<h2>
               <p name="h2-title " name="other-h2">XQuery</p>
               <p name="">
                  <p1 name="bold"> XQuery is used to query XML databases.</p1>
               </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>
        
        </body-contents>
        <body-contents>
               <p name ="summary-title">this is summary</p>
               <p name="summary-desc " other="other-summarydesc">the summary</p>
        </body-contents>
        </body>

Please help me solve this problem. There are restrictions like: 
h1, h2, h3 come sequentially(that means, h3 does not come between h1 and h2) and 
lines with name="section-title" should come before name="section-desc"
h1, h2, h3, etc. should come after section-desc.
I solved the problem of h1,h2,h3,etc [here][1]. I know this is very had. Any help is great.


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