<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format"
                xpath-default-namespace="http://www.tei-c.org/ns/1.0"
                version="2.0">

  <xsl:variable name="cover-image">buechlein-cover/cover.svg</xsl:variable>
  
  <xsl:variable name="cover-background">black</xsl:variable>
  
  <xsl:attribute-set name="text">
    <xsl:attribute name="font-family">Cambria</xsl:attribute>
    <xsl:attribute name="text-align">justify</xsl:attribute>
    <xsl:attribute name="text-align-last">start</xsl:attribute>
    <xsl:attribute name="hyphenate">true</xsl:attribute>
    <xsl:attribute name="hyphenation-keep">page</xsl:attribute>
    <xsl:attribute name="line-height">1.33</xsl:attribute>
    <xsl:attribute name="line-height-shift-adjustment">disregard-shifts</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="metadata">
    <xsl:attribute name="font-family">Tahoma</xsl:attribute>
    <xsl:attribute name="font-size">10pt</xsl:attribute>
    <xsl:attribute name="hyphenate">true</xsl:attribute>
    <xsl:attribute name="hyphenation-keep">page</xsl:attribute>
    <xsl:attribute name="line-height">1.33</xsl:attribute>
    <xsl:attribute name="line-height-shift-adjustment">disregard-shifts</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="first-paragraph"/>
  
  <xsl:attribute-set name="paragraph">
    <xsl:attribute name="text-indent">1em</xsl:attribute>  
  </xsl:attribute-set>
  
  <xsl:attribute-set name="metadata-paragraph">
    <xsl:attribute name="space-before">6pt</xsl:attribute>  
  </xsl:attribute-set>
  
  <xsl:attribute-set name="metadata-title">
    <xsl:attribute name="font-size">120%</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="doc-title" use-attribute-sets="title-page">
    <xsl:attribute name="font-size">150%</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="title-page" use-attribute-sets="head">
    <xsl:attribute name="font-size">120%</xsl:attribute>
    <xsl:attribute name="space-before">18pt</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="epigraph" use-attribute-sets="title-page">
    <xsl:attribute name="font-weight">inherit</xsl:attribute>
    <xsl:attribute name="font-size">85%</xsl:attribute>
    <xsl:attribute name="text-align">justify</xsl:attribute>
    <xsl:attribute name="text-align-last">start</xsl:attribute>
  </xsl:attribute-set>

  <xsl:attribute-set name="imprint" use-attribute-sets="title-page">
    <xsl:attribute name="font-weight">inherit</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="main-head" use-attribute-sets="head">
    <xsl:attribute name="font-size">150%</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="chapter-head" use-attribute-sets="head">
    <xsl:attribute name="font-size">120%</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="head">
    <xsl:attribute name="hyphenate">false</xsl:attribute>
    <xsl:attribute name="text-align">center</xsl:attribute>
    <xsl:attribute name="text-align-last">center</xsl:attribute>
    <xsl:attribute name="space-after">1em</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="drop-cap">
    <!--margin="0pt" color="red" text-depth="0pt" font-size="62pt"
    line-height="40pt" font-weight="bold" font-family="Courier"-->
    <xsl:attribute name="margin">0pt</xsl:attribute>
    <xsl:attribute name="margin-right">1pt</xsl:attribute>
    <xsl:attribute name="text-depth">0pt</xsl:attribute>
    <xsl:attribute name="font-size">300%</xsl:attribute>
    <xsl:attribute name="line-height">76%</xsl:attribute>
    <xsl:attribute name="font-weight">bold</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="page-header">
    <xsl:attribute name="letter-spacing">2pt</xsl:attribute>
    <xsl:attribute name="text-align">center</xsl:attribute>
    <xsl:attribute name="text-align-last">center</xsl:attribute>
    <xsl:attribute name="border-bottom">thin solid black</xsl:attribute>
    <xsl:attribute name="padding-bottom">3pt</xsl:attribute>
    <xsl:attribute name="margin-bottom">6pt</xsl:attribute>
    <!--<xsl:attribute name="font-size">120%</xsl:attribute>-->
    <!--<xsl:attribute name="font-weight">bold</xsl:attribute>-->
    <xsl:attribute name="font-style">italic</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="page-footer">
    <xsl:attribute name="text-align">center</xsl:attribute>
    <xsl:attribute name="text-align-last">center</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="footnote-body">
    <xsl:attribute name="font-size">80%</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="footnote-number">
    <xsl:attribute name="vertical-align">super</xsl:attribute>
    <xsl:attribute name="font-size">75%</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="code">
    <xsl:attribute name="font-family">Consolas</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="italic">
    <xsl:attribute name="font-style">italic</xsl:attribute>
  </xsl:attribute-set>
  
  <xsl:attribute-set name="closer">
    <xsl:attribute name="space-before">12pt</xsl:attribute>  
  </xsl:attribute-set>
  
  
<!-- Elements appearing in file:/C:/Users/Wendell/Projects/Research/Upconversion/Fechner/Working/fechner-edited.xml -->

  <xsl:attribute-set name="octavo">
    <xsl:attribute name="page-height">8.5in</xsl:attribute>  
    <xsl:attribute name="page-width">5.5in</xsl:attribute>  
  </xsl:attribute-set>
  
  <xsl:attribute-set name="page-margins">
    <xsl:attribute name="margin-top">0.75in</xsl:attribute>  
    <xsl:attribute name="margin-bottom">0.75in</xsl:attribute>
    <xsl:attribute name="margin-left">1.25in</xsl:attribute>
    <xsl:attribute name="margin-right">1.25in</xsl:attribute>  
  </xsl:attribute-set>
  
  <xsl:template match="/">
    <fo:root>
      <fo:layout-master-set>
        <fo:simple-page-master master-name="blank"
          xsl:use-attribute-sets="octavo page-margins">
          <fo:region-body region-name="watermark" display-align="center"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="cover"
          xsl:use-attribute-sets="octavo" margin="0pt">
          <fo:region-body region-name="content" display-align="center"
            background-color="{$cover-background}"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="metadata"
          xsl:use-attribute-sets="octavo" margin="0.5in">
          <fo:region-body region-name="content" display-align="center"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="front"
          xsl:use-attribute-sets="octavo page-margins"
          margin-left="1in" margin-right="1in">
          <fo:region-body region-name="content" margin-top="0.5in"
            margin-bottom="0.5in" display-align="center"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="body-first"
          xsl:use-attribute-sets="octavo page-margins" >
          <fo:region-body region-name="content" margin-top="1in"
            margin-bottom="1.25in"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="body-recto"
          xsl:use-attribute-sets="octavo page-margins">
          <fo:region-body region-name="content" margin-top="1in"
            margin-bottom="1.25in"/>
          <fo:region-before region-name="recto-header" extent="1in" display-align="after"/>
          <fo:region-after region-name="footer" extent="1in"/>
        </fo:simple-page-master>
        <fo:simple-page-master master-name="body-verso"
          xsl:use-attribute-sets="octavo page-margins">
          <fo:region-body region-name="content" margin-top="1in"
            margin-bottom="1.25in"/>
          <fo:region-before region-name="verso-header" extent="1in" display-align="after"/>
          <fo:region-after region-name="footer" extent="1in"/>
        </fo:simple-page-master>
        <fo:page-sequence-master master-name="metadata-sequence">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference master-reference="metadata"
              blank-or-not-blank="not-blank"/>
            <fo:conditional-page-master-reference master-reference="blank"
              blank-or-not-blank="blank"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
        <fo:page-sequence-master master-name="front-sequence">
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference master-reference="front"
              blank-or-not-blank="not-blank"/>
            <fo:conditional-page-master-reference master-reference="blank"
              blank-or-not-blank="blank"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
        <fo:page-sequence-master master-name="chapter-sequence">
          <fo:single-page-master-reference master-reference="body-first"/>
          <fo:repeatable-page-master-alternatives>
            <fo:conditional-page-master-reference master-reference="body-recto"
              blank-or-not-blank="not-blank" odd-or-even="odd"/>
            <fo:conditional-page-master-reference master-reference="body-verso"
              blank-or-not-blank="not-blank" odd-or-even="even"/>
            <fo:conditional-page-master-reference master-reference="blank"
              blank-or-not-blank="blank"/>
          </fo:repeatable-page-master-alternatives>
        </fo:page-sequence-master>
      </fo:layout-master-set>
      <xsl:call-template name="cover"/>
      <xsl:apply-templates/>
      <xsl:apply-templates select="$colophon"/>
    </fo:root>
  </xsl:template>
  
  <xsl:template match="teiHeader | front" mode="recto-header verso-header">
    <xsl:apply-templates select="." mode="flag"/>
  </xsl:template>
  
  <xsl:template match="body | front/div" mode="recto-header">
    <fo:static-content flow-name="recto-header">
      <fo:block xsl:use-attribute-sets="page-header">
        <xsl:apply-templates select="head" mode="#current"/>
      </fo:block>
    </fo:static-content>
  </xsl:template>
  
  <xsl:template match="body | front/div" mode="verso-header">
    <fo:static-content flow-name="verso-header">
      <fo:block xsl:use-attribute-sets="page-header">
        <xsl:apply-templates select="head" mode="#current"/>
      </fo:block>
    </fo:static-content>
  </xsl:template>
  
  <xsl:template match="head" mode="recto-header verso-header">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="body/head" mode="recto-header">
    <xsl:text>A MANUAL</xsl:text>
  </xsl:template>
  
  <xsl:template match="head[starts-with(.,'INTRODUCTION')]" mode="recto-header verso-header">
    <xsl:text>INTRODUCTION</xsl:text>
  </xsl:template>
  
  <xsl:template match="head[starts-with(.,'PREFACE')]" mode="recto-header verso-header">
    <xsl:text>PREFACE</xsl:text>
  </xsl:template>
  
  <xsl:template match="head[starts-with(.,'APPENDIX')]" mode="recto-header verso-header">
    <xsl:text>APPENDIX</xsl:text>
  </xsl:template>
  
  <xsl:template match="body/div" mode="recto-header verso-header">
    <xsl:apply-templates select=".." mode="#current"/>
  </xsl:template>
  
  <xsl:template match="teiHeader" mode="footer"/>
  
  <xsl:template match="front | body" mode="footer">
    <fo:static-content flow-name="footer">
      <fo:block xsl:use-attribute-sets="page-footer">
        <fo:page-number/>
      </fo:block>
    </fo:static-content>
  </xsl:template>
  
  <xsl:template match="teiHeader">
    <fo:page-sequence master-reference="metadata-sequence"
      initial-page-number="1" xsl:use-attribute-sets="metadata">
      <xsl:call-template name="watermark"/>      
      <fo:flow flow-name="content">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>

  <xsl:template name="watermark">
    <fo:static-content flow-name="watermark">
      <fo:block-container width="100%" text-indent="0em">
        <fo:block text-align="center" text-align-last="center">
          <fo:instream-foreign-object text-align="center" content-width="3in">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400">
              <g transform="translate(200 200)" stroke="#DFDFDF" fill="none"
                stroke-width="1">
                <circle r="80" transform="rotate(30) translate(80 0)"/>
                <circle r="80" transform="rotate(90) translate(80 0)"/>
                <circle r="80" transform="rotate(150) translate(80 0)"/>
                <circle r="80" transform="rotate(210) translate(80 0)"/>
                <circle r="80" transform="rotate(270) translate(80 0)"/>
                <circle r="80" transform="rotate(330) translate(80 0)"/>
              </g>
            </svg>
          </fo:instream-foreign-object>
        </fo:block>
      </fo:block-container>
    </fo:static-content>
  </xsl:template>
  
  <xsl:template match="fileDesc | titleStmt | publicationStmt |
    sourceDesc | encodingDesc | projectDesc">
    <fo:block>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="editorialDecl"/>
  
  <xsl:template match="titleStmt/title[1]" priority="3">
    <fo:block xsl:use-attribute-sets="metadata-title">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="titleStmt/title" priority="2">
    <fo:block xsl:use-attribute-sets="metadata-paragraph">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="titleStmt/*">
    <fo:block xsl:use-attribute-sets="metadata-paragraph">
      <xsl:apply-templates select="." mode="label"/>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="titleStmt/author" mode="label">
    <xsl:text>By </xsl:text>
  </xsl:template>
  
  <xsl:template match="titleStmt/respStmt" mode="label"/>
  
  <xsl:template match="titleStmt/principal" mode="label">
      <xsl:text>Encoded in TEI and produced by </xsl:text>
  </xsl:template>
  
  <xsl:template match="name">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="resp">
    <xsl:text> (</xsl:text>
    <xsl:apply-templates/>
    <xsl:text>)</xsl:text>
  </xsl:template>
  
  <xsl:template match="front">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="body">
    <xsl:apply-templates select="div"/>
  </xsl:template>
  
  <xsl:template name="cover">
    <fo:page-sequence master-reference="cover" initial-page-number="auto-odd">
      <xsl:call-template name="watermark"/>
      <fo:flow flow-name="content">
        <fo:block text-align="center" text-align-last="center">
          <fo:external-graphic src="url({$cover-image})"
            width="100%" content-width="scale-to-fit"/>          
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>
  
  <xsl:template match="titlePage | div[@type='dedication']">
    <fo:page-sequence master-reference="front-sequence"
      initial-page-number="auto-odd"
      xsl:use-attribute-sets="text">  
      <xsl:call-template name="watermark"/>
      <fo:flow flow-name="content">
        <fo:block text-align="center" text-align-last="center">
          <xsl:if test="@type='dedication'">
            <xsl:attribute name="font-variant">small-caps</xsl:attribute>
          </xsl:if>
          <xsl:apply-templates/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>
  
  <xsl:template match="div" mode="page-numbering"/>
  
  <xsl:template match="body/div[1]" mode="page-numbering">
    <xsl:attribute name="initial-page-number">1</xsl:attribute>
  </xsl:template>
  
  <xsl:template match="front/div" mode="page-numbering">
    <xsl:attribute name="format">i</xsl:attribute>
  </xsl:template>
  
  
  <xsl:template match="div">
    <fo:page-sequence master-reference="chapter-sequence"
      initial-page-number="auto-odd"
      xsl:use-attribute-sets="text">
      <xsl:apply-templates select="." mode="page-numbering"/>
      <fo:static-content flow-name="xsl-footnote-separator">
        <fo:block space-after="6pt">
          <fo:leader leader-pattern="rule"
            leader-length="100%"/>
        </fo:block>
      </fo:static-content>
      <xsl:call-template name="watermark"/>
      
      <xsl:apply-templates select="." mode="recto-header"/>
      <xsl:apply-templates select="." mode="verso-header"/>
      <xsl:apply-templates select=".." mode="footer"/>
      
      <fo:flow flow-name="content">
        <fo:block>
          <xsl:if test="not(preceding-sibling::div)">
            <xsl:apply-templates select="../head"/>
          </xsl:if>
          <xsl:apply-templates/>
        </fo:block>
        <xsl:apply-templates
          select="following-sibling::div[1][empty(head)]" mode="include"/>
      </fo:flow>
    </fo:page-sequence>    
  </xsl:template>
  
  <xsl:template match="div[empty(head)][not(@type='dedication')]" priority="2"/>
  
  <xsl:template match="div" mode="include">
    <fo:block margin-top="6pt" padding-top="6pt" border-top="thin solid black">
      <xsl:apply-templates/>
    </fo:block>
    <xsl:apply-templates mode="include"
      select="following-sibling::div[1][empty(head)]"/>
  </xsl:template>
  
  <xsl:template match="text">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="teiHeader//title[@level='m'] |
    div[@type='colophon']//title[@level='m']">
    <fo:inline xsl:use-attribute-sets="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>

  <xsl:template match="title">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="title[@rend='quoted']">
    <xsl:call-template name="quoted"/>
  </xsl:template>
  
  <xsl:template match="title[@rend='italic']">
    <fo:inline xsl:use-attribute-sets="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>
  
   <xsl:template match="docTitle">
     <fo:block xsl:use-attribute-sets="doc-title">
       <xsl:apply-templates/>
     </fo:block>
   </xsl:template>

   <xsl:template match="titlePart">
     <fo:block>
      <xsl:apply-templates/>
     </fo:block>
   </xsl:template>

  <xsl:template match="body/head">
    <fo:block xsl:use-attribute-sets="main-head">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="div/head">
    <fo:block xsl:use-attribute-sets="chapter-head">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  
  <xsl:template match="p">
    <fo:block xsl:use-attribute-sets="paragraph">
      <xsl:apply-templates/>
    </fo:block>    
  </xsl:template>
  
  <xsl:template match="teiHeader//p" priority="2">
    <fo:block xsl:use-attribute-sets="metadata-paragraph">
      <xsl:apply-templates/>
    </fo:block>    
  </xsl:template>
  
  <xsl:template match="div/p[1]">
    <fo:block xsl:use-attribute-sets="first-paragraph">
      <xsl:apply-templates/>
    </fo:block>    
  </xsl:template>
  
  <xsl:template match="body/div/p[1]/text()[1]">
    <fo:float float="start">
          <fo:block xsl:use-attribute-sets="drop-cap">
            <xsl:value-of select="substring(.,1,1)"/>
          </fo:block>
    </fo:float>
    <xsl:value-of select="substring(.,2)"/>
  </xsl:template>
  
  <xsl:template match="note/p[1]">
    <fo:block xsl:use-attribute-sets="paragraph">
      <xsl:for-each select="../@xml:id">
        <xsl:attribute name="id" select="."/>
      </xsl:for-each>
      <xsl:apply-templates select="parent::note" mode="number"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates/>
    </fo:block>    
  </xsl:template>
  
  <xsl:template match="byline">
    <fo:block xsl:use-attribute-sets="title-page">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>

  <xsl:template match="epigraph">
    <fo:block xsl:use-attribute-sets="epigraph">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="epigraph/quote">
    <fo:block xsl:use-attribute-sets="italic">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="epigraph/bibl">
    <fo:block text-align="end"  text-align-last="end">
      <xsl:if test="ancestor::titlePage">
        <xsl:attribute name="space-before">2pt</xsl:attribute>
      </xsl:if>
      <xsl:text>&#8212; </xsl:text>
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="quote">
    <xsl:call-template name="quoted"/>
  </xsl:template>
  
  <xsl:template match="note/quote">
    <fo:block xsl:use-attribute-sets="paragraph">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="bibl">
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="docImprint">
     <fo:block xsl:use-attribute-sets="imprint" space-before="1em">
       <xsl:apply-templates/>
     </fo:block>
   </xsl:template>

  <xsl:template match="closer">
    <fo:block xsl:use-attribute-sets="closer">
      <xsl:apply-templates/>
    </fo:block>
   </xsl:template>

   <xsl:template match="pb">
      <!--<xsl:apply-templates select="@n"/>
      <xsl:apply-templates select="." mode="flag"/>-->
   </xsl:template>

  <xsl:template match="head">
    <xsl:apply-templates select="." mode="flag"/>
  </xsl:template>
  
  <xsl:template match="signed | dateline">    
    <fo:block xsl:use-attribute-sets="first-paragraph">
      <xsl:apply-templates/>
    </fo:block>
   </xsl:template>
   
  <xsl:template match="lb">
    <fo:block>
      <xsl:if test="@type = 'extra'">
        <xsl:attribute name="space-before">2pt</xsl:attribute>
      </xsl:if>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="foreign">
    <fo:inline xsl:use-attribute-sets="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>
  
  <xsl:template match="hi[@rend='italic']">
    <fo:inline xsl:use-attribute-sets="italic">
      <xsl:apply-templates/>
    </fo:inline>
  </xsl:template>
  
  <xsl:template match="note">
    <fo:footnote>
      <xsl:apply-templates select="." mode="number"/>
       <fo:footnote-body xsl:use-attribute-sets="footnote-body">
         <xsl:apply-templates/>
       </fo:footnote-body>
     </fo:footnote>
  </xsl:template>
  
    
  <xsl:template match="note" mode="number">
    <fo:inline xsl:use-attribute-sets="footnote-number">
      <xsl:number level="any" from="div"/>
    </fo:inline>
  </xsl:template>

   <xsl:template match="figure">
     <fo:float float="start">
       <fo:block-container width="100%" text-indent="0em">
         <xsl:apply-templates/>
       </fo:block-container>
     </fo:float>
   </xsl:template>

   <xsl:template match="graphic">
     <fo:block text-align="center" text-align-last="center"
       space-before="6pt" space-after="6pt">
       <fo:external-graphic src="url('{replace(@url,'\.png$','.svg')}')"
         width="80%" content-width="scale-to-fit"/>
     </fo:block>
   </xsl:template>

  <xsl:template match="ref">
    <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="ref[normalize-space(.) =
    'Compare p. 80, note']">
    <xsl:text>Compare p. </xsl:text>
    <fo:page-number-citation ref-id="{replace(@target,'^#','')}"/>
    <xsl:text>, note</xsl:text>
  </xsl:template>
  
  <xsl:template name="quoted">
    <xsl:text>&#8220;</xsl:text>
    <xsl:apply-templates/>  
    <xsl:text>&#8221;</xsl:text>
  </xsl:template>

    <xsl:template match="q">
      <xsl:call-template name="quoted"/>
    </xsl:template>
   
    <xsl:template match="p/q">
      <xsl:call-template name="quoted"/>
    </xsl:template>
    
  
   <xsl:template match="soCalled">
     <xsl:call-template name="quoted"/>
   </xsl:template>

   <xsl:template match="gi | att | val | code">
     <fo:inline xsl:use-attribute-sets="code">
       <xsl:apply-templates/>
     </fo:inline>
   </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"/>
    <fo:block>
      <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>
    </fo:block>
  </xsl:template>
  
  <xsl:template name="tag">
    <xsl:param name="start" select="false()"/>
    <xsl:param name="end" select="false()"/>
    <xsl:param name="empty" select="false()"/>
    <fo:inline color="darkred">
      <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>
          <xsl:value-of select="."/>
          <xsl:text>"</xsl:text>
        </xsl:for-each>
      </xsl:if>
      <xsl:if test="$empty">/</xsl:if>
      <xsl:text>&gt;</xsl:text>
    </fo:inline>
  </xsl:template>
  
  <xsl:template name="match-msg">
    <xsl:message>
      <xsl:value-of select="name()"/>
      <xsl:text> matched</xsl:text>
    </xsl:message>
  </xsl:template>
  
  <xsl:variable name="colophon" as="element()">
    <div type="colophon" xmlns="http://www.tei-c.org/ns/1.0">
      <head>Colophon</head>
      <p>This edition of Gustav Fechner&#8217;s <title level="m">Büchlein vom
        Leben nach dem Tode</title> (Treatise Concerning Life After Death) was
        prepared starting with an OCR (optical character recognition) scan of
        the 1904 edition, acquired on the Internet. By means of a pipeline of
        XSLT stylesheets (six simple transformations in total) the plain text
        was converted into a rudimentary XML format conformant to the Guidelines
        of the Text Encoding Initiative. Copy editing to correct the text and
        enhance the encoding was then accomplished by hand, along with
        development of transformations to convert the text into HTML for web
        display and to XSL formatting objects (FO) for conversion into PDF. The
        image appearing in Chapter 3 was encoded in SVG by hand, as was the
        cover art.</p>
      <p>All editing and transformation work was done in oXygen XML Editor. PDF
        was generated using the RenderX XEP processor.</p>
      <p>The design of the page layout is based on the design of the original
        1904 edition, with some adjustments for readability. Hyphenation is
        provided by XEP using a TeX algorithm. The font is Cambria (distributed
        in Microsoft Windows 7) with Tahoma for metadata and colophon
        pages.</p>
      <p>For more information on this project, see 
        <ref href="http://piez.org/wendell/projects/buechlein">http://piez.org/wendell/&#x200b;projects/buechlein/</ref>.</p>
    </div>
  </xsl:variable>
  
  <xsl:template match="div[@type='colophon']">
    <fo:page-sequence master-reference="metadata-sequence"
      initial-page-number="auto-odd"
      xsl:use-attribute-sets="metadata">     
      <xsl:call-template name="watermark"/>
      <fo:flow flow-name="content">
        <fo:block>
          <xsl:apply-templates/>
        </fo:block>
      </fo:flow>
    </fo:page-sequence>
  </xsl:template>
  
  <xsl:template match="div[@type='colophon']/head" priority="3">
    <fo:block xsl:use-attribute-sets="metadata-title">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
  <xsl:template match="div[@type='colophon']/*" priority="2">
    <fo:block xsl:use-attribute-sets="metadata-paragraph">
      <xsl:apply-templates/>
    </fo:block>
  </xsl:template>
  
</xsl:stylesheet>