<?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"/>

  <!-- match the root-tag -->
  <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>

  <!-- traverse the xml-structure to where we want to go.
      This can serve other purposes, later -->
  <xsl:template match="w:body">
    <xsl:message>
      <xsl:text>handling w:body</xsl:text>
    </xsl:message>
    <xsl:apply-templates select="w:p"/>
  </xsl:template>

  <!-- match an actual paragraph -->
  <xsl:template match="w:p">
    <xsl:message>
      <xsl:text>handling w:p </xsl:text>
    </xsl:message>

    <!-- While we only want to process the w:t tag,
         the styles are identified in child structures
         under the w:pPr- and w:r-tags. 

        I just communicate them to the next template. -->
    <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> 

  <!-- *** Real Work Starts Here *** -->
  <xsl:template match="w:t">
    <!-- expect two parameters -->
    <xsl:param name="paraStyle"/> 
    <xsl:param name="charStyle"/>
    <!-- Get the actual content of this tag -->
    <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>

    <!-- decide, what to do for each of the 
    set style-names. Put another way:
    Produce Restructured Text -->
    <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>
      <!-- Definition-lists are always a chore.
      You must know the original xml-file a bit to understand this
      when-branch. An option-list, first -->
      <xsl:when test="$paraStyle='para5'">
        <xsl:choose>
          <!-- Definition Item -->
          <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>
          <!-- Not a Definition Item -->
          <xsl:otherwise>
            <xsl:choose>
              <!-- Definition -->
              <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>
              <!-- Something following the item, but not a definition -->
              <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>
      <!-- Now just a field-list. -->
      <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>

      <!-- Anything else ist just text. -->
      <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>

  <!-- This is unused... TODO: Keep or remove -->
  <xsl:template match="w:tab">
    <xsl:text>        </xsl:text>
  </xsl:template>
</xsl:stylesheet>
<!-- EOF -->

