title: Grouping XML elements using XSLT's for-each-group and group-starting-with attribute when the node set is stored in a variable
body: <p>I'm processing a source HTML file that holds tabular data in an unstructured way. Basically it's a bunch of absolutely positioned <code>div</code>s. My goal is to rebuild some sort of structured XML data. So far, using XSLT 2.0 I was able to produce an XML looking like this:</p>
  
  <pre><code>&lt;data&gt;
      &lt;line top="44"&gt;
           &lt;item left="294"&gt;Some heading text&lt;/item&gt;
      &lt;/line&gt;
      &lt;line top="47"&gt;
           &lt;item left="718"&gt;A&lt;/item&gt; &lt;!-- this item is a section-start --&gt;
           &lt;item left="764"&gt;Section heading&lt;/item&gt;
      &lt;/line&gt;
      &lt;line top="78"&gt;
          &lt;item left="92"&gt;Data&lt;/item&gt;
          &lt;item left="144"&gt;Data&lt;/item&gt;
          &lt;item left="540"&gt;Data&lt;/item&gt;
          &lt;item left="588"&gt;Data&lt;/item&gt;
      &lt;/line&gt;
      &lt;line top="101"&gt;
          &lt;item left="61"&gt;B&lt;/item&gt; &lt;!-- this item is a section-start --&gt;
          &lt;item left="144"&gt;Section heading&lt;/item&gt;
      &lt;/line&gt;
      &lt;line top="123"&gt;
          &lt;item left="92"&gt;Data&lt;/item&gt;
          &lt;item left="144"&gt;Data&lt;/item&gt;
      &lt;/line&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>However, what I need to do next is group lines into sections. Each section starts with a line whose first item's value consists of a single letter A – Z. My approach is to hold all the <code>&lt;line&gt;</code> elements in a <code>$lines</code> variable and then use <code>xsl:for-each-group</code> with <code>group-starting-with</code> attribute to identify the element starting a new section.</p>
  
  <p>The respective XSLT fragment looks like this:</p>
  
  <pre><code>&lt;xsl:for-each-group select="$lines/line" group-starting-with="...pattern here..."&gt;
      &lt;section&gt;
          &lt;xsl:copy-of select="current-group()"/&gt;
      &lt;/section&gt;
  &lt;/xsl:for-each-group&gt;
  </code></pre>
  
  <p>The problem is I can't figure out a working pattern to identify section starts. The best I could do was ensuring that <code>//line/item[1]/text()[matches(., '^[A-Z]$')]</code> works when used separately in an XPath evaluator. However, I can't seem to derive a working version to be used with <code>group-starting-with</code>.</p>
  
  <p><em>Update</em> Hence the wanted result should look like this:</p>
  
  <pre><code>&lt;data&gt;
      &lt;section&gt; &lt;!-- this section started automatically because of being at the beginning --&gt;
          &lt;line top="44"&gt;
               &lt;item left="294"&gt;Some heading text&lt;/item&gt;
          &lt;/line&gt;
      &lt;/section&gt;
      &lt;section&gt;
          &lt;line top="47"&gt;
               &lt;item left="718"&gt;A&lt;/item&gt; &lt;!-- this item is a section-start --&gt;
               &lt;item left="764"&gt;Section heading&lt;/item&gt;
          &lt;/line&gt;
          &lt;line top="78"&gt;
              &lt;item left="92"&gt;Data&lt;/item&gt;
              &lt;item left="144"&gt;Data&lt;/item&gt;
              &lt;item left="540"&gt;Data&lt;/item&gt;
              &lt;item left="588"&gt;Data&lt;/item&gt;
          &lt;/line&gt;
      &lt;/section&gt;
      &lt;section&gt;
          &lt;line top="101"&gt;
              &lt;item left="61"&gt;B&lt;/item&gt; &lt;!-- this item is a section-start --&gt;
              &lt;item left="144"&gt;Section heading&lt;/item&gt;
          &lt;/line&gt;
          &lt;line top="123"&gt;
              &lt;item left="92"&gt;Data&lt;/item&gt;
              &lt;item left="144"&gt;Data&lt;/item&gt;
          &lt;/line&gt;
      &lt;/section&gt;
  &lt;/data&gt;
  </code></pre>