<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" >
  <xsl:output method="txt" omit-xml-declaration="yes"/>

  <xsl:template match="/">
    <!-- matches the root-tag -->
    <xsl:message>
      <xsl:text>handling root</xsl:text>
    </xsl:message>
    <xsl:apply-templates select="//w:body"/>
  </xsl:template>

  <xsl:template match="w:body">
    <xsl:message>
      <xsl:text>handling w:body</xsl:text>
    </xsl:message>
    <xsl:apply-templates select="w:p"/>
  </xsl:template>

  <xsl:template match="w:p">
    <xsl:message>
      <xsl:text>handling w:p </xsl:text>
    </xsl:message>

    <xsl:apply-templates select="w:r/w:t">
      <xsl:with-param name="paraStyle" select="w:pPr/w:pStyle/@w:val"/>
      <xsl:with-param name="charStyle" select="w:r/w:rPr/w:rStyle/@w:val"/>
    </xsl:apply-templates>
  </xsl:template> 

  <xsl:template match="w:t">
    <xsl:param name="paraStyle"/> 
    <xsl:param name="charStyle"/>
    <xsl:variable name="content" select="."/>
    <xsl:message>
      <xsl:text>handling w:t</xsl:text>
      <xsl:text> pStyle is </xsl:text>
      <xsl:value-of select="$paraStyle"/> 
    </xsl:message>

    <xsl:choose>
      <xsl:when test="$paraStyle='para1'">
        <xsl:text>&#xA;============&#xA;</xsl:text> 
        <xsl:value-of select="$content"/>
        <xsl:text>&#xA;============&#xA;</xsl:text> 
      </xsl:when>
      <xsl:when test="$paraStyle='para2'">
        <xsl:text>&#xA;----------------------------------------------------&#xA;</xsl:text> 
        <xsl:value-of select="normalize-space($content)"/>
        <xsl:text>&#xA;----------------------------------------------------&#xA;</xsl:text> 
      </xsl:when>
      <xsl:when test="$paraStyle='para3'">
        <xsl:text>&#xA;</xsl:text> 
        <xsl:value-of select="normalize-space($content)"/>
        <xsl:text>&#xA;----------------------------------------------------&#xA;</xsl:text> 
      </xsl:when>
      <xsl:when test="$paraStyle='para4'">
        <xsl:text>&#xA;**</xsl:text> 
        <xsl:value-of select="normalize-space($content)"/>
        <xsl:text>**&#xA;</xsl:text> 
      </xsl:when>
      <xsl:when test="$paraStyle='para5'">
        <xsl:choose>
          <xsl:when test="position()=1 and $charStyle='char1'">
            <xsl:text>&#xA;</xsl:text>
            <xsl:text>**</xsl:text> 
            <xsl:value-of select="normalize-space($content)"/>
            <xsl:text>** </xsl:text> 
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="./preceding-sibling::w:tab">
                <xsl:text>           </xsl:text>
                <xsl:value-of select="normalize-space($content)"/>
                <xsl:text>&#xA;</xsl:text> 
              </xsl:when>
              <xsl:otherwise>
                <xsl:text> </xsl:text>
                <xsl:value-of select="normalize-space($content)"/>
                <xsl:text> </xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="$paraStyle='para7'">
        <xsl:choose>
          <xsl:when test="position()=1 and $charStyle='char2'">
            <xsl:text>&#xA;</xsl:text>
            <xsl:text>*</xsl:text> 
            <xsl:value-of select="normalize-space($content)"/>
            <xsl:text>* </xsl:text> 
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="./preceding-sibling::w:tab">
                <xsl:text>           </xsl:text>
                <xsl:value-of select="normalize-space($content)"/>
                <xsl:text>&#xA;</xsl:text> 
              </xsl:when>
              <xsl:otherwise>
                <xsl:text> </xsl:text>
                <xsl:value-of select="normalize-space($content)"/>
                <xsl:text> </xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
 
      <xsl:otherwise>
        <xsl:text>&#xA;</xsl:text> 
        <xsl:value-of select="normalize-space($content)"/>
        <xsl:text>&#xA;</xsl:text> 
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="w:tab">
    <xsl:text>        </xsl:text>
  </xsl:template>
</xsl:stylesheet>
