<?xml version="1.0" encoding="ISO-8859-1" ?>
<!-- 
Copyright J.M. Vanel 2001 - under GNU public licence 
jmvanel@free.fr 
$Header: /home/jmv/cvsroot/XMLPublication/kernel/group-by-headers.xslt,v 1.2 2002/05/08 10:26:46 jmv Exp $
Worldwide Botanical Knowledge Base 
http://wwbota.free.fr/
-->

<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
                version="1.1"
xmlns="http://www.w3.org/1999/xhtml" >

<xsl:output method='xml' />
<xsl:variable name='WRAPPERTAG'>div</xsl:variable>

<xsl:template match='*[local-name()="body"]' mode='initial' >
 <xsl:apply-templates select="." mode="wrap-h1" />
</xsl:template>

<xsl:template match='*[local-name()="body"]' mode="wrap-h1">
 <xsl:variable name='body'>
 <xsl:copy>
  <xsl:value-of select='@*' />
  <xsl:for-each select='.//*[local-name()="h1"]'>
   <xsl:message> HEADER: <xsl:value-of select="." /> </xsl:message>
   <xsl:variable name='name-id' select='generate-id()' />
   <xsl:text>
   </xsl:text>
   <p class="h1">
    <xsl:copy-of select="." />
    <!-- Inspired from M. Kay , 2nd edition p. 496 -->
    <xsl:for-each select='following-sibling::node()
      [local-name()!="h1"]
      [generate-id(preceding-sibling::*[local-name()="h1"][1]) = $name-id]'>
     <xsl:apply-templates select="." mode='initial' />
    </xsl:for-each>
   </p><xsl:text>
   </xsl:text>
  </xsl:for-each>
 </xsl:copy>
 </xsl:variable>
<!-- DEBUG 
xsl:copy-of select="$body" 
<xsl:copy-of select="$body" />
 fin DEBUG
-->
 <xsl:apply-templates select="$body/*" mode='wrap-h2' />
</xsl:template>

<xsl:template match='*[local-name()=$WRAPPERTAG][@class="h1"]' mode="wrap-h2">
 <p class="h1">
  <xsl:copy-of select='*[local-name()="h1"]' />
  <xsl:for-each select='.//*[local-name()="h2"]'>
   <xsl:variable name='name-id' select='generate-id()' />
   <xsl:text>
   </xsl:text>
   <p class="h2">
    <xsl:copy-of select="." />
    <!-- Inspired from M. Kay , 2nd edition p. 496 -->
    <xsl:for-each select='following-sibling::node()
      [local-name()!="h2"]
      [generate-id(preceding-sibling::*[local-name()="h2"][1]) = $name-id]'>
     <xsl:apply-templates select="." mode='wrap-h2' />
    </xsl:for-each>
   </p><xsl:text>
   </xsl:text>
  </xsl:for-each>
 </p><xsl:text>
 </xsl:text>
</xsl:template>

<xsl:template match="@*|node()" mode='initial' >
 <xsl:copy>
  <xsl:apply-templates select="@*" mode='initial' /> 
  <xsl:apply-templates mode='initial' /> 
 </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()" mode='wrap-h2' >
 <xsl:copy>
  <xsl:apply-templates select="@*" mode='wrap-h2' /> 
  <xsl:apply-templates mode='wrap-h2' /> 
 </xsl:copy>
</xsl:template>

<xsl:template match="/">
 <xsl:apply-templates select="*" mode='initial' /> 
</xsl:template>

</xsl:stylesheet> 
