title: Grouping several groups in XSLT 2
body: <p>I'm trying to add hierarchy to some grotty extruded typesetting XML. I can't seem to manage grouping several kinds of groups in the same parent element at once.</p>
  
  <p>What I have (simplified, obviously):</p>
  
  <pre><code>&lt;article&gt;
    &lt;h1&gt;A section title here&lt;/h1&gt;
    &lt;p&gt;A paragraph.&lt;/p&gt;
    &lt;p&gt;Another paragraph.&lt;/p&gt;
    &lt;bl&gt;Bulleted list item.&lt;/bl&gt;
    &lt;bl&gt;Another bulleted list item.&lt;/bl&gt;
    &lt;h1&gt;Another section title&lt;/h1&gt;
    &lt;p&gt;Yet another paragraph.&lt;/p&gt;
  &lt;/article&gt;
  </code></pre>
  
  <p>What I want:</p>
  
  <pre><code>&lt;article&gt;
    &lt;sec&gt;
      &lt;h1&gt;A section title here&lt;/h1&gt;
      &lt;p&gt;A paragraph.&lt;/p&gt;
      &lt;p&gt;Another paragraph.&lt;/p&gt;
      &lt;list&gt;
        &lt;list-item&gt;Bulleted list item.&lt;/list-item&gt;
        &lt;list-item&gt;Another bulleted list item.&lt;/list-item&gt;
      &lt;/list&gt;
    &lt;/sec&gt;
    &lt;sec&gt;
      &lt;h1&gt;Another section title&lt;/h1&gt;
      &lt;p&gt;Yet another paragraph.&lt;/p&gt;
    &lt;/sec&gt;
  &lt;/article&gt;
  </code></pre>
  
  <p>This almost works for the list items:</p>
  
  <pre><code>&lt;xsl:for-each-group select="*" group-adjacent="boolean(self::BL)"&gt;
     &lt;xsl:choose&gt;
        &lt;xsl:when test="current-grouping-key()"&gt;
           &lt;list&gt;&lt;xsl:apply-templates select="current-group()"/&gt;&lt;/list&gt;
        &lt;/xsl:when&gt;
        &lt;xsl:otherwise&gt;
           &lt;xsl:apply-templates select="current-group()"/&gt;
              &lt;/xsl:otherwise&gt;
     &lt;/xsl:choose&gt;
   &lt;/xsl:for-each-group&gt;
  </code></pre>
  
  <p>but it only handles the very first list in an article; and as soon as I try to add another xsl:for-each-group to cover the sections, the list-item one stops working.</p>
  
  <p>Ideas? Many thanks in advance!</p>