Endnotes

From TEIWiki
Jump to navigation Jump to search

This xslt transforms tei:note elements for display in xhtml. It places a numbered marker at the insertion point and reproduces the note content in a separate section at the end of the document. Links between the insertion marker and note are automatically generated.

Input TEI

<p>...Lorem ipsum dolor sit amet, consectetuer 
adipiscing elit.<note><p>Suspendisse faucibus. 
Nulla a tortor id pede aliquam venenatis.</p>
<p>Donec eget lectus.</p></note> Fusce hendrerit, 
quam ac nonummy rutrum, turpis odio ultrices tortor...</p>

Output

XHTML 1.0 (Strict)

At the insertion point:

<p>...Lorem ipsum dolor sit amet, consectetuer 
adipiscing elit.<sup><a name="refpoint-1" 
href="#note-1" title="Link to note 1 at the end 
of this document."><span class="hideme">[</span>1<span 
class="hideme">]</span></a></sup> Fusce hendrerit, 
quam ac nonummy rutrum, turpis odio ultrices tortor...</p>

Elsewhere in the document

  <hr class="subdivider"/>
  <div class="notes">
   <h2><a id="notes"/>Notes:</h2>
   <div class="footnote"><p>
      <a id="note-1" href="#refpoint-1" title="Link to insertion point 
       of note in main text."><span class="hideme">[</span>1<span 
       class="hideme">]</span></a>. Suspendisse faucibus. 
       Nulla a tortor id pede aliquam venenatis.</p>
       <p>Donec eget lectus.</p>
   </div>
  <!-- [other html:div[class='note']] -->
  </div>

XSLT

The same stylesheet works in XSLT 1.0 and XSLT 2.0.

Create note section

The following template should be called (using <xsl:call-template name="notessection"/>) at the point at which you wish the notes to appear in your output document. This call will most likely be in the template for your document root / or TEI.2 templates:

<xsl:template name="notessection">
 <!-- Test if tei:text contains any tei:note elements at any level -->
 <xsl:if test="text//note">
  <!-- If yes: add html:hr element and build html:div for the context note -->
  <hr class="subdivider"/>
  <div class="notes">
   <h2><a id="notes"/>Notes:</h2>
    <xsl:for-each select=".//note">
     <xsl:apply-templates select="." mode="shownotes"/>
    </xsl:for-each></div>
  </xsl:if>
  <!-- If no: do nothing -->
</xsl:template>

Build reference marker and link at insertion point

This code builds the reference marker and link at the insertion point. The text in html:span[class='hideme'] is for use when CSS styles are turned off or in accessibility devices (To ensure they do not appear when the styles are turned on, place the following in your CSS: span.hideme {disрlay:none;}).

<!-- match any tei:note element in tei:text -->
<xsl:template match="text//note">
  <!-- Construct the number for the context note by 
       a) counting all preceding tei:note in tei:text
       b) adding 1 to this number  -->
  <xsl:variable name="notenum" select="count(preceding::note[ancestor::text])+1"/>
  <!-- Normalise the spacing for this number -->
  <xsl:variable name="linklabel" select="normalize-space($notenum)"/>
  <!-- build the link calling the linklable variable -->
  <sup><a>
      <xsl:attribute name="name">refpoint-<xsl:value-of select="$linklabel"/></xsl:attribute>
      <xsl:attribute name="href">#note-<xsl:value-of select="$linklabel"/></xsl:attribute>
      <xsl:attribute name="title">Link to note <xsl:value-of select="$linklabel"/> 
        at the end of this document.</xsl:attribute>
      <span class="hideme">[</span><xsl:value-of select="$linklabel"/>
      <span class="hideme">]</span></a></sup>
  <xsl:text> </xsl:text>
</xsl:template>

Output note content at desired location

This code outputs the not content at the desired location in the output document. Each note is a html:div[class="footnote"] containing one or more html:p. The link back to the insertion point is the note number.

<!-- Match all tei:note elements in tei:text 
     when called by an xsl:apply-template mode="shownotes" -->
<xsl:template match="text//note" mode="shownotes">
  <!-- Generate note number as above -->
  <xsl:variable name="notenum" select="count(preceding::note[ancestor::text])+1"/>
  <xsl:variable name="linklabel" select="normalize-space($notenum)"/>
  <!-- output tei:note
       a) build wrapper -->
  <div class="footnote">
    <!-- test if tei:p[parent::tei:note] is first -->
    <xsl:if test="p[position()=1]">
      <!-- If yes, build html:p, placing note number at beginning -->
      <p>
      <a>
      <xsl:attribute name="id">note-<xsl:value-of select="$linklabel"/></xsl:attribute>
      <xsl:attribute name="href">#refpoint-<xsl:value-of select="$linklabel"/></xsl:attribute>
      <xsl:attribute name="title"><xsl:text>Link to insertion point of note in main 
       text.</xsl:text></xsl:attribute>
      <span class="hideme">[</span>
      <xsl:value-of select="$notenum"/>
          <span class="hideme">]</span>
        </a><xsl:text>. </xsl:text>
        <xsl:apply-templates select="p[position()=1]"/>
        </p>
    </xsl:if>
    <!-- If no output as html:p -->
    <xsl:for-each select="p[not(position()=1)]">
      <p>
        <xsl:apply-templates select="."/>
      </p>
    </xsl:for-each>
  </div>
</xsl:template>

Limitations, assumptions, and quirks

This xslt code is subject to the following limitations, assumptions, and quirks

  1. all tei:note elements must contain at least one tei:p (it is not hard to adjust the code for notes that contain CDATA, however.
  2. the note content is output to html:div rather than html:p, since each note is understood as a collection of paragraphs.