<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml"
  exclude-result-prefixes="exsl estr edate a fo local rng tei teix xd"
  extension-element-prefixes="exsl estr edate" version="1.0"
  xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
  xmlns:edate="http://exslt.org/dates-and-times"
  xmlns:estr="http://exslt.org/strings" xmlns:exsl="http://exslt.org/common"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:html="http://www.w3.org/1999/xhtml"
  xmlns:local="http://www.pantor.com/ns/local"
  xmlns:rng="http://relaxng.org/ns/structure/1.0"
  xmlns:tei="http://www.tei-c.org/ns/1.0"
  xmlns:teix="http://www.tei-c.org/ns/Examples"
  xmlns:xd="http://www.pnp-software.com/XSLTdoc"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xd:doc type="stylesheet">
    <xd:short> TEI stylesheet dealing with elements from the core module, making
      HTML output. </xd:short>
    <xd:detail> This library is free software; you can redistribute it and/or
      modify it under the terms of the GNU Lesser General Public License as
      published by the Free Software Foundation; either version 2.1 of the
      License, or (at your option) any later version. This library is
      distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
      without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
      PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
      details. You should have received a copy of the GNU Lesser General Public
      License along with this library; if not, write to the Free Software
      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA </xd:detail>
    <xd:author>See AUTHORS</xd:author>
    <xd:cvsId>$Id$</xd:cvsId>
    <xd:copyright>2007, TEI Consortium</xd:copyright>
  </xd:doc>
  <xd:doc>
    <xd:short>Process elements *</xd:short>
    <xd:param name="forcedepth">forcedepth</xd:param>
    <xd:detail>
      <p> anything with a head can go in the TOC </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="*" mode="maketoc">
    <xsl:param name="forcedepth"/>
    <xsl:variable name="myName">
      <xsl:value-of select="local-name(.)"/>
    </xsl:variable>
    <xsl:if test="head or $autoHead='true'">
      <xsl:variable name="Depth">
        <xsl:choose>
          <xsl:when test="not($forcedepth='')">
            <xsl:value-of select="$forcedepth"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$tocDepth"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="thislevel">
        <xsl:choose>
          <xsl:when test="$myName = 'div'">
            <xsl:value-of select="count(ancestor::div)"/>
          </xsl:when>
          <xsl:when test="starts-with($myName,'div')">
            <xsl:choose>
              <xsl:when test="ancestor-or-self::div0">
                <xsl:value-of select="substring-after($myName,'div')"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="substring-after($myName,'div') - 1"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:when>
          <xsl:otherwise>99</xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="pointer">
        <xsl:apply-templates mode="generateLink" select="."/>
      </xsl:variable>
      <li class="toc">
        <xsl:call-template name="header">
          <xsl:with-param name="toc" select="$pointer"/>
          <xsl:with-param name="minimal">false</xsl:with-param>
	  <xsl:with-param name="display">plain</xsl:with-param>
        </xsl:call-template>
        <xsl:if test="$thislevel &lt; $Depth">
          <xsl:call-template name="continuedToc"/>
        </xsl:if>
      </li>
    </xsl:if>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements ab</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="ab">
    <div>
      <xsl:call-template name="rendToClass">
	<xsl:with-param name="default">ab</xsl:with-param>
      </xsl:call-template>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements addrLine</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="addrLine">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements address</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="address">
    <div class="address">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements bibl</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="bibl">
    <xsl:choose>
      <xsl:when test="parent::cit">
        <div class="citbibl">
          <xsl:text>(</xsl:text>
          <xsl:apply-templates/>
          <xsl:text>)</xsl:text>
        </div>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements byline</xd:short>
    <xd:detail>
      <p> </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="byline">
    <div class="byline">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element change</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="change">
    <tr>
      <td valign="top" width="15%">
        <xsl:value-of select="date"/>
      </td>
      <td width="85%">
        <xsl:value-of select="item"/>
      </td>
    </tr>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element choice</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="choice">
    <xsl:choose>
      <xsl:when test="abbr and expan">
	<xsl:apply-templates select="expan"/>
	<xsl:text> (</xsl:text>
	<xsl:apply-templates select="abbr"/>
	<xsl:text>)</xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element cit</xd:short>
    <xd:detail>
      <p> quoting </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="cit">
    <xsl:choose>
      <xsl:when test="@rend='display'">
        <blockquote>
	  <xsl:choose>
	    <xsl:when test="@rend">
	      <xsl:attribute name="class">
		<xsl:value-of select="@rend"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:when test="@rendition">
	      <xsl:call-template name="applyRendition"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="class">
		<xsl:text>cit</xsl:text>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>
          <p>
            <xsl:apply-templates select="q|quote"/>
            <xsl:apply-templates select="bibl"/>
          </p>
        </blockquote>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements code</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="code">
    <tt>
      <xsl:apply-templates/>
    </tt>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements  corr</xd:short>
    <xd:detail>Does not do anything yet.</xd:detail>
  </xd:doc>
  <xsl:template match="corr">
    <xsl:apply-templates/>
  </xsl:template>


  <xd:doc>
    <xd:short>Process elements  del</xd:short>
    <xd:detail></xd:detail>
  </xd:doc>
  <xsl:template match="del">
    <span style="text-decoration: line-through;">
      <xsl:apply-templates/>
    </span>
  </xsl:template>


  <xd:doc>
    <xd:short>Process elements eg</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="eg">
    <div>
      <xsl:if test="$cssFile">
        <xsl:attribute name="class">
	  <xsl:text>pre_eg</xsl:text>
	  <xsl:if test="not(*)">
	  <xsl:text> cdata</xsl:text>
	  </xsl:if>
	</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements emph</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="emph">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
	</span>
      </xsl:when>
      <xsl:otherwise>
        <em>
          <xsl:apply-templates/>
        </em>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements epigraph</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="epigraph">
    <div class="epigraph">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements epigraph/lg</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="epigraph/lg">
    <table>
      <xsl:apply-templates/>
    </table>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements foreign</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="foreign">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
	<xsl:apply-templates/>
	</span>
      </xsl:when>
      <xsl:otherwise>
	<span class="foreign">
	      <xsl:apply-templates/>
	</span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements gap</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="gap"> [...]<xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements gi</xd:short>
    <xd:detail>
      <p> special purpose </p >
    </xd:detail>
  </xd:doc>
  <xsl:template match="gi">
    <span class="gi">
      <xsl:text>&lt;</xsl:text>
      <xsl:apply-templates/>
      <xsl:text>&gt;</xsl:text>
    </span>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements gi</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="gi" mode="plain">
    <span class="gi">
      <xsl:text>&lt;</xsl:text>
      <xsl:apply-templates/>
      <xsl:text>&gt;</xsl:text>
    </span>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements att</xd:short>
    <xd:detail>
      <p> special purpose </p >
    </xd:detail>
  </xd:doc>
  <xsl:template match="att">
    <span class="gi">
      <b>@<xsl:apply-templates/></b>
    </span>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements  gloss</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="gloss">
    <span class="gloss">
      <xsl:apply-templates/>
    </span>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements head</xd:short>
    <xd:detail>
      <p> headings etc </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="head">
    <xsl:variable name="parent" select="local-name(..)"/>
    <xsl:if test="not(starts-with($parent,'div'))">
      <xsl:apply-templates/>
    </xsl:if>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element head in plain mode</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="head" mode="plain">
    <xsl:if test="preceding-sibling::head">
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:apply-templates mode="plain"/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements hi</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="hi">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
      <span>
	<xsl:call-template name="applyRendition"/>
	<xsl:apply-templates/>
      </span>
      </xsl:when>
      <xsl:otherwise>
        <strong>
          <xsl:apply-templates/>
        </strong>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements ident</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="ident">
    <xsl:choose>
      <xsl:when test="@type">
        <span class="ident-{@type}">
          <xsl:apply-templates/>
        </span>
      </xsl:when>
      <xsl:otherwise>
        <strong>
          <xsl:apply-templates/>
        </strong>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item" mode="bibl">
    <p>
      <xsl:call-template name="makeAnchor"/>
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item" mode="glosstable">
    <tr>
      <td valign="top">
        <strong>
          <xsl:apply-templates mode="print" select="preceding-sibling::*[1]"
          />
        </strong>
      </td>
      <td>
        <xsl:call-template name="makeAnchor"/>
        <xsl:apply-templates/>
      </td>
    </tr>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item" mode="gloss">
    <dt>
      <xsl:call-template name="makeAnchor"/>
        <xsl:apply-templates mode="print"
          select="preceding-sibling::label[1]"/>
    </dt>
    <dd>
      <xsl:apply-templates/>
    </dd>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item">
    <li>
      <xsl:call-template name="rendToClass"/>
      <xsl:if test="@n">
        <xsl:attribute name="value">
          <xsl:value-of select="@n"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:choose>
	<xsl:when test="@id">
	  <xsl:call-template name="makeAnchor">
	    <xsl:with-param name="name">
	      <xsl:value-of select="@id"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:when>
	<xsl:when test="$generateParagraphIDs='true'">
	  <xsl:call-template name="makeAnchor">
	    <xsl:with-param name="name">
	      <xsl:value-of select="generate-id()"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:when>
      </xsl:choose>
      <xsl:apply-templates/>
    </li>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item" mode="inline">
    <xsl:if test="preceding-sibling::item">, </xsl:if>
    <xsl:if
      test="not(following-sibling::item) and preceding-sibling::item">
      and </xsl:if>
    <xsl:text>&#x2022; </xsl:text>
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements item/label</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="item/label">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
	<xsl:apply-templates/>
	</span>
      </xsl:when>
      <xsl:otherwise>
        <strong>
          <xsl:apply-templates/>
        </strong>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements kw</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="kw">
    <span class="kw">
      <xsl:apply-templates/>
    </span>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements l</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="l" mode="Copying">
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements l[@copyOf]|lg[@copyOf]</xd:short>
    <xd:detail>
      <p> copyOf handling </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="l[@copyOf]|lg[@copyOf]">
    <xsl:variable name="W">
      <xsl:choose>
        <xsl:when test="starts-with(@copyof,'#')">
          <xsl:value-of select="substring-after(@copyof,'#')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@copyof"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:apply-templates mode="Copying" select="key('IDS',$W)"/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements label</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="label">
      <xsl:call-template name="makeAnchor"/>
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements label</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="label" mode="print">
    <xsl:call-template name="makeAnchor"/>
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
        <xsl:apply-templates/>
	</span>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements lb</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="lb">
    <br>
      <xsl:call-template name="rendering"/>
    </br>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements l</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="l">
    <div class="l">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements lg</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="lg">
    <div class="lg">
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements lg/l</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="lg/l">
    <div>
      <xsl:attribute name="class">
        <xsl:choose>
          <xsl:when test="@rend='Alignr'">
            <xsl:text>right</xsl:text>
          </xsl:when>
          <xsl:when test="@rend='Alignc'">
            <xsl:text>center</xsl:text>
          </xsl:when>
          <xsl:when test="starts-with(@rend,'indent(')">
            <xsl:text>indent</xsl:text>
            <xsl:value-of
              select="concat(substring-before(substring-after(@rend,'('),')'),'em')"
            />
          </xsl:when>
          <xsl:when test="@rend='indent'">
            <xsl:text>indent1</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>l</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements lg</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="lg" mode="Copying">
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements list</xd:short>
    <xd:detail>
      <p>Lists. Depending on the value of the 'type' attribute, various HTML
        lists are generated: <dl>
          <dt>bibl</dt>
          <dd>Items are processed in mode 'bibl'</dd>
          <dt>catalogue</dt>
          <dd>A gloss list is created, inside a paragraph</dd>
          <dt>gloss</dt>
          <dd>A gloss list is created, expecting alternate label and item
            elements</dd>
          <dt>glosstable</dt>
          <dd>Label and item pairs are laid out in a two-column table</dd>
          <dt>inline</dt>
          <dd>A comma-separate inline list</dd>
          <dt>runin</dt>
          <dd>An inline list with bullets between items</dd>
          <dt>unordered</dt>
          <dd>A simple unordered list</dd>
          <dt>ordered</dt>
          <dd>A simple ordered list</dd>
          <dt>vallist</dt>
          <dd>(Identical to glosstable)</dd>
        </dl>
      </p>
    </xd:detail>
  </xd:doc>
  <xsl:template match="list">
    <xsl:if test="head">
      <p class="listhead">
	<xsl:apply-templates select="head"/>
      </p>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="@type='catalogue'">
        <p>
          <dl>
            <xsl:call-template name="rendToClass"/>
            <xsl:for-each select="item">
              <p/>
              <xsl:apply-templates mode="gloss" select="."/>
            </xsl:for-each>
          </dl>
        </p>
      </xsl:when>
      <xsl:when test="@type='gloss' and @rend='multicol'">
        <xsl:variable name="nitems">
          <xsl:value-of select="count(item)div 2"/>
        </xsl:variable>
        <p>
          <table>
            <xsl:call-template name="rendToClass"/>
            <tr>
              <td valign="top">
                <dl>
                  <xsl:apply-templates mode="gloss"
                    select="item[position()&lt;=$nitems ]"/>
                </dl>
              </td>
              <td valign="top">
                <dl>
                  <xsl:apply-templates mode="gloss"
                    select="item[position() &gt;$nitems]"/>
                </dl>
              </td>
            </tr>
          </table>
        </p>
      </xsl:when>
      <xsl:when test="@type='gloss' or  label">
        <dl>
          <xsl:call-template name="rendToClass"/>
          <xsl:apply-templates mode="gloss" select="item"/>
        </dl>
      </xsl:when>
      <xsl:when test="@type='glosstable' or @type='vallist'">
        <table>
          <xsl:call-template name="rendToClass"/>
          <xsl:apply-templates mode="glosstable" select="item"/>
        </table>
      </xsl:when>
      <xsl:when test="@type='inline'">
	<!--<xsl:if test="not(item)">None</xsl:if>-->
        <xsl:apply-templates mode="inline" select="item"/>
      </xsl:when>
      <xsl:when test="@type='runin'">
        <p>
          <xsl:apply-templates mode="runin" select="item"/>
        </p>
      </xsl:when>
      <xsl:when test="@type='unordered' or @type='simple'">
        <ul>
          <xsl:call-template name="rendToClass"/>
          <xsl:apply-templates select="item"/>
        </ul>
      </xsl:when>
      <xsl:when test="@type='bibl'">
        <xsl:apply-templates mode="bibl" select="item"/>
      </xsl:when>
      <xsl:when test="starts-with(@type,'ordered')">
        <ol>
          <xsl:call-template name="rendToClass"/>
          <xsl:if test="starts-with(@type,'ordered:')">
            <xsl:attribute name="start">
              <xsl:value-of select="substring-after(@type,':')"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:apply-templates select="item"/>
        </ol>
      </xsl:when>
      <xsl:otherwise>
        <ul>
          <xsl:call-template name="rendToClass"/>
          <xsl:apply-templates select="item"/>
        </ul>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements list/label</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="list/label"/>

  <xd:doc>
    <xd:short>Process elements listBibl</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>

  <xsl:template match="listBibl">
    <xsl:choose>
      <xsl:when test="biblStruct">
	<dl class="listBibl">
	  <xsl:for-each select="biblStruct">
	    <xsl:sort select="translate(*/author/surname|*[1]/author/orgName|*[1]/author/name|*[1]/editor/surname|*[1]/editor/name|*[1]/title,$uc,$lc)"/>
	    <xsl:sort select="monogr/imprint/date"/>
	    <dt>
	      <xsl:call-template name="makeAnchor"/>
	      <xsl:apply-templates select="." mode="xref"/>
	    </dt>
	    <dd>
	      <xsl:apply-templates select="."/>
	    </dd>
	  </xsl:for-each>
	</dl>
      </xsl:when>
      <xsl:otherwise>
	<ol class="listBibl">
	  <xsl:for-each select="bibl|biblItem">
	    <li>
	      <xsl:call-template name="makeAnchor">
		<xsl:with-param name="name">
		  <xsl:apply-templates mode="ident" select="."/>
		</xsl:with-param>
	      </xsl:call-template>
	      <xsl:apply-templates select="."/>
	    </li>
	  </xsl:for-each>
	</ol>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements mentioned</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="mentioned">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
        <xsl:apply-templates/>
	</span>
      </xsl:when>
      <xsl:otherwise>
	<span class="mentioned">
	  <xsl:apply-templates/>
	</span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements name in mode "plain"</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="name" mode="plain">
    <xsl:variable name="ident">
      <xsl:apply-templates mode="ident" select="."/>
    </xsl:variable>
      <xsl:call-template name="makeAnchor">
	<xsl:with-param name="name">
	  <xsl:value-of select="$ident"/>
	</xsl:with-param>
      </xsl:call-template>
    <xsl:apply-templates/>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements name and persName</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="name|persName">
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="not(ancestor::person|ancestor::biblStruct)"/>
      <xsl:when test="following-sibling::name|following-sibling::persName">
	<xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:text>. </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements note</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="note">
    <xsl:variable name="identifier">
      <xsl:text>Note</xsl:text>
      <xsl:call-template name="noteID"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="ancestor::bibl"> (<xsl:apply-templates/>) </xsl:when>
      <xsl:when test="@place='inline'">
	<xsl:call-template name="makeAnchor">
	  <xsl:with-param name="name" select="$identifier"/>
	</xsl:call-template>
        <xsl:text> (</xsl:text>
        <xsl:apply-templates/>
        <xsl:text>)</xsl:text>
      </xsl:when>
      <xsl:when test="@place='display'">
	<xsl:call-template name="makeAnchor">
	  <xsl:with-param name="name" select="$identifier"/>
	</xsl:call-template>
        <blockquote>
	  <xsl:choose>
	    <xsl:when test="@rend">
	      <xsl:attribute name="class">		
		<xsl:value-of select="@rend"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:when test="@rendition">
	      <xsl:call-template name="applyRendition"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="class">
	      <xsl:text>note</xsl:text>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>
	  <p>
            <xsl:apply-templates/>
          </p>
        </blockquote>
      </xsl:when>
      <xsl:when test="@place='foot' or @place='end'">
	<xsl:call-template name="makeAnchor">
	  <xsl:with-param name="name" select="concat($identifier,'_return')"/>
	</xsl:call-template>
        <xsl:choose>
          <xsl:when test="$footnoteFile='true'">
            <a class="notelink" 
	       title="Go to note"
	       href="{$masterFile}-notes.html#{$identifier}">
              <sup>
                <xsl:call-template name="noteN"/>
              </sup>
            </a>
          </xsl:when>
          <xsl:otherwise>
            <a class="notelink" 
	       title="Go to note"
	       href="#{$identifier}">
              <sup>
                <xsl:call-template name="noteN"/>
              </sup>
            </a>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
	<xsl:call-template name="makeAnchor">
	  <xsl:with-param name="name" select="$identifier"/>
	</xsl:call-template>
        <xsl:text> [</xsl:text>
        <xsl:call-template name="i18n">
          <xsl:with-param name="word">Note</xsl:with-param>
        </xsl:call-template>
        <xsl:text>: </xsl:text>
        <xsl:apply-templates/>
        <xsl:text>]</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements note</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="note" mode="printnotes">
    <xsl:if test="not(ancestor::bibl)">
      <xsl:variable name="identifier">
	<xsl:text>Note</xsl:text>
        <xsl:call-template name="noteID"/>
      </xsl:variable>
      <xsl:variable name="parent">
        <xsl:call-template name="locateParentdiv"/>
      </xsl:variable>
      <xsl:if test="$verbose='true'">
        <xsl:message>Note <xsl:value-of select="$identifier"/>
	<xsl:text> with parent </xsl:text>
	<xsl:value-of select="$parent"/></xsl:message>
      </xsl:if>
      <div class="note">
	<xsl:call-template name="makeAnchor">
	  <xsl:with-param name="name" select="$identifier"/>
	</xsl:call-template>
	<span class="noteLabel">
	  <xsl:call-template name="noteN"/>
	  <xsl:text>. </xsl:text>
	</span>
	<div class="noteBody">
	  <xsl:apply-templates/>
	</div>
	<xsl:if test="$footnoteBackLink= 'true'">
	  <xsl:text> </xsl:text>
	  <a class="link_return"
	     title="Go back to text"
	     href="#{concat($identifier,'_return')}">&#8629;</a>
	</xsl:if>
      </div>
    </xsl:if>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements note[@type='action']</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="note[@type='action']">
    <div class="right"><b>Action <xsl:number count="note[@type='action']"
          level="any"/></b>: <i>
        <xsl:apply-templates/>
      </i></div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element pb</xd:short>
    <xd:detail>Indication of a page break. For the purposes of HTML, we simply
      make it an anchor if it has an ID.</xd:detail>
  </xd:doc>
  <xsl:template match="pb">
      <xsl:call-template name="makeAnchor"/>
      <p style="page-break-before: always"/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element p</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="p">
    <xsl:variable name="wrapperElement">
      <xsl:choose>
        <xsl:when
          test="specList|quote|moduleSpec|list|eg|teix:egXML|table|specGrp|specGrpRef|q[@rend='display']|figure">
          <xsl:text>div</xsl:text>
        </xsl:when>
        <xsl:when test="parent::p">
          <xsl:text>div</xsl:text>
        </xsl:when>
        <xsl:when test="parent::remarks">
          <xsl:text>div</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>p</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$wrapperElement}">
      <xsl:call-template name="rendToClass">
	<xsl:with-param name="default">
	  <xsl:if test="$wrapperElement='div'">p</xsl:if>
	</xsl:with-param>
      </xsl:call-template>

      <xsl:choose>
	<xsl:when test="@id">
	  <xsl:call-template name="makeAnchor">
	    <xsl:with-param name="name">
	      <xsl:value-of select="@id"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:when>
	<xsl:when test="$generateParagraphIDs='true'">
	  <xsl:call-template name="makeAnchor">
	    <xsl:with-param name="name">
	      <xsl:value-of select="generate-id()"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:when>
      </xsl:choose>
      <xsl:if test="$numberParagraphs='true'">
        <xsl:number/>
        <xsl:text> </xsl:text>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements p[@rend='box']</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="p[@rend='box']">
    <p class="box">
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements q and said</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="q|said">
    <xsl:choose>
      <xsl:when test="p">
        <blockquote>
	  <xsl:choose>
	    <xsl:when test="@rend">
	      <xsl:attribute name="class">
		<xsl:value-of select="@rend"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:when test="@rendition">
	      <xsl:call-template name="applyRendition"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="class">
		<xsl:text>q</xsl:text>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>
          <xsl:apply-templates/>
        </blockquote>
      </xsl:when>
      <xsl:when test="@rend='display'">
        <p class="blockquote">
          <xsl:apply-templates/>
        </p>
      </xsl:when>
      <xsl:when test="text">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:when test="lg">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:call-template name="makeQuote"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements q[@rend='display']</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="q[@rend='display']">
    <blockquote>
	  <xsl:choose>
	    <xsl:when test="@rend">
	      <xsl:attribute name="class">
		<xsl:value-of select="@rend"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:when test="@rendition">
	      <xsl:call-template name="applyRendition"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="class">
		<xsl:text>q</xsl:text>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>
      <xsl:choose>
        <xsl:when test="p">
          <xsl:apply-templates/>
        </xsl:when>
        <xsl:otherwise>
          <p>
            <xsl:apply-templates/>
          </p>
        </xsl:otherwise>
      </xsl:choose>
    </blockquote>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements q[@rend='eg']</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="q[@rend='eg']">
    <div>
      <xsl:if test="$cssFile">
        <xsl:attribute name="class">eg</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </div>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements quote</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="quote">
    <xsl:choose>
      <xsl:when test="parent::cit">
        <div class="citquote">
          <xsl:apply-templates/>
        </div>
      </xsl:when>
      <xsl:when test="contains(concat(' ', @rend, ' '), ' quoted ')">
        <xsl:value-of select="$preQuote"/>
        <xsl:apply-templates/>
        <xsl:value-of select="$postQuote"/>
        <xsl:if test="following-sibling::bibl">
          <span class="quotedbibl">
            <xsl:text>(</xsl:text>
            <xsl:apply-templates select="following-sibling::bibl"/>
            <xsl:text>)</xsl:text>
          </span>
        </xsl:if>
      </xsl:when>
      <xsl:when test="parent::p or parent::note">
	<div class="blockquote">
	  <xsl:apply-templates/>
	</div>
      </xsl:when>
      <xsl:otherwise>
        <blockquote>
	  <xsl:choose>
	    <xsl:when test="@rend">
	      <xsl:attribute name="class">
		<xsl:value-of select="@rend"/>
	      </xsl:attribute>
	    </xsl:when>
	    <xsl:when test="@rendition">
	      <xsl:call-template name="applyRendition"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:attribute name="class">
		<xsl:text>quote</xsl:text>
	      </xsl:attribute>
	    </xsl:otherwise>
	  </xsl:choose>

          <xsl:choose>
            <xsl:when test="p">
              <xsl:apply-templates/>
            </xsl:when>
            <xsl:when test="l">
              <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
              <p>
                <xsl:apply-templates/>
              </p>
            </xsl:otherwise>
          </xsl:choose>
        </blockquote>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements resp</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="resp">
    <xsl:apply-templates/>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements respStmt</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="respStmt">
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements salute</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="salute">
    <p class="left">
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements seg</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="seg">
    <span>
	<xsl:choose>
	  <xsl:when test="@type">
	    <xsl:attribute name="class">
	      <xsl:value-of select="@type"/>
	    </xsl:attribute>
	  </xsl:when>
	  <xsl:when test="@rend">
	    <xsl:attribute name="class">
	      <xsl:value-of select="@rend"/>
	    </xsl:attribute>
	  </xsl:when>
	</xsl:choose>
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements  sic</xd:short>
    <xd:detail>Does not do anything yet.</xd:detail>
  </xd:doc>
  <xsl:template match="sic">
    <xsl:apply-templates/>
    <xsl:text> (sic)</xsl:text>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements signed</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="signed">
    <p class="left">
      <xsl:apply-templates/>
    </p>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements space</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="space">
    <xsl:choose>
      <xsl:when test="@quantity|@extent">
        <xsl:call-template name="space_loop">
          <xsl:with-param name="extent" select="@quantity|@extent"/>
        </xsl:call-template>
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text> </xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements term</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="term">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
        <xsl:apply-templates/>
	</span>
      </xsl:when>      
      <xsl:otherwise>
        <span class="term">
          <xsl:apply-templates/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements title</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="title" mode="withbr">
    <xsl:value-of select="."/>
    <br/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements titleStmt/title</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="titleStmt/title">
    <xsl:if test="preceding-sibling::title">
      <br/>
    </xsl:if>
    <xsl:apply-templates/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process element title</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xd:doc>
    <xd:short>Process elements witList</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="witList">
    <xsl:apply-templates select="./witness"/>
  </xsl:template>
  <xd:doc>
    <xd:short>Process elements witness</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="witness">
    <p>
      <a name="{@sigil}"/>
      <b>Sigle: <xsl:value-of select="@sigil"/></b>
      <br/>
      <xsl:value-of select="text()"/>
      <br/>
      <xsl:apply-templates select="biblStruct"/>
      <xsl:if test="child::note"><br/>Zie noot: <xsl:apply-templates
          select="child::note"/></xsl:if>
    </p>
  </xsl:template>


  <xd:doc>
    <xd:short>[html] Activate a value for @rendition</xd:short>
    <xd:param name="value">value</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="applyRendition">
    <xsl:attribute name="class">
  
      <xsl:choose>
	<xsl:when test="@rendition=''"/>
	<xsl:when test="contains(normalize-space(@rendition),' ')">
	  <xsl:call-template name="splitRendition">
	    <xsl:with-param name="value">
	      <xsl:value-of select="normalize-space(@rendition)"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:call-template name="findRendition">
	    <xsl:with-param name="value">
	      <xsl:value-of select="@rendition"/>
	    </xsl:with-param>
	  </xsl:call-template>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:attribute>
   </xsl:template>

  <xd:doc>
    <xd:short>[html] Get another value from a space-separated list</xd:short>
    <xd:param name="value">value</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="splitRendition">
    <xsl:param name="value"/>
    <xsl:choose>
      <xsl:when test="$value=''"/>
      <xsl:when test="contains($value,' ')">
	<xsl:call-template name="findRendition">
	  <xsl:with-param name="value">
	    <xsl:value-of select="substring-before($value,' ')"/>
	  </xsl:with-param>
	</xsl:call-template>
	<xsl:call-template name="splitRendition">
	  <xsl:with-param name="value">
	    <xsl:value-of select="substring-after($value,' ')"/>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
	<xsl:call-template name="findRendition">
	  <xsl:with-param name="value">
	    <xsl:value-of select="$value"/>
	  </xsl:with-param>
	</xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="findRendition">
    <xsl:param name="value"/>
    <xsl:choose>
      <xsl:when test="starts-with($value,'#')">
	<xsl:value-of select="substring-after($value,'#')"/>
	<xsl:text> </xsl:text>
      </xsl:when>
      <xsl:otherwise>
	<xsl:for-each select="document($value)">
	  <xsl:apply-templates select="@id"/>
	  <xsl:text> </xsl:text>
	</xsl:for-each>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>[html] Active a value for @rend</xd:short>
    <xd:param name="value">value</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="applyRend">
    <xsl:param name="value"/>
    <xsl:choose>
      <xsl:when test="not($value='')">
        <xsl:variable name="thisparm"
          select="substring-before($value,$rendSeparator)"/>
        <xsl:call-template name="renderingInner">
          <xsl:with-param name="value" select="$thisparm"/>
          <xsl:with-param name="rest"
            select="substring-after($value,$rendSeparator)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] </xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="continuedToc">
    <xsl:if
      test="div|div0|div1|div2|div3|div4|div5|div6">
      <ul class="toc">
        <xsl:apply-templates mode="maketoc"
          select="div|div0|div1|div2|div3|div4|div5|div6"
        />
      </ul>
    </xsl:if>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] How to identify a note</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="noteID">
    <xsl:choose>
      <xsl:when test="@id">
        <xsl:value-of select="@id"/>
      </xsl:when>
      <xsl:when test="@n">
        <xsl:value-of select="@n"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:number count="note" level="any"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>[html] How to label a note</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="noteN">
    <xsl:choose>
      <xsl:when test="@n">
        <xsl:value-of select="@n"/>
      </xsl:when>
      <xsl:when test="not(@place) and $consecutiveFootnoteNumbers='true'">
            <xsl:number 
		count="note[not(@place)]" 
		level="any"/>
      </xsl:when>
      <xsl:when test="not(@place)">
        <xsl:choose>
          <xsl:when test="ancestor::front">
            <xsl:number count="note[not(@place)]" 
			from="front"
			level="any"/>
          </xsl:when>
          <xsl:when test="ancestor::back">
            <xsl:number count="note[not(@place)]" 
			from="back"
			level="any"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:number 
		count="note[not(@place)]" 
		from="body"
		level="any"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:when test="@place='end'">
        <xsl:choose>
          <xsl:when test="$consecutiveFootnoteNumbers = 'true'">
            <xsl:number 
		count="note[./@place='end']" 
		level="any"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="ancestor::front">
                <xsl:number count="note[./@place='end']" from="front"
                  level="any"/>
              </xsl:when>
              <xsl:when test="ancestor::back">
                <xsl:number count="note[./@place='end']" from="back"
                  level="any"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:number count="note[./@place='end']" from="body"
                  level="any"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$consecutiveFootnoteNumbers = 'true'">
            <xsl:number 
		count="note[./@place='foot']" 
		level="any"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:choose>
              <xsl:when test="ancestor::front">
                <xsl:number count="note[./@place='foot']" from="front"
                  level="any"/>
              </xsl:when>
              <xsl:when test="ancestor::back">
                <xsl:number count="note[./@place='foot']" from="back"
                  level="any"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:number count="note[./@place='foot']" from="body"
                  level="any"/>
              </xsl:otherwise>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] Show relevant footnotes</xd:short>
    <xd:param name="currentID">currentID</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="partialFootNotes">
    <xsl:param name="currentID"/>
    <xsl:choose>
      <xsl:when test="$currentID='current'"/>
      <xsl:when test="$currentID='' and $splitLevel=-1">
        <xsl:call-template name="printNotes"/>
      </xsl:when>
      <xsl:when test="$currentID=''">
        <xsl:for-each select=" descendant::text">
          <xsl:call-template name="printNotes"/>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="count(key('IDS',$currentID))&gt;0">
            <xsl:for-each select="key('IDS',$currentID)">
              <xsl:call-template name="printNotes"/>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates mode="xpath"
              select="ancestor-or-self::TEI.2/descendant::text">
              <xsl:with-param name="xpath" select="$currentID"/>
              <xsl:with-param name="action">notes</xsl:with-param>
            </xsl:apply-templates>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] </xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>

  <xsl:template name="printNotes">
      <xsl:choose>
	<xsl:when test="$footnoteFile='true' and ancestor-or-self::TEI.2/text/descendant::note[@place='foot' or @place='end']">
">
	  <xsl:variable name="BaseFile">
	    <xsl:value-of select="$masterFile"/>
            <xsl:call-template name="addCorpusID"/>
          </xsl:variable>
          <xsl:call-template name="outputChunk">
            <xsl:with-param name="ident">
              <xsl:value-of select="concat($BaseFile,'-notes')"/>
            </xsl:with-param>
            <xsl:with-param name="content">
              <xsl:call-template name="writeNotes"/>
            </xsl:with-param>
          </xsl:call-template>
        </xsl:when>
	<xsl:when test="self::div">
	  <xsl:variable name="depth">
	    <xsl:apply-templates mode="depth" select="."/>
	  </xsl:variable>
	  <xsl:choose>
	    <xsl:when test="$depth &lt; $splitLevel ">
	  <xsl:if test="child::*[not(self::div)]/descendant::note[@place='foot' or @place='end']">
	    <div class="notes">
	      <div class="noteHeading">
		<xsl:call-template name="i18n">
		  <xsl:with-param name="word">noteHeading</xsl:with-param>
		</xsl:call-template>
	      </div>
	      <xsl:apply-templates 
		  mode="printnotes"
		  select="child::*[not(self::div)]/descendant::note[@place='foot' or @place='end']"/>
	    </div>
	  </xsl:if>
	    </xsl:when>
	    <xsl:otherwise>
	  <xsl:if test="descendant::note[@place='foot' or @place='end']">
	    <div class="notes">
	      <div class="noteHeading">
		<xsl:call-template name="i18n">
		  <xsl:with-param name="word">noteHeading</xsl:with-param>
		</xsl:call-template>
	      </div>
	      <xsl:apply-templates 
		  mode="printnotes"
		  select="descendant::note[@place='foot' or @place='end']"/>
	    </div>
	  </xsl:if>
	    </xsl:otherwise>
	  </xsl:choose>
	</xsl:when>
	<xsl:when test="$splitLevel &gt;-1 and $requestedID='' and
			$STDOUT='true'">
	  <xsl:for-each
	      select="descendant-or-self::text/*[1]/*[1]">
	    <xsl:call-template name="printNotes"/>
	  </xsl:for-each>
	</xsl:when>

	<xsl:when test="$splitLevel &gt;-1 and self::text"/>
	<!--
	    <div class="notes text">
	    <div class="noteHeading">
	    <xsl:call-template name="i18n">
	    <xsl:with-param name="word">noteHeading</xsl:with-param>
	    </xsl:call-template>
	    </div>
	    <xsl:for-each
	    select="descendant::note[(@place='foot' or @place='end') and
	    not(ancestor::div)]">
	    <xsl:apply-templates mode="printnotes" select="."/>
	    </xsl:for-each>
	    </div>
	    </xsl:when>
	-->
	
        <xsl:otherwise>
	  <xsl:if test="descendant::note[@place='foot' or @place='end']">
	    <div class="notes">
	      <div class="noteHeading">
		<xsl:call-template name="i18n">
		  <xsl:with-param name="word">noteHeading</xsl:with-param>
		</xsl:call-template>
	      </div>
	      <xsl:apply-templates 
		  mode="printnotes"
		  select="descendant::note[@place='foot' or @place='end']"/>
	    </div>
	  </xsl:if>
	</xsl:otherwise>
      </xsl:choose>
    <xsl:if
	test="ancestor-or-self::TEI.2/text/descendant::app">

      <div class="notes">
	<div class="noteHeading">
	  <xsl:call-template name="i18n">
	    <xsl:with-param name="word">noteHeading</xsl:with-param>
	  </xsl:call-template>
	</div>
	<xsl:apply-templates mode="printnotes"
		 select="descendant::app"/>
      </div>
    </xsl:if>

  </xsl:template>
  <xd:doc>
    <xd:short>[html] </xd:short>
    <xd:detail>
      <p> rendering. support for multiple rendition elements added by Nick
        Nicholas </p>
    </xd:detail>
  </xd:doc>
  <xsl:template name="rendering">
    <xsl:call-template name="applyRend">
      <xsl:with-param name="value" select="concat(@rend,$rendSeparator)"/>
    </xsl:call-template>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] </xd:short>
    <xd:param name="value">the current segment of the value of the rend
      attribute</xd:param>
    <xd:param name="rest">the remainder of the attribute</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="renderingInner">
    <xsl:param name="value"/>
    <xsl:param name="rest"/>
    <xsl:choose>
      <xsl:when test="$value='bold'">
        <b>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </b>
      </xsl:when>
      <xsl:when test="$value='center'">
        <center>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </center>
      </xsl:when>
      <xsl:when test="$value='code'">
        <b>
          <tt>
            <xsl:call-template name="applyRend">
              <xsl:with-param name="value" select="$rest"/>
            </xsl:call-template>
          </tt>
        </b>
      </xsl:when>
      <xsl:when test="$value='ital'">
        <i>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </i>
      </xsl:when>
      <xsl:when test="$value='italic'">
        <i>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </i>
      </xsl:when>
      <xsl:when test="$value='it'">
        <i>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </i>
      </xsl:when>
      <xsl:when test="$value='italics'">
        <i>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </i>
      </xsl:when>
      <xsl:when test="$value='i'">
        <i>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </i>
      </xsl:when>
      <xsl:when test="$value='sc'">
        <!--   <small>
	   <xsl:value-of
	   select="translate(.,'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
	   </small>
      -->
        <span style="font-variant: small-caps">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='plain'">
        <xsl:call-template name="applyRend">
          <xsl:with-param name="value" select="$rest"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$value='quoted'">
        <xsl:text>‘</xsl:text>
        <xsl:call-template name="applyRend">
          <xsl:with-param name="value" select="$rest"/>
        </xsl:call-template>
        <xsl:text>’</xsl:text>
      </xsl:when>
      <xsl:when test="$value='sub'">
        <sub>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </sub>
      </xsl:when>
      <xsl:when test="$value='sup'">
        <sup>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </sup>
      </xsl:when>
      <xsl:when test="$value='important'">
        <span class="important">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <!-- NN added -->
      <xsl:when test="$value='ul'">
        <u>
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </u>
      </xsl:when>
      <!-- NN added -->
      <xsl:when test="$value='interlinMarks'">
        <xsl:text>`</xsl:text>
        <xsl:call-template name="applyRend">
          <xsl:with-param name="value" select="$rest"/>
        </xsl:call-template>
        <xsl:text>´</xsl:text>
      </xsl:when>
      <xsl:when test="$value='overbar'">
        <span style="text-decoration:overline">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='expanded'">
        <span style="letter-spacing: 0.15em">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='strike'">
        <span style="text-decoration: line-through">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='small'">
        <span style="font-size: 75%">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='large'">
        <span style="font-size: 150%">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='smaller'">
        <span style="font-size: 50%">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='larger'">
        <span style="font-size: 200%">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='calligraphic'">
        <span style="font-family: cursive">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='gothic'">
        <span style="font-family: fantasy">
          <xsl:call-template name="applyRend">
            <xsl:with-param name="value" select="$rest"/>
          </xsl:call-template>
        </span>
      </xsl:when>
      <xsl:when test="$value='noindex'">
        <xsl:call-template name="applyRend">
          <xsl:with-param name="value" select="$rest"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="local-name(.)='p'">
            <xsl:call-template name="unknownRendBlock">
              <xsl:with-param name="rest" select="$rest"/>
              <xsl:with-param name="value" select="$value"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="unknownRendInline">
              <xsl:with-param name="rest" select="$rest"/>
              <xsl:with-param name="value" select="$value"/>
            </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] </xd:short>
    <xd:param name="extent">extent</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="space_loop">
    <xsl:param name="extent"/>
    <xsl:choose>
      <xsl:when test="$extent &lt; 1"> </xsl:when>
      <xsl:otherwise>
        <xsl:text> </xsl:text>
        <xsl:variable name="newextent">
          <xsl:value-of select="$extent - 1"/>
        </xsl:variable>
        <xsl:call-template name="space_loop">
          <xsl:with-param name="extent" select="$newextent"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] Process unknown rend attribute by turning it into
    an HTML class</xd:short>
    <xd:param name="value">current value</xd:param>
    <xd:param name="rest">remaining values</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>

  <xsl:template name="unknownRendBlock">
    <xsl:param name="value"/>
    <xsl:param name="rest"/>
    <xsl:if test="not($value='')">
      <xsl:attribute name="class">
	<xsl:value-of select="$value"/>
      </xsl:attribute>
      <xsl:call-template name="applyRend">
	<xsl:with-param name="value" select="$rest"/>
    </xsl:call-template>
    </xsl:if>
  </xsl:template>
  
  <xd:doc>
    <xd:short>[html]  Process unknown rend attribute by turning it into
    an HTML class</xd:short>
    <xd:param name="value">value</xd:param>
    <xd:param name="rest">rest</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>

  <xsl:template name="unknownRendInline">
    <xsl:param name="value"/>
    <xsl:param name="rest"/>
    <xsl:if test="not($value='')">
      <span class="{$value}">
	<xsl:call-template name="applyRend">
	  <xsl:with-param name="value" select="$rest"/>
	</xsl:call-template>
      </span>
    </xsl:if>
  </xsl:template>
  <xd:doc>
    <xd:short>[html] create external notes file</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="writeNotes">
    <html>
      <xsl:call-template name="addLangAtt"/>
      <head>
        <title>
          <xsl:apply-templates
            select="descendant-or-self::text/front//docTitle//text()"/>
          <xsl:text>: </xsl:text>
          <xsl:call-template name="i18n">
            <xsl:with-param name="word">noteHeading</xsl:with-param>
          </xsl:call-template>
        </title>
        <xsl:call-template name="includeCSS"/>
        <xsl:call-template name="cssHook"/>
      </head>
      <body>
        <xsl:attribute name="onload">
          <xsl:text>startUp()</xsl:text>
        </xsl:attribute>
        <xsl:call-template name="bodyHook"/>
        <xsl:call-template name="bodyJavascriptHook"/>
	<div class="stdheader">
        <xsl:call-template name="stdheader">
          <xsl:with-param name="title">
            <xsl:apply-templates
              select="descendant-or-self::text/front//docTitle//text()"/>
            <xsl:text>: </xsl:text>
            <xsl:call-template name="i18n">
              <xsl:with-param name="word">noteHeading</xsl:with-param>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:call-template>
	</div>
        <div class="notes">
          <div class="noteHeading">
            <xsl:call-template name="i18n">
              <xsl:with-param name="word">noteHeading</xsl:with-param>
            </xsl:call-template>
          </div>
          <xsl:apply-templates mode="printnotes"
            select="descendant::note[@place]"/>
        </div>
        <xsl:call-template name="stdfooter"/>
        <xsl:call-template name="bodyEndHook"/>
      </body>
    </html>
  </xsl:template>

  <xd:doc>
    <xd:short>[html] convert rend attribute to HTML class</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="rendToClass">
    <xsl:param name="id">true</xsl:param>
    <xsl:param name="default"/>
    <xsl:choose>
      <xsl:when test="@rend and starts-with(@rend,'class:')">
        <xsl:attribute name="class">
          <xsl:value-of select="substring-after(@rend,'class:')"/>
        </xsl:attribute>
      </xsl:when>


      <xsl:when test="@rend">
        <xsl:attribute name="class">
          <xsl:value-of select="@rend"/>
        </xsl:attribute>
      </xsl:when>
      <xsl:when test="@rendition">
	<xsl:call-template name="applyRendition"/>
      </xsl:when>
      <xsl:when test="not($default='')">
        <xsl:attribute name="class">
	  <xsl:value-of select="$default"/>
        </xsl:attribute>
      </xsl:when>
    </xsl:choose>
    <xsl:if test="$id='true'">
      <xsl:choose>
	<xsl:when test="@id">
	  <xsl:copy-of select="@id"/>
	</xsl:when>
	<xsl:when test="@id">
	  <xsl:attribute name="id">
	    <xsl:value-of select="@id"/>
	  </xsl:attribute>
	</xsl:when>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <xd:doc>
    <xd:short>[html] Create a point to which we can link in the HTML</xd:short>
    <xd:param name="name">value for identifier</xd:param>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="makeAnchor">
    <xsl:param name="name"/>
    <xsl:choose>
      <xsl:when test="$name and $xhtml='true'">
	<span>
	  <xsl:attribute name="id">
	    <xsl:value-of select="$name"/>
	  </xsl:attribute>
	  <xsl:comment>anchor</xsl:comment>
	</span>	
      </xsl:when>
      <xsl:when test="$name">
	<a name="{$name}">
	  <xsl:comment>anchor</xsl:comment>
	</a>
      </xsl:when>
      <xsl:when test="@id and $xhtml='true'">
	<span>
	  <xsl:attribute name="id">
	    <xsl:value-of select="@id"/>
	  </xsl:attribute>
	  <xsl:comment>anchor</xsl:comment>
	</span>	
      </xsl:when>
      <xsl:when test="@id">
        <a name="{@id}">
          <xsl:comment>anchor</xsl:comment>
        </a>
      </xsl:when>
      <xsl:otherwise>
	<xsl:variable name="me">
	<xsl:value-of select="$masterFile"/>-<xsl:value-of
          select="local-name(.)"/>-<xsl:value-of
	  select="generate-id()"/>
	</xsl:variable>
	<xsl:choose>
	  <xsl:when test="$xhtml='true'">
	    <span>
	      <xsl:attribute name="id">
		<xsl:value-of select="$me"/>
	      </xsl:attribute>
	      <xsl:comment>anchor</xsl:comment>
	    </span>	
	  </xsl:when>
	  <xsl:otherwise>
	    <a name="{$me}">
	      <xsl:comment>anchor</xsl:comment>
	    </a>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:otherwise>

    </xsl:choose>
  </xsl:template>

  <xd:doc>
    <xd:short>Process elements soCalled</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template match="soCalled">
    <xsl:choose>
      <xsl:when test="@rend">
        <xsl:call-template name="rendering"/>
      </xsl:when>
      <xsl:when test="@rendition">
	<span>
	<xsl:call-template name="applyRendition"/>
	<xsl:apply-templates/>
	</span>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$preQuote"/>
	<xsl:apply-templates/>
	<xsl:value-of select="$postQuote"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xd:doc>
    <xd:short>Copy all attributes in HTML namespace</xd:short>
    <xd:detail> </xd:detail>
  </xd:doc>
  <xsl:template name="htmlAttributes">
    <xsl:for-each select="@*">
      <xsl:if test="namespace-uri(.)='http://www.w3.org/1999/xhtml'">
      <xsl:attribute name="{local-name(.)}">
	<xsl:value-of select="."/>
      </xsl:attribute>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>



</xsl:stylesheet>
