Re: [xsl] Grouping with siblings

Subject: Re: [xsl] Grouping with siblings
From: "Liam R. E. Quin liam@xxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 16 Feb 2025 00:00:15 -0000
On Sat, 2025-02-15 at 20:49 +0000, rick@xxxxxxxxxxxxxx wrote:
> Thank you very much Martin. That got me going in the right direction.

A quick note - in some cases group-by won't work well. An example can
be if the things to be wrapped are part of a longer sequence, so
there's stuff outside the group but at the ame level.

An approach there can be to use a mode to grab the nodes you want to be
children of each chapter in the template matching chapter, and to
ignore those nodes when encountered with apply-templates processing
later, as the template for chapter handled them:

<xsl:template match="chapter">
  <chapter> <!--* or xsl:copy *-->
    <xsl:apply-attributes select="@*" />

    <!--* find the next chapter, if any *-->  B 
    <xsl:variable name="next" as="element(chapter)?"
      select="following-sibling::chapter[1]" />

    <!--* process all nodes up to the next chapter,
        * or up to the end of our container:
        *-->
    <xsl:apply-attributes select="following-sibling::node()[
      if ($next) then (. << $next) else true()
      ]" mode="we-want-these-ones" />
  </chapter>
</xsl:template>
<xsl:mode name="we-want-these-ones" on-no-match="shallow-copy" />

<xsl:template match="node()[preceding-sibling::chapter]" />

liam



--
Liam Quin,B https://www.delightfulcomputing.com/
Available for XML/Document/Information Architecture/XSLT/
XSL/XQuery/Web/Text Processing/A11Y training, work & consulting.
Barefoot Web-slave, antique illustrations: B http://www.fromoldbooks.org

Current Thread