<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="text"/>

  <xsl:strip-space elements="*"/>

  <xsl:param name="width" select="76"/>

  <xsl:template match="RevisionDate/Date | AvailabilityDate"/>

  <xsl:template match="ResumeBody/ResumeSection">
    <xsl:apply-templates/>
    <xsl:text>


</xsl:text>
    <xsl:if test="position() = last()">
      <xsl:text>[ generated using http://www.xmlportfolio.com/resume.txt.xsl ]</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="PersonalData|ResumeSection//ResumeSection|SectionTitle">
    <xsl:apply-templates/>
    <xsl:if test="position() != last()">
      <xsl:text>

</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template match="SectionTitle[count(ancestor::ResumeSection)=1]">
    <xsl:value-of select="translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    <xsl:text>

</xsl:text>
  </xsl:template>

  <xsl:template match="First|Month|State">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="City">
    <xsl:apply-templates/>
    <xsl:text>, </xsl:text>
  </xsl:template>

  <xsl:template match="Name|AddressLine|PostalCode|Email|SectionTitle/JobTitle|SectionTitle/EmployerName|Voice">
    <xsl:apply-templates/>
    <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template match="AreaCode">
    <xsl:apply-templates/>
    <xsl:text>-</xsl:text>
  </xsl:template>

  <xsl:template match="CurrentPosition">
    <xsl:text>present</xsl:text>
  </xsl:template>

  <xsl:template match="p" name="paragraph">
    <xsl:param name="string" select="string(.)"/>
    <xsl:param name="prependSpaceLength" select="0"/>
    <xsl:variable name="max-string-length">
      <xsl:call-template name="compute-max-string-length">
        <xsl:with-param name="string" select="$string"/>
        <xsl:with-param name="prependSpaceLength" select="$prependSpaceLength"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="string-length($string) > $width and $max-string-length > $prependSpaceLength">
        <xsl:value-of select="substring($string, 1, $max-string-length)"/>
        <xsl:text>
</xsl:text>
        <xsl:variable name="prependString">
          <xsl:call-template name="prependSpace">
            <xsl:with-param name="length" select="$prependSpaceLength"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="paragraph">
          <xsl:with-param name="string" select="concat($prependString, substring($string, $max-string-length+1))"/>
          <xsl:with-param name="prependSpaceLength" select="$prependSpaceLength"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$string"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="compute-max-string-length">
    <xsl:param name="string"/>
    <xsl:param name="count" select="0"/>
    <xsl:param name="prependSpaceLength"/>
    <xsl:variable name="newCount" select="$count + string-length(substring-before($string, ' '))+1"/>
    <xsl:choose>
      <xsl:when test="contains($string, ' ') and $newCount &lt;= $width">
        <xsl:call-template name="compute-max-string-length">
          <xsl:with-param name="string" select="substring-after($string, ' ')"/>
          <xsl:with-param name="count" select="$newCount"/>
          <xsl:with-param name="prependSpaceLength" select="$prependSpaceLength"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$count &lt;= $prependSpaceLength and contains($string, ' ')">
        <xsl:value-of select="$newCount"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$count"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="prependSpace">
    <xsl:param name="length"/>
    <xsl:if test="$length > 0">
      <xsl:text> </xsl:text>
      <xsl:call-template name="prependSpace">
        <xsl:with-param name="length" select="$length - 1"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <xsl:template match="StartDate">
    <xsl:apply-templates/>
    <xsl:text> - </xsl:text>
  </xsl:template>

  <xsl:template match="ol">
    <xsl:apply-templates>
      <xsl:with-param name="ordered" select="true()"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="ul">
    <xsl:if test="preceding-sibling::*[1][self::p]">
      <xsl:text>

</xsl:text>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="li">
    <xsl:param name="ordered" select="false()"/>
    <xsl:variable name="prependString">
      <xsl:choose>
        <xsl:when test="$ordered">
          <xsl:value-of select="concat('  ', position(), '. ')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>  + </xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="currentText">
      <xsl:apply-templates/>
    </xsl:variable>
    <xsl:call-template name="paragraph">
      <xsl:with-param name="string" select="concat($prependString, $currentText)"/>
      <xsl:with-param name="prependSpaceLength" select="string-length($prependString)"/>
    </xsl:call-template>
    <xsl:if test="position() != last()">
      <xsl:text>
</xsl:text>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
