This problem is known as "positional grouping" and you will find examples if you google under that name. In XSLT 2.0 you can tackle it using xsl:for-each-group. In 1.0 there are basically two approaches: recursive traversal of the siblings, or reduction to a value-based grouping problem using generate-id(preceding-sibling::Heading1) as the grouping key (which you can then use in a standard Muenchian grouping pattern).
If your recursive solution didn't work then it had a bug, and there's little I can do to help you find the bug if you don't post your code.
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference