title: XSLT - adding attribute dynamically in grouping
body: <p>I have a xml like follows,</p>
  
  <pre><code>&lt;doc&gt;
      &lt;section&gt;
          &lt;p id="main"&gt;aa&lt;/p&gt;
          &lt;p id="main"&gt;bb&lt;/p&gt;
          &lt;p id="main"&gt;cc&lt;/p&gt;
          &lt;p id="para1"&gt;dd&lt;/p&gt;
          &lt;p id="main"&gt;ee&lt;/p&gt;
          &lt;p id="main"&gt;ff&lt;/p&gt;
          &lt;p id="main"&gt;gg&lt;/p&gt;
          &lt;p id="para2"&gt;hh&lt;/p&gt;
          &lt;p id="main"&gt;ii&lt;/p&gt;
          &lt;p id="main"&gt;jj&lt;/p&gt;
          &lt;p id="para1"&gt;xx&lt;/p&gt;
          &lt;p id="main"&gt;yy&lt;/p&gt;
          &lt;p id="main"&gt;zz&lt;/p&gt;
      &lt;/section&gt;
  &lt;/doc&gt;
  </code></pre>
  
  <p>my task is group the above content based on <code>id="para1"</code> and <code>id='para2'</code> attributes and add a section to each group. My desired output is </p>
  
  <pre><code>&lt;doc&gt;
      &lt;section&gt;
          &lt;p id="main"&gt;aa&lt;/p&gt;
          &lt;p id="main"&gt;bb&lt;/p&gt;
          &lt;p id="main"&gt;cc&lt;/p&gt;
      &lt;/section&gt;
      &lt;section  type="First para"&gt;
          &lt;p id="para1"&gt;dd&lt;/p&gt;
          &lt;p id="main"&gt;ee&lt;/p&gt;
          &lt;p id="main"&gt;ff&lt;/p&gt;
          &lt;p id="main"&gt;gg&lt;/p&gt;
      &lt;/section&gt;
      &lt;section type="Second para"&gt;
          &lt;p id="para2"&gt;hh&lt;/p&gt;
          &lt;p id="main"&gt;ii&lt;/p&gt;
          &lt;p id="main"&gt;jj&lt;/p&gt;
      &lt;/section&gt;
      &lt;section  type="First para"&gt;
          &lt;p id="para1"&gt;xx&lt;/p&gt;
          &lt;p id="main"&gt;yy&lt;/p&gt;
          &lt;p id="main"&gt;zz&lt;/p&gt;
      &lt;/section&gt;
  &lt;/doc&gt;
  </code></pre>
  
  <p>The xsl to do this task is follows,</p>
  
  <pre><code>&lt;xsl:template match="@*|node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()"/&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="section"&gt;
          &lt;xsl:for-each-group select="p" group-starting-with="p[starts-with(@id, 'para')]"&gt;
              &lt;section type="?"&gt;
                  &lt;xsl:apply-templates select="current-group()"/&gt;
              &lt;/section&gt;      
          &lt;/xsl:for-each-group&gt;
      &lt;/xsl:template&gt;
  </code></pre>
  
  <p>This xsl will do the job but can you suggest a way that how can I add <code>type</code> attribute value of the adding <code>&lt;section&gt;</code> node based on the group?</p>