title: Group-adjacent nodes using xslt
body: <p>Questions concerning the group-adjacent function have already been asked, I know. For some reason though, I can't get this to work. </p>
  
  <p>I inherited  project at work that uses xslt and my boss doesn't appreciate how steep the learning curve is for learning xslt and how dissimilar it is from procedural programming.  </p>
  
  <p>I'm nonetheless determined to learn it as well as I can, but I still have to work with it while I'm learning. Any help in the solving the below problem would be enormously appreciated.</p>
  
  <p>I have an xml document that's formatted as such:</p>
  
  <pre><code>&lt;document xml:lang="EN"&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;Section/&gt; &lt;!--section marker--&gt;
    &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
    &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
    &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
    &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
    &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
    &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
    &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
    &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
    &lt;Section/&gt; &lt;!--section marker--&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
  &lt;/document&gt;
  </code></pre>
  
  <p>It has basically no hierarchy at the moment, and the  markers are included to give it structure and are included throughout the file. I'm trying to group all TOC elements and nest them within a containing element:</p>
  
  <pre><code>&lt;document xml:lang="EN"&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;CRPg&gt;text&lt;/CRPg&gt;
    &lt;Section/&gt; &lt;!--section marker--&gt;
    &lt;Wrapper&gt; &lt;!-- create new element to wrap around TOC and TOCAuth --&gt;
     &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
     &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
     &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
     &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
     &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
     &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
     &lt;TOCChap&gt;chapter title&lt;/TOCChap&gt;
     &lt;TOCAu&gt;chapter author&lt;/TOCAu&gt;
    &lt;/Wrapper&gt;
    &lt;Section/&gt; &lt;!--section marker--&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
    &lt;Txt&gt;body text&lt;/Txt&gt;
  &lt;/document&gt;
  </code></pre>
  
  <p>This is proving more difficult than I had anticipated. I've tried many different versions of the below code, and I've pasted in the one that seems the least wrong. I realize it's still pretty bad:</p>
  
  <pre><code>&lt;xsl:template match="*"&gt;
  &lt;xsl:for-each-group select="TOCChap | TOCAuth" group-adjacent="self::TOCChap | self::TOCAuth"&gt;
    &lt;xsl:choose&gt;
      &lt;xsl:when test="current-grouping-key()"&gt;
        &lt;wrapper&gt;
          &lt;xsl:apply-templates select="current-group()"/&gt;
  
        &lt;/wrapper&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></p>
  
  <p>Thank you in advance. </p>