<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:tei="http://www.tei-c.org/ns/1.0"
  exclude-result-prefixes="tei" version="1.0">

  <!-- Elements appearing in file:/C:/Users/Wendell/Projects/Research/Upconversion/Fechner/Working/fechner-edited.xml -->

  <xsl:output method="xml"/>
  <xsl:variable name="css">
    <style type="text/css">
  body { font-family: Cambria, Georgia, Times, "Times New Roman", serif;
    font-size: 16pt;
    width: 60%; margin-left: 15%; margin-right: 25% }

  .teiHeader, .pageFooter { border: medium solid black; padding: 0.5em;
      background-color: lemonchiffon }

  .teiHeader { border: medium solid black }

  .pageFooter { border: thin solid black; font-size: 80%; margin-top: 4ex }  
  .pageFooter > * { margin: 0px }

  .tag { font-family: sans-serif; color: purple; font-size: 80%; font-weight: bold }
  .tag > .val { font-weight: normal; color: blue }

  .fileDesc, .titleStmt { margin: 0px }
  
  .titleStmt * { margin: 0px }
  
  .titleStmt > .respStmt > span.resp:before { content: ' (' }
  .titleStmt > .respStmt > span.resp:after { content: ')' }
  .titleStmt > .principal:before { content: 'Encoded by ' }
  .titleStmt span.title.m-level { font-style:italic }
  .pageFooter .title { font-style: italic }
  
  .header-subsection
    { margin-top: 1em; font-size: 90% }

  .header-subsection > *:first-child {margin-top: 0px }
  .header-subsection > *:last-child {margin-bottom: 0px }
  .header-subsection:before { font-family: sans-serif; font-weight: bold }
  
  .sourceDesc:before { content: 'Sources:' }
  .projectDesc:before { content: 'Project:' }
  .editorialDecl:before { content: 'Editorial:' }
  
  .titlePage { text-align: center; margin-top: 5ex }
  .docTitle { font-size: 200%; font-weight: bold }
  .byline { margin-top: 2ex; font-size: 120% }
  
  .epigraph { width: 80%; margin-left: auto; margin-right: auto;
              margin-top: 3ex }

  .epigraph { text-align: left }
  .epigraph .quote { font-style: italic; font-size: 90% }
  .epigraph .bibl { text-align: right }
  .epigraph .bibl:before { content: "&#8212;" }
  
  .docImprint { margin-top: 4ex; font-size: 110% }

  .body, .div { margin-top: 4ex; padding-top: 1ex; border-top: thin solid black; }
  .head + .div { padding-top: 0px; margin-top: 0px; border-top: none }
  
  .pb { position: relative; right: -15%; font-size: 80% }
  
  .div.dedication { border: none; width: 40%; margin-left: auto; margin-right: auto;
           margin-top: 3ex; text-align: center; font-variant: small-caps }

  .dedication > * { margin: 0px }
  .dedication > .closer { text-align: right }
  .dedication > .closer:before { content: "&#8212;" }
  
  .body > .head,
  .div  > .head { text-align: center; margin-top: 1ex }
  
  .pb { display: block; float: right }
  .pb:before { content: '[' }
  .pb:after { content: ']' }
  
  .note { width: 80%; margin-left: 20%; padding: 1ex;
          border-top: thin solid black; border-bottom: thin solid black;
          font-size: 80% }
  .note:last-child { border-bottom: none }
  .note > *:first-child { margin-top: 0px }
  .note > *:last-child { margin-bottom: 0px }

  .figure { text-align: center }
  
  .quoted { font-style:inherit }
  .quoted:before { content: '&#8220;' }
  .quoted:after  { content: '&#8221;' }
  .quoted .quoted:before { content: '&#8216;' }
  .quoted .quoted:after  { content: '&#8217;' }
  
  .italic { font-style:italic }

  .foreign { font-style: italic }
  
  a { color: inherit; text-decoration: none }
  a:hover { text-decoration: underline }
  .uri-literal { font-size: 90%; font-family: sans-serif }
  
  sup { vertical-align: top }
  
  .monospace { font-size: 90%;
               font-family: Consolas,
               "Andale Mono",
               "Lucida Console",
               "Lucida Sans Typewriter",
               "DejaVu Sans Mono",
               monospace }
    </style>
  </xsl:variable>
  
  
  <xsl:template match="tei:TEI">
    <html>
      <head>
        <title>
          <xsl:value-of select="/descendant::tei:title[1]"/>
        </title>
        <xsl:copy-of select="$css"/>
      </head>
      <body>
        <xsl:apply-templates/>
        <xsl:call-template name="page-footer"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="tei:teiHeader | tei:fileDesc | tei:titleStmt |
    tei:titleStmt/tei:author | tei:titleStmt/tei:respStmt |
    tei:titleStmt/tei:principal | tei:publicationStmt |
    tei:encodingDesc">
    <xsl:call-template name="generic-div"/>
  </xsl:template>
  
  <xsl:template match="tei:sourceDesc| tei:projectDesc | tei:editorialDecl">
    <div>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">header-subsection</xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template match="tei:titleStmt/tei:title[1]" priority="2">
    <h3>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">main</xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </h3>
  </xsl:template>
  
  <xsl:template match="tei:titleStmt/tei:title">
    <h4>
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </h4>
  </xsl:template>
  
  <xsl:template match="tei:titleStmt/tei:respStmt/tei:resp |
    tei:titleStmt/tei:respStmt/tei:name">
    <xsl:call-template name="generic-span"/>
  </xsl:template>

  <xsl:template match="tei:text | tei:front | tei:body">
    <xsl:call-template name="generic-div"/>
  </xsl:template>
  
  <xsl:template match="tei:titlePage | tei:docTitle | tei:titlePart">
    <xsl:call-template name="generic-div"/>
  </xsl:template>
  
  <xsl:template match="tei:div">
    <div>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">
          <xsl:value-of select="@type"/>
          <xsl:if test="not(tei:head)">
            <xsl:text> unheaded</xsl:text>
          </xsl:if>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  
  <xsl:template match="tei:title">
    <span>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">
          <xsl:for-each select="@level">
            <xsl:value-of select="."/>
            <xsl:text>-level</xsl:text>
          </xsl:for-each>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </span>
  </xsl:template>
  
  <xsl:template match="tei:p">
    <p>
      <xsl:call-template name="assign-class"/>
      <xsl:if test="not(preceding-sibling::*)">
        <xsl:for-each select="parent::tei:note">
          <xsl:variable name="id">
            <xsl:apply-templates select="." mode="id"/>
          </xsl:variable>
          <a href="#ref-{$id}">
            <xsl:number level="any"/>
          </a>
          <xsl:text>. </xsl:text>
        </xsl:for-each>
      </xsl:if>
      <xsl:apply-templates/>
    </p>
    <xsl:apply-templates select=".//tei:figure | .//tei:note" mode="show"/>
  </xsl:template>

  <xsl:template match="tei:ref">
    <a href="{@target}">
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">
          <xsl:if test="normalize-space(.) = normalize-space(@target)">uri-literal</xsl:if>
        </xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </a>
  </xsl:template>

  <xsl:template match="tei:soCalled | tei:q" name="quoted">
    <span>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">quoted</xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="tei:code | tei:gi | tei:att | tei:val">
    <span>
      <xsl:call-template name="assign-class">
        <xsl:with-param name="special">monospace</xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="tei:div[@type='dedication']//tei:lb |
    tei:docImprint//tei:lb">
    <br class="lb"/>
  </xsl:template>
  
  <xsl:template match="tei:lb">
    <xsl:text> </xsl:text>
  </xsl:template>
  
  <xsl:template match="tei:byline | tei:epigraph | tei:docImprint | tei:quote">
    <xsl:call-template name="generic-div"/>
  </xsl:template>

  <xsl:template match="tei:p/tei:quote">
    <xsl:call-template name="quoted"/>
  </xsl:template>

  <xsl:template match="tei:epigraph/tei:bibl">
    <xsl:call-template name="generic-div"/>
  </xsl:template>
  
  <xsl:template match="tei:bibl">
    <xsl:call-template name="generic-span"/>
  </xsl:template>
  
  <xsl:template match="tei:closer">
    <xsl:call-template name="generic-div"/>
  </xsl:template>

  <xsl:template match="tei:p//tei:pb">
    <span>
      <xsl:call-template name="assign-class"/>
      <xsl:value-of select="@n"/>
    </span>
  </xsl:template>
  
  <xsl:template match="tei:pb">
    <div>
      <xsl:call-template name="assign-class"/>
      <xsl:value-of select="@n"/>
    </div>
  </xsl:template>
  
  <xsl:template match="tei:body/tei:head">
    <h2>
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>
  
  <xsl:template match="tei:div/tei:head">
    <h3>
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </h3>
  </xsl:template>
  
  <xsl:template match="tei:head">
    <xsl:apply-templates select="." mode="flag"/>
  </xsl:template>
  
  <xsl:template match="tei:signed | tei:dateline">
    <xsl:call-template name="generic-div"/>
  </xsl:template>

  <xsl:template match="tei:note">
    <xsl:variable name="id">
      <xsl:apply-templates select="." mode="id"/>
    </xsl:variable>
    <sup class="fn-ref" id="ref-{$id}">
      <a href="#{$id}">
        <xsl:number level="any"/>
      </a>
    </sup>
  </xsl:template>
  
  <xsl:template match="tei:figure"/>
  <!-- Suppressed from appearing in line, and instead
    picked up after tei:p parents. Note that figures 
    not inside tei:p will not display
    with this stylesheet! -->
  
  <xsl:template match="tei:note | tei:figure" mode="show">
    <xsl:variable name="id">
      <xsl:apply-templates select="." mode="id"/>
    </xsl:variable>
    <div id="{$id}">
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="tei:graphic">
    <img src="{@url}" alt="{@url}" width="100%">
      <xsl:call-template name="assign-class"/>
    </img>
  </xsl:template>

  <xsl:template match="tei:foreign | tei:hi">
    <xsl:call-template name="generic-span"/>
  </xsl:template>
  
  <xsl:template name="page-footer">
    <div class="pageFooter">
      <p>
        <span class="title">
          <xsl:value-of select="/*/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:title[1]"/>
        </span>
        <xsl:text>, by </xsl:text>
        <xsl:value-of select="/*/tei:teiHeader/tei:fileDesc/tei:titleStmt/tei:author"/>
      </p>
      <p>Encoded and produced by Wendell Piez, November-December 2011</p>
      <xsl:apply-templates select="/*/tei:teiHeader/tei:fileDesc/tei:publicationStmt/tei:p"/>
    </div>
  </xsl:template>
  
        
  <xsl:template match="@*"/>

  <xsl:template name="tag">
    <xsl:param name="start" select="false()"/>
    <xsl:param name="end" select="false()"/>
    <xsl:param name="empty" select="false()"/>
    <span class="tag">
      <xsl:text>&lt;</xsl:text>
      <xsl:if test="$end">/</xsl:if>
      <xsl:value-of select="name()"/>
      <xsl:if test="$start">
        <xsl:for-each select="@*">
          <xsl:text> </xsl:text>
          <xsl:value-of select="name()"/>
          <xsl:text>="</xsl:text>
          <span class="val">
            <xsl:value-of select="."/>
          </span>
          <xsl:text>"</xsl:text>
        </xsl:for-each>
      </xsl:if>
      <xsl:if test="$empty">/</xsl:if>
      <xsl:text>&gt;</xsl:text>
    </span>
  </xsl:template>
  
  <xsl:template name="match-msg">
    <xsl:message>
      <xsl:value-of select="name()"/>
      <xsl:text> matched</xsl:text>
    </xsl:message>
  </xsl:template>
  
  <xsl:template match="*[not(node())]" mode="flag" priority="2">
    <xsl:call-template name="match-msg"/>
    <xsl:call-template name="tag">
      <xsl:with-param name="start" select="true()"/>
      <xsl:with-param name="empty" select="true()"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="*[../text()[normalize-space()]]" mode="flag">
    <xsl:call-template name="match-msg"/>
    <xsl:call-template name="tag">
      <xsl:with-param name="start" select="true()"/>
    </xsl:call-template>
    <xsl:apply-templates/>
    <xsl:call-template name="tag">
      <xsl:with-param name="end" select="true()"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="*" mode="flag">
    <xsl:call-template name="match-msg"/>
    <div>
      <xsl:call-template name="tag">
        <xsl:with-param name="start" select="true()"/>
      </xsl:call-template>
      <xsl:apply-templates/>
      <xsl:call-template name="tag">
        <xsl:with-param name="end" select="true()"/>
      </xsl:call-template>
    </div>
  </xsl:template>

  <xsl:template name="generic-div">
    <div>
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  
  <xsl:template name="generic-span">
    <span>
      <xsl:call-template name="assign-class"/>
      <xsl:apply-templates/>
    </span>
  </xsl:template>
  
  <xsl:template name="assign-class">
    <xsl:param name="special"/>
    <xsl:variable name="class">
      <xsl:value-of select="local-name(.)"/>
      <xsl:for-each select="@rend">
        <xsl:text> </xsl:text>
        <xsl:value-of select="."/>
      </xsl:for-each>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$special"/>
    </xsl:variable>
    <xsl:attribute name="class">
      <xsl:value-of select="normalize-space($class)"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="*[@xml:id]" mode="id">
    <xsl:value-of select="@xml:id"/>
  </xsl:template>
  
  <xsl:template match="*" mode="id">
    <xsl:value-of select="generate-id()"/>
  </xsl:template>

</xsl:stylesheet>
