<?xml version="1.0" encoding="Shift_JIS"?>
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:jtr="http://purl.org/jintrick/2003/XSLT"
	exclude-result-prefixes="jtr"
>

<!--
* 使い方 *
http://members.jcom.home.ne.jp/jintrick/Personal/d20035l#d16_1
-->


<xsl:template match="*" mode="jtr:Structuring">
	<xsl:variable name="level" select="number(substring(local-name(), 2))" />

	<!-- 各セクションは次のように構造化されます。 -->
	<!-- グループ化している要素の要素名やクラス名は自由に変更できます -->

	<div class="section-niveau{$level}" xmlns="http://www.w3.org/1999/xhtml">
		<div class="section-haut">
			<xsl:call-template name="jtr:Header">
				<xsl:with-param name="level" select="$level" />
			</xsl:call-template>
		</div>

		<div class="texte">
			<!-- この要素は変更・削除・複製できません（ここから） -->
			<xsl:call-template name="jtr:Body">
				<xsl:with-param name="level" select="$level" />
			</xsl:call-template>
			<!-- この要素は変更・削除・複製できません（ここまで） -->
		</div>

		<div class="section-pied">
			<xsl:call-template name="jtr:Footer">
				<xsl:with-param name="level" select="$level" />
			</xsl:call-template>
		</div>

	</div>

<!-- ちなみにXHTML2.0なら例えば次のようになります -->
<!-- 名前空間を与えている点に注意 -->

<!--

	<section xmlns="http://www.w3.org/2002/06/xhtml2">
		<h><xsl:apply-templates select="child::*|attribute::*" /></h>
		<xsl:call-template name="jtr:Body">
			<xsl:with-param name="level" select="$level" />
		</xsl:call-template>
	</section>

-->

</xsl:template>

<xsl:template name="jtr:Header">
	<!-- カレントノード : Heading Element -->
	<xsl:param name="level" />
	<xsl:apply-templates select="self::*" />
</xsl:template>

<xsl:template name="jtr:Body">
	<!-- カレントノード : Heading Element -->

	<xsl:param name="level" />

	<xsl:variable name="union" select="count(parent::*/child::*) + 1" />

	<xsl:variable name="current_position">
		<xsl:number level="single" count="*" />
	</xsl:variable>

	<!-- 後続する見出しの集合 -->
	<xsl:variable name="followingHeadings" select="following-sibling::*[substring(local-name(),1,1) = 'h' and  boolean(number(substring(local-name(),2)))]" />

	<!-- 次の下位見出しのポジション -->
	<xsl:variable name="next_position">
		<xsl:choose>
			<xsl:when test="$followingHeadings">
				<xsl:for-each select="$followingHeadings[position() = 1]">
					<xsl:number level="single" count="*" />
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$union" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- 次の(同位|上位)見出しのポジション -->
	<xsl:variable name="limit_position">
		<xsl:choose>
			<xsl:when test="$followingHeadings[number(substring(local-name(),2)) &lt;= $level]">
				<xsl:for-each select="$followingHeadings[number(substring(local-name(),2)) &lt;= $level][position() = 1]">
					<xsl:number level="single" count="*" />
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$union" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	
	<xsl:apply-templates select="following-sibling::*[position() &lt; number($next_position - $current_position)]" />
	<xsl:apply-templates select="following-sibling::*[position() &lt; number($limit_position - $current_position) and local-name() = concat('h', $level + 1)]" mode="jtr:Structuring" />

</xsl:template>

<xsl:template name="jtr:Footer">
	<!-- カレントノード : Heading Element -->

	<xsl:param name="level" />
	<xsl:text> </xsl:text>

</xsl:template>

<!--
-->

</xsl:stylesheet>

