something like the following seems to do what you want:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="x" name="x">
<xsl:param name="level" select="2"/>
<xsl:param name="content" select="*"/>
<xsl:for-each-group select="$content"
group-starting-with="*[name()=concat('h',$level)]">
<xsl:choose>
<xsl:when test="$level>6">
<xsl:copy-of select="$content"/>
</xsl:when>
<xsl:when test="name()=concat('h',$level)">
<div class="{name()}">
<xsl:call-template name="x">
<xsl:with-param name="level" select="$level+1"/>
<xsl:with-param name="content" select="current-group()"/>
</xsl:call-template>
</div>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="x">
<xsl:with-param name="level" select="$level+1"/>
<xsl:with-param name="content" select="current-group()"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
$ saxon9 h.xml h.xsl
<?xml version="1.0" encoding="UTF-8"?>
<div class="h3">
<h3>Title 3</h3>
<p>bla bla bla</p>
<p>bla bla bla</p>
</div>
<div class="h2">
<h2>Title 2</h2>
<p>bla bla bla</p>
<p>bla bla bla</p>
<div class="h3">
<h3>Title 3</h3>
<p>bla bla bla</p>
<p>bla bla bla</p>
<div class="h4">
<h4>Title 4</h4>
<p>bla bla bla</p>
<p>bla bla bla</p>
</div>
</div>
</div>
--~------------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--