<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY ldquo '&#x201C;' >
<!ENTITY rdquo '&#x201D;' >
<!ENTITY lsquo '&#x2018;' >
<!ENTITY rsquo '&#x2019;' >
<!ENTITY mdash "&#x2014;" >
<!ENTITY nbsp  "&#xA0;" >
]>

<!-- ============================================================= -->
<!--  DHQuarterly article
      proof transformation
      Wendell Piez
      July-August 2005, September 2006                             -->
<!-- ============================================================= -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:q="http://digitalhumanities.org/DHQ/namespace">

  <xsl:output method="html" indent="yes"/>

  <!-- xsl:import href="fallback.xsl"/ -->

  <xsl:strip-space
    elements="q:DHQarticle q:DHQheader
    q:abstract q:text q:epigraph q:xtext q:div
    q:table q:letter q:ps q:closer q:opener q:cit q:list
    q:epigraph q:lg q:sp q:example q:figure q:lb q:ptr q:notes
    q:figures q:listBibl"/>

  <xsl:key name="element-by-id" match="*[@id]" use="@id"/>

  <xsl:variable name="all-notes" select="//q:note"/>

  <xsl:variable name="all-bibls" select="//q:bibl[not(@type='reviewTarget')]"/>

  <xsl:param name="css">
    <xsl:text>

body { line-height: 1.6em;
       font-family: Arial, Arial Unicode MS, Lucida Grande, sans-serif; }

div#DHQheader
        { margin-top: 0.5em;
          margin-bottom: 0.5em;
          margin-left: 1em;
          margin-right: 1em;
          background-color: lightgrey;
          border: 1px darkslategray solid;
          padding-left: 0.5em;
          padding-right: 0.5em;
          padding-top: 0em;
          padding-bottom: 0.5em }

#DHQheader h3
        { margin-top: 0.5em;
          margin-bottom: 0em }

#DHQheader p
        { margin-left:1em;
          text-indent: -1em;
          margin-top: 0em;
          margin-bottom: 0.5em }


div.abstract,
div.teaser   { font-family: sans-serif;
               font-size: smaller;
               line-height: 120%;
                }

div.epigraph,
div.citation {  font-size: 90%;
                margin-top: 1em;
                margin-bottom: 1em;
                margin-left: 10%;
                margin-right: 10%;
                border: thin black dashed;
                padding-left: 0.5em;
                padding-right: 0.5em;
                padding-top: 0.5em;
                padding-bottom: 0.5em }

div.xtext { font-size: 90%;
            margin-top: 1em;
            margin-bottom: 1em;
            margin-left: 10%;
            margin-right: 10%;
            border: thin black dashed;
            padding-left: 0.5em;
            padding-right: 0.5em;
            padding-top: 0.5em;
            padding-bottom: 0.5em }

div.reviewTarget
          { font-size: 90%;
            margin-top: 1em;
            margin-bottom: 1em;
            margin-left: 1.5em;
            margin-right: 1.5em;
            border: thin black dashed;
            padding-left: 0.5em;
            padding-right: 0.5em;
            padding-top: 0.5em;
            padding-bottom: 0.5em }

div.blockquote  { font-size: 90%;
                  margin-left: 10%;
                  margin-right: 10%;
                  margin-top: 1em;
                  margin-bottom: 1em }

div.linegroup { margin-top: 1em;
                margin-bottom: 1em;
                margin-left: 2em;
                margin-right: 2em }

h1, h2, h3, h4, h5, h6 { line-height: 1.2em }

h4.header-div-head
          { margin-top: 0.5em;
            margin-bottom: 0.5em;
            padding-bottom: 0.25em;
            border-bottom: thin solid black;
            font-style: italic }

h2.xtext-head
          { margin-top: 0em;
            font-style: italic;
            text-align: center }

h2.xtext-head emph { font-style: normal }

h5.table-h5,
h5.figure-h5,
h5.example-h5
          { margin-top: 0em;
            margin-bottom: 0.5em }

p.salute,
p.signed,
p.dateline
           { margin-top: 0em;
             margin-bottom: 0em }

div.example   { margin-top: 0.5em;
                margin-bottom: 0.5em;
                margin-left: 1em;
                margin-right: 1em;
                border: thin black dashed;
                padding-left: 0.5em;
                padding-right: 0.5em;
                padding-top: 0.5em;
                padding-bottom: 0.5em }

table.outer {   margin-top: 0.5em;
                margin-bottom: 0.5em;
                border: thin black dashed;
                margin-left: 1em;
                margin-right: 1em;
                padding-left: 0.5em;
                padding-right: 0.5em;
                padding-top: 0.5em;
                padding-bottom: 0.5em }

table.inner  {   }

div.figure   {  margin-top: 0.5em;
                margin-bottom: 0.5em;
                margin-left: 1em;
                margin-right: 1em;
                border: thin black dashed;
                padding-left: 0.5em;
                padding-right: 0.5em;
                padding-top: 0.5em;
                padding-bottom: 0.5em }

div.letter   { margin-top: 0.5em;
                margin-bottom: 0.5em;
                margin-left: 2em;
                margin-right: 2em;
                border: thin black dashed;
                padding-left: 0.5em;
                padding-right: 0.5em;
                padding-top: 0.5em;
                padding-bottom: 0.5em }

p.line { margin-left:1em;
          text-indent: -1em;
          margin-top: 0em;
          margin-bottom: 0em }

p.tagline { margin-left:40%;
            text-indent: -5%;
            margin-top: 0em;
            margin-bottom: 0em }

div.postscript { margin-top: 1em;
                 margin-bottom: 0em }

div.opener { margin-top: 0em;
             margin-bottom: 0em }

li ul { margin-top: 0em;
        margin-bottom: 0em }

li p { margin-top: 0em;
       margin-bottom: 0em }

div.closer { margin-top: 0em;
             margin-bottom: 0em ;
             margin-left: 40% }

div.endmatter { }

.div-label { margin-bottom: 0em;
              font-style: italic }

div.caption { font-size: 90%;
              margin-top: 0.5em; }

.item-label { font-weight: bold }

td.item-label
  { vertical-align: text-top;
    text-align: right }

td.item-entry { vertical-align: text-top }

table.glossary { border-collapse: collapse;
                 border-top: thin solid black;
                 margin-top: 0.5em;
                 margin-bottom: 0.5em }

tr.gloss-item td { border-bottom: thin solid black }

.gloss-entry
  { margin-top: 0.5em;
    margin-bottom: 0.5em;
    padding: 0.5em }

dd.plain-list { }

ul, ol { margin-top: 0.5em;
         margin-bottom: 0.5em }


a.ref { font-size: 75%;
        font-family: sans-serif;
        font-weight: bold;
        font-style: normal }

div.endnote { margin: 0.5em;
              padding: 0.5em;
              border: thin black dashed }

a.note-num { float: left;
             margin-right: 0.5em;
             font-size: 120%;
             padding-right: 0.25em;
             padding-left: 0.25em }

td { padding: 0.25em;
     margin: 0.25em }

td.label {  }

    </xsl:text>
  </xsl:param>

  <xsl:template match="/">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:DHQarticle | q:DHQdraft">
    <html>
      <head>
        <title>
          <xsl:value-of select="(/*/q:DHQheader/q:title | q:text/q:head)[1]"/>
        </title>
        <style type="text/css">
          <xsl:copy-of select="$css"/>
      </style>
      </head>
      <body>
        <xsl:apply-templates/>
        <xsl:call-template name="notes"/>
        <xsl:call-template name="bibliography"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="q:DHQheader">
    <div id="DHQheader">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:abstract | q:teaser" priority="1">
    <xsl:call-template name="header-div">
      <xsl:with-param name="content">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:DHQheader/q:author"/>

  <xsl:template match="q:DHQheader/*" priority="0"/>

  <xsl:template match="q:DHQheader/q:title" priority="2">
    <div>
      <h3>
        <xsl:apply-templates/>
      </h3>
      <p>
        <xsl:for-each select="following-sibling::q:author">
          <xsl:if test="preceding-sibling::q:author">
            <xsl:choose>
              <xsl:when test="following-sibling::q:author">, </xsl:when>
              <xsl:otherwise> and </xsl:otherwise>
            </xsl:choose>
          </xsl:if>
          <xsl:apply-templates select="q:name | text()[normalize-space()]"/>
        </xsl:for-each>
      </p>
    </div>
  </xsl:template>

  <xsl:template match="q:family">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="q:DHQheader/q:history | q:DHQheader/q:langUsage"/>

  <!--<xsl:template match="q:teaser">
  <xsl:call-template name="header-div">
    <xsl:with-param name="content">
      <p style="margin-top:0em;margin-bottom:0em">
        <xsl:apply-templates/>
      </p>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>
-->

  <xsl:template name="header-div">
    <!-- makes a div for an arbitrary element in the header -->
    <xsl:param name="content">
      <xsl:apply-templates/>
    </xsl:param>
    <div class="{local-name()}">
      <h4 class="header-div-head">
        <xsl:value-of select="local-name()"/>
      </h4>
      <xsl:copy-of select="$content"/>
    </div>
  </xsl:template>

  <xsl:template match="q:text">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:xtext">
    <div class="xtext">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:epigraph">
    <div class="epigraph">
      <xsl:apply-templates select="*[not(self::q:ref|self::q:ptr|self::q:bibl)]"/>
      <p class="tagline">
        <xsl:apply-templates select="q:ref | q:ptr | q:bibl"/>
      </p>
    </div>
  </xsl:template>

  <xsl:template match="q:p">
    <xsl:variable name="whitespace-munge">
      <xsl:if test="not(preceding-sibling::*[not(self::q:label)])">
        <xsl:text>margin-top:0em;</xsl:text>
      </xsl:if>
      <xsl:if test="not(following-sibling::*)">
        <xsl:text>margin-bottom:0em;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <p>
      <xsl:if test="string($whitespace-munge)">
        <xsl:attribute name="style">
          <xsl:value-of select="$whitespace-munge"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="q:eg">
    <pre class="eg">
      <xsl:apply-templates/>
    </pre>
  </xsl:template>

  <xsl:template match="q:lg">
    <div class="linegroup">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:lg//q:lg | q:epigraph//q:lg">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:l">
    <p class="line">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:variable name="number-divs" select="boolean(//q:div[not(q:head)])"/>
  <!-- we want to number our divs if there exist any headless divs:
       if such exist this variable is bound to 'true()' -->

  <xsl:template match="q:div">
    <xsl:if test="not(q:head)">
      <xsl:call-template name="make-head">
        <xsl:with-param name="content">
          <xsl:call-template name="div-number"/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template name="make-head">
    <xsl:param name="context" select="."/>
    <xsl:param name="content">
      <xsl:text>[Warning: template 'make-head' received no content]</xsl:text>
    </xsl:param>
    <xsl:param name="htmltag">
      <xsl:text>h</xsl:text>
      <xsl:variable name="level" select="count(ancestor-or-self::q:div) + 1"/>
      <xsl:choose>
        <xsl:when test="$level &lt;= 6">
          <xsl:value-of select="$level"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>6</xsl:text>
          <xsl:message>warning: div deeper than 6 encountered</xsl:message>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:param>
    <xsl:element name="{$htmltag}">
      <xsl:attribute name="class">
        <xsl:value-of select="local-name($context)"/>
        <xsl:text>-</xsl:text>
        <xsl:value-of select="$htmltag"/>
      </xsl:attribute>
      <a name="{generate-id($context)}">
        <xsl:copy-of select="$content"/>
      </a>
    </xsl:element>
  </xsl:template>

  <xsl:template match="q:text/q:head">
    <!-- this code is complex because a div has to get a header
      even when the div has no 'head' child -->
    <xsl:call-template name="make-head">
      <xsl:with-param name="context" select=".."/>
      <xsl:with-param name="content">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="div-number">
    <xsl:number level="multiple" format="I.1 " count="q:div"/>
  </xsl:template>

  <xsl:template match="q:div/q:head">
    <!-- this code is complex because a div has to get a header
      even when the div has no 'head' child -->
    <xsl:call-template name="make-head">
      <xsl:with-param name="context" select=".."/>
      <xsl:with-param name="content">
        <!-- we provide a number if this heads a div and if any div in the
          document has no head -->
        <xsl:if test="$number-divs">
          <xsl:call-template name="div-number"/>
        </xsl:if>
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:appendix/q:head">
    <xsl:call-template name="make-head">
      <xsl:with-param name="context" select=".."/>
      <xsl:with-param name="content">
        <!-- we provide a number if this heads a div and if any div in the
          document has no head -->
        <xsl:text>Appendix </xsl:text>
        <xsl:number/>
        <xsl:text>: </xsl:text>
        <xsl:apply-templates/>
      </xsl:with-param>
      <xsl:with-param name="htmltag">h2</xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:head[parent::q:xtext]">
    <!-- this code is complex because a div has to get a header
       even when the div has no 'head' child -->
    <h2 class="xtext-head">
      <xsl:apply-templates/>
    </h2>
  </xsl:template>

  <xsl:template match="q:cit">
    <xsl:apply-templates select="q:quote"/>
    <xsl:apply-templates
      select="*[not(self::q:quote) and not(../q:quote[@rend='block'])]"/>
    <!-- other children are handled inside a quote[@rend='block'],
         but not otherwise -->
  </xsl:template>
  
  <xsl:template match="q:letter">
    <div class="letter">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:figure | q:example">
    <xsl:call-template name="make-labeled-div"/>
  </xsl:template>

  <xsl:template match="q:table">
    <table class="outer" id="{generate-id()}">
      <tr>
        <td>
          <h5 class="table-h5">
            <xsl:apply-templates select="." mode="label"/>
            <xsl:if test="normalize-space(q:label)"> &mdash; </xsl:if>
            <xsl:apply-templates select="q:label"/>
          </h5>
        </td>
      </tr>
      <tr>
        <td>
          <table class="inner">
            <xsl:apply-templates select="q:row"/>
          </table>
        </td>
      </tr>
      <xsl:for-each select="q:caption">
        <tr>
          <td>
            <xsl:apply-templates select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="make-labeled-div">
    <xsl:param name="id" select="generate-id()"/>
    <xsl:param name="label">
      <xsl:apply-templates select="." mode="label"/>
      <xsl:if test="normalize-space(q:label)"> &mdash; </xsl:if>
      <xsl:apply-templates select="q:label"/>
    </xsl:param>
    <xsl:param name="contents">
      <xsl:apply-templates select="*[not(self::q:label)]"/>
    </xsl:param>
    <div class="{local-name()}" id="{$id}">
      <xsl:call-template name="make-head">
        <xsl:with-param name="context" select="."/>
        <xsl:with-param name="content">
          <xsl:copy-of select="$label"/>
        </xsl:with-param>
        <xsl:with-param name="htmltag" select="'h5'"/>
      </xsl:call-template>
      <xsl:copy-of select="$contents"/>
    </div>
  </xsl:template>

  <xsl:template match="q:row">
    <tr>
      <xsl:apply-templates/>
    </tr>
  </xsl:template>

  <xsl:template match="q:cell">
    <td>
      <xsl:for-each select="@role">
        <xsl:attribute name="class">
          <xsl:value-of select="."/>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:apply-templates/>
    </td>
  </xsl:template>

  <xsl:template match="q:graphic">
    <img src="{@url}">
      <xsl:for-each select="../q:figDesc">
        <xsl:attribute name="title">
          <xsl:apply-templates mode="plain-text"/>
        </xsl:attribute>
      </xsl:for-each>
    </img>
  </xsl:template>

  <xsl:template match="q:figDesc"/>

  <xsl:template match="q:caption">
    <div class="caption">
      <xsl:choose>
        <xsl:when test="text[normalize-space()]">
          <p class="caption">
            <xsl:apply-templates/>
          </p>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates/>
        </xsl:otherwise>
      </xsl:choose>
    </div>
  </xsl:template>
  <!-- xsl:template match="q:quote[@rend='block']
  [*[not(self::lg|self::letter)]]" priority="2">
  <!- - if we have only letter or line group children, we don't
       indent them extra - ->
  <xsl:apply-templates/>
</xsl:template -->

  <xsl:template match="q:quote[@rend='block']">
    <div class="blockquote">
      <xsl:apply-templates/>
      <xsl:for-each select="parent::q:cit/q:ptr | parent::q:cit/q:ref | parent::q:cit/q:bibl">
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="."/>
      </xsl:for-each>
    </div>
  </xsl:template>

  <xsl:template match="q:quote[@rend='inline']">
    <xsl:call-template name="quotes">
      <xsl:with-param name="contents">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:opener | q:closer">
    <div class="{local-name()}">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:salute | q:signed | q:dateline">
    <p class="{local-name()}">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="q:ps">
    <div class="postscript">
      <p
        style="float:left; font-style: italic; font-weight:bold;
      margin-top:0em; margin-bottom:0em"
        >P.S.&nbsp;&mdash;&nbsp;</p>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:list[not(@type) or @type='unordered']">
    <!-- values of @type can be:
         "ordered" | "unordered" | "simple" | "gloss" -->
    <ul class="unordered">
      <xsl:apply-templates select="q:item"/>
    </ul>
  </xsl:template>

  <xsl:template match="q:list[@type='ordered']">
    <ol>
      <xsl:apply-templates/>
    </ol>
  </xsl:template>

  <xsl:template match="q:list[@type='simple']">
    <dl>
      <xsl:apply-templates/>
    </dl>
  </xsl:template>

  <xsl:template match="q:list[@type='gloss']">
    <!-- values of @type can be:
       "ordered" | "unordered" | "simple" | "gloss" -->
    <table class="glossary">
      <xsl:apply-templates/>
    </table>
  </xsl:template>

  <xsl:template match="q:list[@type='gloss']/q:item">
    <tr class="gloss-item">
      <td class="item-label">
        <xsl:apply-templates select="q:label"/>
      </td>
      <td class="item-entry">
        <xsl:apply-templates mode="in-gloss-list"/>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="q:label" mode="in-gloss-list"/>

  <xsl:template match="*" mode="in-gloss-list">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:item">
    <li>
      <xsl:apply-templates/>
    </li>
  </xsl:template>

  <xsl:template match="q:list[@type='simple']/q:item">
    <dd class="plain-list">
      <xsl:apply-templates/>
    </dd>
  </xsl:template>

  <xsl:template match="q:item/q:label">
    <div class="item-label">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template
    match="q:item/q:label[../text()[normalize-space() and not(parent::list/@type='gloss')]]"
    priority="2">
    <span class="item-label">
      <xsl:apply-templates/>
      <xsl:text> &mdash;</xsl:text>
    </span>
  </xsl:template>

  <xsl:template match="q:code">
    <tt class="{local-name()}">
      <xsl:apply-templates/>
    </tt>
  </xsl:template>

  <xsl:template match="q:foreign | q:emph">
    <i class="{local-name()}">
      <xsl:apply-templates/>
    </i>
  </xsl:template>

  <xsl:template match="q:term">
    <b class="{local-name()}">
      <xsl:apply-templates/>
    </b>
  </xsl:template>

  <xsl:template match="q:q">
    <xsl:call-template name="quotes">
      <xsl:with-param name="contents">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:hi">
    <emph>
      <xsl:apply-templates/>
    </emph>
  </xsl:template>

  <xsl:template match="q:hi[@rend]">
    <xsl:variable name="contents">
      <span>
        <xsl:variable name="rend">
          <xsl:if test="contains(@rend,'bold')">font-weight: bold;</xsl:if>
          <xsl:if test="contains(@rend,'italic')">font-style: italic;</xsl:if>
          <xsl:if test="contains(@rend,'monospace')">font-family:monospace;</xsl:if>
          <xsl:if test="contains(@rend,'smcaps')">font-variant:small-caps;</xsl:if>
          <xsl:if test="contains(@rend,'subscript')">vertical-align:sub;font-size:80%;</xsl:if>
          <xsl:if test="contains(@rend,'superscript')">vertical-align:super;font-size:80%;</xsl:if>
          <xsl:if test="contains(@rend,'underline')">text-decoration: underline;</xsl:if>
          <xsl:if test="contains(@rend,'strikethrough')">text-decoration: line-through;</xsl:if>
        </xsl:variable>
        <xsl:if test="normalize-space($rend)">
          <xsl:attribute name="style">
            <xsl:value-of select="$rend"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates/>
      </span>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains(@rend,'quotes')">
        <xsl:call-template name="quotes">
          <xsl:with-param name="contents" select="$contents"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$contents"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <xsl:template match="q:gi | q:class | q:tag | q:val">
    <tt>
      <xsl:apply-templates/>
    </tt>
  </xsl:template>

  <xsl:template match="q:att">
    <tt>@<xsl:text/>
      <xsl:apply-templates/>
    </tt>
  </xsl:template>

  <xsl:template match="q:called">
    <xsl:call-template name="quotes">
      <xsl:with-param name="contents">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:listBibl | q:notes"/>
  <!-- These are picked up in the named templates
    'bibliography' and 'notes' and can be suppressed from the default traversal -->

  <xsl:template match="q:appendix">
    <div class="appendix">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:label">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template
    match="q:imprint | q:pubPlace | q:publisher |
    q:author | q:name | q:vol">
    <span class="{local-name()}">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="q:idno">
    <span class="{@type}">
      <xsl:text> [</xsl:text>
      <xsl:for-each select="@type">
        <xsl:value-of select="."/>
        <xsl:text>: </xsl:text>
      </xsl:for-each>
      <xsl:apply-templates/>
      <xsl:text>]</xsl:text>
    </span>
  </xsl:template>

  <xsl:template match="q:title[@rend='quotes']">
    <xsl:call-template name="quotes">
      <xsl:with-param name="contents">
        <xsl:apply-templates/>
      </xsl:with-param>
    </xsl:call-template>
  </xsl:template>

  <xsl:template match="q:title[@rend='italic' or not(@rend)]">
    <i class="title">
      <xsl:apply-templates/>
    </i>
  </xsl:template>

  <xsl:template match="q:title[@rend='none']">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:date">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="q:bibl">
    <xsl:apply-templates select="." mode="label"/>
  </xsl:template>

  <xsl:template match="q:bibl[@type='reviewTarget']">
    <div class="reviewTarget">
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="q:bibl/q:label"/>

  <xsl:template match="q:note">
    <xsl:apply-templates select="." mode="generated-reference"/>
  </xsl:template>

  <xsl:template match="q:ref[not(@target)]">
    <xsl:for-each select="parent::q:cit"> [</xsl:for-each>
    <xsl:apply-templates/>
    <xsl:for-each select="parent::q:cit">]</xsl:for-each>
  </xsl:template>

  <xsl:template match="q:ref">
    <xsl:for-each select="parent::q:cit"> [</xsl:for-each>
    <xsl:variable name="target">
    <xsl:choose>
      <xsl:when test="starts-with(@target,'#')">
        <xsl:text>#</xsl:text>
        <xsl:value-of select="generate-id(key('element-by-id',substring-after(@target,'#')))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@target"/>
      </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    <a class="ref" href="{$target}">
      <xsl:if test="not(starts-with(@target,'#'))">
        <xsl:attribute name="target">_new</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </a>
    <xsl:for-each select="parent::q:cit">]</xsl:for-each>
  </xsl:template>

  <xsl:template match="q:ptr">
    <!-- When @target does not start with '#' this is
    assumed to be an external reference -->
    <xsl:choose>
      <xsl:when test="normalize-space(@target)">
        <xsl:for-each select="parent::q:cit">
          <xsl:text> </xsl:text>
        </xsl:for-each>
        <a class="ref" href="{@target}">
          <xsl:text>&lt;</xsl:text>
          <xsl:value-of select="@target"/>
          <xsl:text>&gt;</xsl:text>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>ptr element not processed (it no @target)</xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="q:ptr[starts-with(@target,'#')]">
    <!-- may point at anything with an id, including
    tables, notes, bibls, figures, examples, divs -->
    <xsl:variable name="target" select="substring-after(@target,'#')"/>
    <xsl:apply-templates select="key('element-by-id',$target)"
      mode="generated-reference">
      <xsl:with-param name="loc" select="@loc"/>
      <!-- pointers to bibl elements accept $loc parameters -->
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="*" mode="generated-reference">
    <a href="#{generate-id()}" class="ref">
      <xsl:apply-templates select="." mode="label"/>
    </a>
  </xsl:template>

  <xsl:template match="q:bibl" mode="generated-reference">
    <xsl:param name="loc" select="false()"/>
    <xsl:text>(</xsl:text>
    <a href="#{generate-id()}" class="ref">
      <xsl:apply-templates select="." mode="label"/>
    </a>
    <xsl:if test="$loc">
      <xsl:text>, </xsl:text>
      <xsl:value-of select="$loc"/>
    </xsl:if>
    <xsl:text>)</xsl:text>
  </xsl:template>

  <xsl:template match="q:note" mode="generated-reference">
    <a class="ref" href="#{generate-id()}">
      <xsl:text>[</xsl:text>
      <xsl:number level="any"/>
      <xsl:text>]</xsl:text>
    </a>
  </xsl:template>

  <xsl:template match="q:table" mode="label">
    <xsl:text>Table </xsl:text>
    <xsl:number level="any" from="q:xtext | /"/>
  </xsl:template>

  <xsl:template match="q:example" mode="label">
    <xsl:text>Example </xsl:text>
    <xsl:number level="any" from="q:xtext | /"/>
  </xsl:template>

  <xsl:template match="q:figure" mode="label">
    <xsl:text>Figure </xsl:text>
    <xsl:number level="any" from="q:xtext | /"/>
  </xsl:template>

  <xsl:template match="q:bibl" mode="label">
    <xsl:for-each select="parent::q:cit">
      <xsl:text> </xsl:text>
    </xsl:for-each>
    <xsl:text>[</xsl:text>
    <xsl:apply-templates select="q:label" mode="label"/>
    <xsl:text>]</xsl:text>
  </xsl:template>

  <xsl:template match="q:bibl/q:label" mode="label">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="*" mode="label">
    <xsl:value-of select="local-name()"/>
    <xsl:text> </xsl:text>
    <xsl:number level="any"/>
  </xsl:template>
  <!--
<xsl:number
  count="note[not(@id=preceding::ptr/@target] |
  ptr[key('elements-by-id',@target)/self::note]
     [not(@target = preceding::ptr/@target or @target = preceding::note/@id]"/>

<xsl:key name="notes-and-noterefs"
  match="note|ptr" use="self::node/@id | self::ptr/@target"/>
-->

  <xsl:template name="notes">
    <xsl:if test="$all-notes">
      <div class="endmatter">
        <h2>Notes</h2>
        <xsl:apply-templates select="//q:note" mode="notes"/>
        <!-- modify this to present sorted by first appearance
           *or reference*, whichever comes first -->
      </div>
    </xsl:if>
  </xsl:template>

  <xsl:template name="bibliography">
    <xsl:if test="$all-bibls">
      <div class="endmatter">
        <h2>Bibliography</h2>
        <xsl:apply-templates select="//q:bibl" mode="bibliography">
          <xsl:sort data-type="text" select="q:label"/>
        </xsl:apply-templates>
      </div>
    </xsl:if>
  </xsl:template>

  <xsl:template match="q:figures">
    <!-- we output a section for figures if there's one in the input;
       it will include all figures, however (not just the ones here). -->
    <div class="endmatter">
      <h2>Figures</h2>
      <xsl:apply-templates select="//q:figure" mode="figures"/>
    </div>
  </xsl:template>

  <xsl:template match="q:figure" mode="figures">
    <xsl:choose>
      <xsl:when test="parent::q:figures">
        <xsl:apply-templates select="."/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select=".">
          <xsl:with-param name="id" select="concat(generate-id(),'-shadow')"/>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="q:bibl" mode="bibliography">
    <p class="bibl-entry">
      <a name="{generate-id()}" class="ref">
        <xsl:apply-templates select="q:label" mode="label"/>
      </a>
      <xsl:text> </xsl:text>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="q:note[text()[normalize-space()]]" mode="notes">
    <div class="endnote">
      <a class="note-num" name="{generate-id()}">
        <xsl:text>[</xsl:text>
        <xsl:number level="any"/>
        <xsl:text>]</xsl:text>
      </a>
      <p style="margin-top:0em; margin-bottom: 0em">
        <xsl:apply-templates/>
      </p>
    </div>
  </xsl:template>

  <xsl:template match="q:note" mode="notes">
    <!-- if no PCDATA is present we assume we have block content -->
    <div class="endnote">
      <a class="note-num" name="{generate-id()}">
        <xsl:text>[</xsl:text>
        <xsl:number level="any"/>
        <xsl:text>]</xsl:text>
      </a>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template name="quotes">
    <xsl:param name="contents">
      <xsl:apply-templates/>
    </xsl:param>
    <xsl:variable name="level">
      <xsl:value-of
        select="count(ancestor::q:quote[@rend='inline'] |
                    ancestor::q:called |
                    ancestor::q:q |
                    ancestor::q:title[@rend='quotes'])"
      />
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$level mod 2">
        <xsl:text>&lsquo;</xsl:text>
        <xsl:copy-of select="$contents"/>
        <xsl:text>&rsquo;</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>&ldquo;</xsl:text>
        <xsl:copy-of select="$contents"/>
        <xsl:text>&rdquo;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
