I read some answers here on moving nodes down/wrapping elements, but can´t figure out the solution to my problem. Here it is: I want to turn a flat document hierarchy in a "deep" hierarchy with xslt (2.0). The problem is to limit the search for siblings to a certain sibling, in my case: Search all siblings from h1 up to next h1 node.
xml-code
<root>
<h1>A heading</h1>
<para>Some text.</para>
<h2>More text</h2>
<table>Content</table>
<pic>a picture</pic>
<h1>Another heading.</h1>
<para>Some text again.</para>
<para>More text.</para>
...
</root>
Real document is much longer, there are up to five levels of headings, there is no fixed order of elements, and in real life I need to move not only the h1-sections, but also the h2-sections and so on. I´ve tried several attempts with copy-of- and similar expressions, but I´m a newbie in xslt. So how to wrap all h1-elements and its siblings up to the next h1-element with a chapter-element and h2-elements in the same way with section-elements?
Desired output is
<root>
<chapter>
<h1>A heading</h1>
<para>Some text.</para>
<section>
<h2>More text</h2>
<table>Content</table>
<pic>a picture</pic>
</section>
</chapter>
<chapter>
<h1>Another heading.</h1>
<para>Some text again.</para>
<para>More text.</para>
</chapter>
...
</root>