<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <!--<xsl:copy>-->
      <xsl:apply-templates select="@* | node() "/>
    <!--</xsl:copy>-->
  </xsl:template>

<xsl:template match="HEAD"/>
<xsl:template match="B | I">
  <xsl:apply-templates/>
</xsl:template>


  <xsl:template match="DIV" >
    <xsl:copy>
      <xsl:apply-templates select="@* | P[@CLASS = 'ahead'] "/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="P[@CLASS = 'ahead']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
	<xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
	    <xsl:element name="LEVEL0">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::P" />
        </xsl:element>
	  </xsl:when>
	  <xsl:otherwise>
		   <xsl:apply-templates select="following-sibling::P"/>
	  </xsl:otherwise>
	</xsl:choose>
  </xsl:template>

  <xsl:template match="P[@CLASS = 'Main']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
	  <xsl:choose>
	    <xsl:when test="string-length($vContents) &gt; 0" >
        <xsl:element name="LEVEL1">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <!--<xsl:apply-templates select="following-sibling::P[@CLASS = 'Sub1'] " />-->
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Main')] " />
        </xsl:element>
    </xsl:when>
      <xsl:otherwise>
        <xsl:element name="LEVEL1"></xsl:element>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Main')] " />
        
      </xsl:otherwise>
	  </xsl:choose>
    
  </xsl:template>

  <xsl:template match="P[@CLASS = 'Sub1']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
    <xsl:element name="LEVEL2">
    <xsl:copy>
      <xsl:value-of select="."/>
    </xsl:copy>
	  <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub1')]" />
       </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub1')] " />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="P" >
    <xsl:variable name="Curr_Level" select="translate(@CLASS, 'Sub', '') + 1"/>
    <xsl:variable name="Prec_Level">
      <xsl:value-of select="translate(preceding-sibling::P[1]/@CLASS, 'Sub', '') + 1 "/>
    </xsl:variable>
    <xsl:variable name="Foll_Level">
      <xsl:value-of select="translate(following-sibling::P[1]/@CLASS, 'Sub', '') + 1"/>
    </xsl:variable>

    <xsl:variable name="vTempLevel" select="concat('Sub',$Curr_Level)"/>

    <xsl:choose>
      <xsl:when test="$Prec_Level  &lt; $Curr_Level">
        <xsl:element name="{concat('LEVEL', $Curr_Level)}">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:if test="not($Curr_Level &gt; $Foll_Level) ">
            <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub1')]" />
          </xsl:if>
        </xsl:element>
      </xsl:when>

      <xsl:when test="$Prec_Level = $Curr_Level">
        <xsl:copy>
          <xsl:value-of select="."/>
        </xsl:copy>
        <xsl:if test="not($Curr_Level &gt; $Foll_Level) ">
          <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub1')]" />
        </xsl:if>
      </xsl:when>

      <xsl:otherwise>
        <xsl:element name="{concat('LEVEL', $Curr_Level)}">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:if test="not($Curr_Level &gt; $Foll_Level)">
            <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub1')]" />
          </xsl:if>
        </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- Adding New Templates for testing -->
  <!-- <xsl:template match="P[@CLASS = 'Sub2']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
        <xsl:element name="LEVEL3">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub2')]" />
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub2')] " />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="P[@CLASS = 'Sub3']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
        <xsl:element name="LEVEL4">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub3')]" />
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub3')] " />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="P[@CLASS = 'Sub4']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
        <xsl:element name="LEVEL5">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub4')]" />
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub4')] " />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="P[@CLASS = 'Sub5']" >
    <xsl:variable name="vContents" select="normalize-space(.)"/>
    <xsl:choose>
      <xsl:when test="string-length($vContents) &gt; 0" >
        <xsl:element name="LEVEL6">
          <xsl:copy>
            <xsl:value-of select="."/>
          </xsl:copy>
          <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub5')]" />
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="following-sibling::P[1][not(@CLASS = 'Sub5')] " />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>-->
  <!-- End of Templates for testing-->

</xsl:stylesheet>
