<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns="http://www.w3.org/1999/xhtml" xpath-default-namespace="http://www.w3.org/1999/xhtml">
	<xsl:output method="xml" indent="yes"/>
	<xsl:template match="@*|node()">
		<xsl:copy>
			<xsl:apply-templates select="@*|node()"/>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="body">
		<xsl:copy>
			<xsl:for-each-group select="element()|comment()|processing-instruction()" group-starting-with="h1">
				<xsl:apply-templates select="." mode="group"/>
			</xsl:for-each-group>
		</xsl:copy>
	</xsl:template>
	<xsl:template match="h1" mode="group">
		<h>
			<xsl:apply-templates select="@*|node()"/>
		</h>
		<xsl:for-each-group select="current-group() except ." group-starting-with="h2">
			<xsl:apply-templates select="." mode="group"/>
		</xsl:for-each-group>
	</xsl:template>
	<xsl:template match="h2|h3|h4|h5|h6" mode="group">
		<xsl:variable name="this" select="name()"/>
		<xsl:variable name="next" select="translate($this, '23456', '34567')"/>
		<section>
			<h>
				<xsl:apply-templates select="@*|node()"/>
			</h>
			<xsl:for-each-group select="current-group() except ." group-starting-with="*[name() = $next]">
				<xsl:apply-templates select="." mode="group"/>
			</xsl:for-each-group>
		</section>
	</xsl:template>
	<xsl:template match="element()|comment()|processing-instruction()" mode="group">
		<xsl:apply-templates select="current-group()"/>
	</xsl:template>
	
	<!-- Templates to delete attributes copied from DTD.  -->
	<xsl:template match="@shape"/><!-- a element -->
	<xsl:template match="@rowspan[. = 1]"/><!-- th, td element -->
	<xsl:template match="@colspan[. = 1]"/><!-- th, td element -->
	<xsl:template match="@version"/><!-- html element, 1.1 -->
	<xsl:template match="@profile"/><!-- head element, 1.1 -->
	
	<!-- Additional templates can be added here -->	
</xsl:stylesheet>

