<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:output method="html"/>

  <xsl:variable name="path">
    <xsl:value-of select="document('config.xml')//config/path"/>
  </xsl:variable>

  <xsl:variable name="path_css">
    <xsl:value-of select="document('config.xml')//config/css"/>
  </xsl:variable>

  <xsl:variable name="filename">
    <xsl:value-of select="/TEI.2/teiHeader/fileDesc/titleStmt/title"/>
  </xsl:variable>

  <xsl:strip-space elements="bibl"/>
  <xsl:strip-space elements="title"/>
  <xsl:strip-space elements="author"/>
  <xsl:strip-space elements="corr"/>

  <xsl:template match="/">

    <html lang="it">
      <head>
        <title>Centro Studi GIACOMO PUCCINI: <xsl:value-of select="$filename"/></title>
        <link rel="stylesheet" type="text/css" media="screen" href="{$path_css}struttura.css"/>
        <link rel="stylesheet" type="text/css" media="print" href="{$path_css}stampa.css"/>
      </head>
      <body class="corpo_lettera">

        <xsl:apply-templates/>

        <!-- Visualizzazione note a piè di pagina  -->
        <xsl:for-each select="//note[@place='foot']">
          <p class="note">
            <xsl:variable name="numero">
              <xsl:number level="any" count="note[@place='foot']" from="teiHeader"/>
            </xsl:variable>
            <a name="{concat('Note',$numero)}"/>
            <b>
              <xsl:value-of select="$numero"/>.</b>
            <xsl:text> </xsl:text>
            <xsl:apply-templates/>
          </p>
        </xsl:for-each>
        <!--Fine visualizzazione note a piè di pagina-->

        <!--Visualizzazione della data ultima modifica, responsabile della pubblicazione e crediti -->
        <p align="right">
          <i><xsl:apply-templates select="//fileDesc/publicationStmt/publisher"/>
            <xsl:text/>&#169; <xsl:apply-templates
              select="//revisionDesc/descendant::date[last()]"/></i>
          <br/>

          <xsl:call-template name="crediti"/>
          <xsl:call-template name="link_crediti"/>
          <xsl:call-template name="descrizione"/>
          <xsl:call-template name="img_cartaIntestata"/>
          <xsl:call-template name="desc_storia"/>
        </p>
      </body>
    </html>
  </xsl:template>



  <xsl:template match="teiHeader">
    <!-- Visualizzazione titolo-->
    <p style="font-size:20px; margin-bottom:0px; ">
      <xsl:apply-templates select="fileDesc/titleStmt/title"/>
    </p>
    <br/>

    <!-- Visualizzazione destinatario, luogo di destinazione-->
    <table style="width:100%; font-size: 14px;">
      <tr>
        <td>
          <p align="left">
            <b>
              <xsl:choose>
                <xsl:when test="//letHeading/addressee[not(@attested='unk')]">
                  <xsl:choose>
                    <xsl:when test="//letHeading/addressee[not(@attested='unk')]//persName/xref">
                      <xsl:apply-templates
                        select="//letHeading/addressee//persName[(position()=1)]/xref"/>
                      <xsl:if
                        test="//letHeading/addressee[not(@attested='unk')]//persName[(position()!=1)]/xref"
                        > e <xsl:apply-templates
                          select="//letHeading/addressee//persName[(position()!=1)]/xref"/>
                      </xsl:if>
                    </xsl:when>

                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(//letHeading/addressee//persName)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                  <xsl:if test="//letHeading/addressee//placeName/preceding-sibling::persName">
                    <xsl:choose>
                      <xsl:when test="//letHeading/addressee/placeName/xref">, <xsl:apply-templates
                          select="//letHeading/addressee//placeName/xref"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="normalize-space(//letHeading/addressee/placeName)"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:if>
                  <xsl:if test="//letHeading/addressee//placeName[not(preceding-sibling::persName)]">
                    <xsl:choose>
                      <xsl:when test="//letHeading/addressee/placeName/xref">
                        <xsl:apply-templates select="//letHeading/addressee//placeName/xref"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="normalize-space(//letHeading/addressee/placeName)"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:if>
                </xsl:when>
                <xsl:when test="//letHeading/addressee[@attested='unk']">
                  <p align="left"/>
                </xsl:when>
              </xsl:choose>
            </b>
          </p>
        </td>
        <td>
          <!-- Visualizzazione luogo e data di spedizione -->
          <xsl:choose>
            <xsl:when
              test="//letHeading/placeLet[not(@attested='unk')] and //letHeading/dateLet[not(@attested='unk')]">
              <p align="right">
                <b>
                  <xsl:choose>
                    <xsl:when test="//letHeading/placeLet[not(@attested='unk')]/xref">
                      <xsl:apply-templates select="//letDesc/letHeading/placeLet/xref"/>, </xsl:when>
                    <xsl:otherwise><xsl:value-of
                        select="normalize-space(//letDesc/letHeading/placeLet)"/>,</xsl:otherwise>
                  </xsl:choose>
                  <xsl:text> </xsl:text>
                  <xsl:apply-templates select="//letHeading/dateLet/date[not(@n)]"/>
                  <xsl:apply-templates select="//letHeading/dateLet/dateRange"/>
                </b>
              </p>
            </xsl:when>
          </xsl:choose>
          <xsl:choose>
            <xsl:when
              test="//letHeading/placeLet[@attested='unk'] and //letHeading/dateLet[not(@attested='unk')]">
              <p align="right">
                <b>
                  <xsl:apply-templates select="//letHeading/dateLet/date[not(@n)]"/>
                </b>
              </p>
            </xsl:when>
          </xsl:choose>
          <xsl:choose>
            <xsl:when
              test="//letHeading/placeLet[not(@attested='unk')] and //letHeading/dateLet[@attested='unk']">
              <p align="right">
                <b>
                  <xsl:apply-templates select="//letHeading/placeLet"/>
                </b>
              </p>
            </xsl:when>
          </xsl:choose>
          <xsl:choose>
            <xsl:when
              test="//letHeading/placeLet[@attested='unk'] and //letHeading/dateLet[@attested='unk']">
              <p align="right"/>
            </xsl:when>
          </xsl:choose>
        </td>
      </tr>
    </table>
    <br/>

    <!-- Visualizzazione della descrizione fisica: tipo di documento, tipo di supporto paginazione e foliazione -->
    <xsl:choose>
      <xsl:when
        test="//letDesc/physDesc/type[(.='Telegramma')] or //letDesc/physDesc/type[(.='Telegramma (minuta)')]">
        <span style="font-variant: small-caps;font-size:13px;">
          <xsl:apply-templates select="//letDesc/physDesc/type"/>
        </span>
      </xsl:when>
      <xsl:otherwise>
        <span style="font-variant: small-caps;font-size:13px;">
          <xsl:choose>
            <xsl:when test="//letDesc/physDesc/type[(.='Lettera')] and //letDesc[@status='frag']">
              <xsl:apply-templates select="//letDesc/physDesc/type"/> (frammento) </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="//letDesc/physDesc/type"/>
            </xsl:otherwise>
          </xsl:choose>
          <xsl:if test="//letDesc/physDesc/writing">
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="//letDesc/physDesc/writing"/>
          </xsl:if>
          <xsl:if test="//support//seg[@type='tipo di supporto']">
            <xsl:text> </xsl:text>
            <xsl:value-of select="//support//seg[@type='tipo di supporto']/text()"/>
          </xsl:if>
          <xsl:if test="//support//seg[@type='tipo di supporto']//xref">
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="//support//seg[@type='tipo di supporto']//xref"/>
          </xsl:if>
          <xsl:if test="//paraphItem[@id='para1']/paraphDesc">
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="//paraphItem[@id='para1']/paraphDesc"/>
          </xsl:if>
          <xsl:if test="//paraphItem[@id='para1']//paraphText">
            <xsl:text> </xsl:text>
            <xsl:apply-templates select="//paraphItem[@id='para1']//paraphText"/>
          </xsl:if>
          <xsl:call-template name="link_cartaIntestata"/>
          <xsl:call-template name="descarta_intestata"/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
    <br/>

    <!-- Richiamo delle informazioni contenute nella sezione della busta ovvero del template chiamato 'busta'-->
    <xsl:call-template name="busta"/>
  </xsl:template>

  <xsl:template name="desc_"/>

  <!-- Visualizzazione delle informazioni relative alla collocazione (link all'indece delle sigle, nome collezione e numero) e all'eventuale provenienza della busta-->
  <xsl:template name="intestazione2">
    <xsl:choose>
      <xsl:when test="//repository[@key]">
        <br/>
        <span style="font-variant: small-caps;font-size:13px;">Luogo di conservazione: </span>
        <a>
          <xsl:attribute name="href">indici/indice-sigle.html#<xsl:apply-templates
              select="//letIdentifier/repository/@key"/></xsl:attribute>
          <xsl:apply-templates select="//letIdentifier/repository/@key"/>
        </a>
        <xsl:if test="//collection[not(@type)]">,<xsl:text> </xsl:text><xsl:apply-templates
            select="//collection"/></xsl:if>
        <xsl:if test="//idno[not(@type)]">,<xsl:text> </xsl:text><xsl:apply-templates
            select="//idno"/></xsl:if>
      </xsl:when>
      <xsl:when test="//repository[(.='Collezione privata')]">
        <br/>
        <span style="font-variant: small-caps;font-size:13px;">Luogo di conservazione: </span>
       <xsl:apply-templates
          select="//collection"/>
        <xsl:if test="//idno[not(@type)]">,<xsl:text> </xsl:text><xsl:apply-templates
          select="//idno"/></xsl:if>
      </xsl:when>
      <xsl:when test="//repository[@type]">
        <span style="font-variant: small-caps;font-size:13px;">Luogo di conservazione:
        </span>sconosciuto</xsl:when>
      <xsl:otherwise/>
    </xsl:choose>

    <!-- Visualizzazione della storia del documento: timbro di protocollo, aggiunte manoscritte -->
    <xsl:if
      test="//history/note or  //history/provenance or //seg[@type='reference number'] or //envelope//note">
      <!--    <xsl:call-template name="link_storia"/>-->
      <xsl:call-template name="link_storia"/>
    </xsl:if>

    <xsl:if
      test="//layout//seg[not(@type='tipo di supporto')] or //physDesc//musicNotation or //physDesc//decoration or //physDesc/condition or //physDesc//paraphernalia//paraphItem[not(@id='carta_intestata')][not(@id='busta_intestata')][not(@id='para1')]">
      <!--<xsl:call-template name="link_desc"/>-->
      <xsl:call-template name="link_desc"/>
    </xsl:if>
    
    <xsl:choose>
    <!--Visualizzazione delle informazioni sulla eventuale pubblicazione. Se il documento è inedito sarà stampata la dicitura "inedito/a a seconda del genere di documento-->
      
      <xsl:when test="//additional/listBibl">
        <br/>
        <span style="font-variant: small-caps;font-size:13px;">Pubblicazione: </span>
        <xsl:if test="//additional//bibl[not(position()=last())]">
          <xsl:for-each select="//additional//bibl[not(position()=last())]"><xsl:apply-templates
            select="."/>; </xsl:for-each>
        </xsl:if>
        <xsl:if test="//additional//bibl[last()]">
          <xsl:apply-templates select="//additional//bibl[last()]"/>
        </xsl:if>
      </xsl:when>
      
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when
            test="//letDesc/physDesc/type[(.='Telegramma')] or //letDesc/physDesc/type[(.='Biglietto da visita')] or  //letDesc/physDesc/type[(.='Biglietto')] or 
            //letDesc/physDesc/type[(.='Biglietto con busta')]">
            <br/>
            <span style="font-variant: small-caps;font-size:13px;"> Inedito</span>
          </xsl:when>
          <xsl:otherwise>
            <br/>
            <span style="font-variant: small-caps;font-size:13px;">Inedita</span>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>

    <!--Visualizzazione sul tipo di documento da cui è avvenuta la trascrizione: originale, fac-simile...-->
    <xsl:choose>
      <xsl:when test="//letIdentifier/note">
        <br/>
        <span style="font-variant: small-caps;font-size:13px;">Trascrizione: </span>
        <xsl:apply-templates select="//letIdentifier/note"/>
        <br/>
      </xsl:when>
      <xsl:otherwise> </xsl:otherwise>
    </xsl:choose>

    <!--Visualizzazione di eventuali note relative alla trascrizione della lettera-->
    <xsl:choose>
      <xsl:when test="//letDesc/letHeading/note">
        <span style="font-variant: small-caps;font-size:13px;">Note editoriali: </span>
        <xsl:apply-templates select="//letDesc/letHeading/note"/>
        <br/>
      </xsl:when>
      <xsl:otherwise> </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <xsl:template name="storia"/>

  <xsl:template name="busta">

    <!--Visualizzazione dell'indirizzo del mittente-->
    <xsl:choose>
      <xsl:when test="//envelope/envPart//address[@type='sender']">
        <span style="font-variant: small-caps;font-size:13px;">Indirizzo del mittente: </span>
        <br/>
        <xsl:apply-templates select="//envelope/envPart/div/p"/>
        <xsl:apply-templates select="//envelope/envPart//address[@type='sender']"/>
      </xsl:when>
    </xsl:choose>

    <!--Visualizzazione dell'indirizzo del destinatario-->
    <xsl:choose>
      <xsl:when test="//envelope/envPart//address[@type='receiver']">
        <span style="font-variant: small-caps;font-size:13px;">Indirizzo: </span>
        <br/>
        <xsl:apply-templates select="//envelope/envPart//address[@type='receiver']"/>
      </xsl:when>
    </xsl:choose>

    <xsl:call-template name="telegramma"/>

    <!--Visualizzazione dei timbri postali:partenza, arrivo, transito, altri-->
    <xsl:if test="//envelope/envPart/postmark[@n='d']">
      <br/>
      <span style="font-variant: small-caps;font-size:13px;">Timbro postale di partenza: </span>
      <xsl:apply-templates select="//envelope[not(@type)]/envPart/postmark[@n='d']/placeName"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="//envelope[not(@type)]/envPart/postmark[@n='d']/date"/>

    </xsl:if>

    <xsl:if test="//envelope/envPart/postmark[@n='a']">
      <br/>
      <span style="font-variant: small-caps;font-size:13px;">Timbro postale di arrivo: </span>
      <xsl:apply-templates select="//envelope/envPart/postmark[@n='a']/placeName"/>
      <xsl:text> </xsl:text>
      <xsl:apply-templates select="//envelope/envPart/postmark[@n='a']/date"/>

    </xsl:if>

    <xsl:if test="//envelope/envPart/postmark[@n='t']">
      <xsl:for-each select="//envelope/envPart/postmark[@n='t']">
        <span style="font-variant: small-caps;font-size:13px;">Timbro postale di transito: </span>
        <xsl:apply-templates select="placeName"/>
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="date"/>
        <br/>
      </xsl:for-each>
    </xsl:if>

    <xsl:if test="//envelope/envPart/postmark[not(@n)]">
      <xsl:for-each select="//envelope/envPart/postmark[not (@n)]">
        <span style="font-variant: small-caps;font-size:13px;">Altro timbro postale: </span>
        <xsl:apply-templates select="placeName"/>
        <xsl:text> </xsl:text>
        <xsl:apply-templates select="date"/>
        <br/>
      </xsl:for-each>
    </xsl:if>

    <xsl:call-template name="intestazione2"/>

  </xsl:template>

  <xsl:template name="telegramma">

    <xsl:if test="//type[(.='Telegramma')]">
      <!--Eliminazione link dall'intestazione del telegramma-->
      <xsl:if test="//letDesc/letHeading/addressee/placeName">
        <span style="font-variant: small-caps;font-size:13px;">partito da: </span>
        <xsl:apply-templates select="//letDesc/letHeading/placeLet//text()"/>
      </xsl:if>
      <xsl:if test="//letHeading/dateLet/date[@n='1']">, <xsl:apply-templates
          select="//letHeading/dateLet/date[@n='1']/text()"/></xsl:if>
      <xsl:if test="//letHeading/dateLet/date[@n='1']/time">, ore <xsl:apply-templates
          select="//letHeading/dateLet/date[@n='1']/time"/></xsl:if>
      <xsl:if test="//letHeading/placeLet"/>; <span style="font-variant: small-caps;font-size:13px;"
        >arrivato a: </span><xsl:apply-templates select="//letHeading/addressee/placeName//text()"/>
      <xsl:if test="//letHeading/dateLet/date[@n='3']">, <xsl:apply-templates
          select="//letHeading/dateLet/date[@n='3']/text()"/></xsl:if>
      <xsl:if test="//letHeading/dateLet/date[@n='3']/time">, ore <xsl:apply-templates
          select="//letHeading/dateLet/date[@n='3']/time"/></xsl:if>
      <br/></xsl:if>
  </xsl:template>

  <xsl:template match="revisionDesc"/>
  <xsl:template match="profileDesc"/>
  <xsl:template match="envelope"/>

  <!--Definizione dello spazio iniziale e finale del testo-->
  <xsl:template match="//text">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <!--Suddivisione in righe dell'indirizzo-->
  <xsl:template match="//addrLine">
    <span class="indirizzo">
      <xsl:apply-templates/>
    </span>
    <br/>
  </xsl:template>

  <!--visualizzazione decorazioni-->
  <xsl:template match="//paraphText">
    <i>
      <xsl:apply-templates/>
    </i>
  </xsl:template>
  <xsl:template match="//paraphDesc/p">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="//paraphText/p">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="//decoText/p">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="//decoDesc/p">
    <xsl:apply-templates/>
  </xsl:template>

  <!--Visualizzazione delle note nel testo-->
  <xsl:template match="//note[@place='in-text']">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Visualizzazione citazioni fra virgolette angolate-->
  <xsl:template match="//quote">&#171;<xsl:apply-templates/>&#187;</xsl:template>

  <!--Visualizzazione delle parti stampate in corsivo-->
  <xsl:template match="//print">
    <span style="font: normal normal smaller Courier New">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <!--Visualizzazione delle abbraviazioni sciolte-->
  <xsl:template match="//expan">
    <font color="black">[<xsl:apply-templates/>]</font>
  </xsl:template>

  <!--Visualizzazione delle parole sbagliate che non hanno bisogno della forma corretta-->

  <xsl:template match="sic[@corr]">
    <xsl:apply-templates/>
    <font color="black"> [sic]</font>
  </xsl:template>

  <xsl:template match="sic">
    <xsl:apply-templates/>
    <font color="black"> [sic]</font>
  </xsl:template>

  <!--<xsl:template match="sic">      <xsl:apply-templates select="child::*"/>[sic]</xsl:template>-->

  <!--Visualizzazione delle parole sbagliate di cui non sappiamo con sicurezza quale sia la forma corretta-->
  <!--  <xsl:template match="sic[not(@corr)]"> [<xsl:apply-templates/>]
  </xsl:template>-->

  <!--Visualizzazione delle parole sbagliate che hanno bisogno della forma corretta-->
  <xsl:template match="corr[@sic]">
    <xsl:apply-templates select="@sic"/>
    <font color="black"> [recte: </font>
    <xsl:apply-templates/>
    <font color="black">]</font>
  </xsl:template>

  <!--Visualizzazione p barrato-->
  <xsl:template match="abbr[@rend]">
    <xsl:if test="@rend='p_cross'">
      <i style="font-family:'palatino linotype'">p&#x0331;</i>
    </xsl:if>
  </xsl:template>

  <!-- Interruzione di riga -->
  <xsl:template match="lb">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>

  <xsl:template match="history/note/lb | //seg[@type='reference number']//lb">
    <xsl:apply-templates/> /<xsl:text> </xsl:text>
  </xsl:template>

  <!--definizione di paragrafo-->
  <xsl:template match="p">
    <p
      style="font-size: 12px; margin-left: 5px; margin-right: 0px;margin-bottom: 8px; margin-top: 0px; line-height: 18px; font-family: verdana,helvetica,sans-serif;">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <!-- Visualizzazione dei nomi delle opere i corsivo all'interno delle note -->
  <xsl:template match="note[@place='in-text']//title | note[@place='foot']//title">
    <xsl:choose>
      <xsl:when test="@type='journal'"> «<xsl:apply-templates/>»</xsl:when>
      <xsl:when test="@type='newspaper'"> «<xsl:apply-templates/>»</xsl:when>
      <xsl:otherwise>
        <i>
          <xsl:apply-templates/>
        </i>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Visualizzazione dei gap-->
  <xsl:template match="gap">[...]<xsl:apply-templates/></xsl:template>

  <xsl:template match="quote//title[@type='opera']">
    <xsl:apply-templates/>
  </xsl:template>


  <xsl:template match="seg//title[@type='opera']">
    <span style="font-variant: normal">
      <i>
        <xsl:apply-templates/>
      </i>
    </span>
  </xsl:template>

  <!-- Visualizzazione degli interventi editoriali -->


  <!--Visualizzazione delle diverse tipologie di aggiunte. Nell'ordine: apice, apice e sottolineato, sottolineato, corsivo, doppia sottolineatura, linea, linea breve [@rend='superscript']-->
  <xsl:template match="hi[@rend='superscript']">
    <sup>
      <xsl:apply-templates/>
    </sup>
  </xsl:template>

  <xsl:template match="hi[@rend='underl_sup']">
    <u>
      <sup>
        <xsl:apply-templates/>
      </sup>
    </u>
  </xsl:template>

  <xsl:template match="//hi[@rend='underline']">
    <u>
      <xsl:apply-templates/>
    </u>
  </xsl:template>

  <xsl:template match="//hi[@rend='italic']">
    <i>
      <xsl:apply-templates/>
    </i>
  </xsl:template>

  <xsl:template match="//hi[@rend='bold']">
    <b>
      <xsl:apply-templates/>
    </b>
  </xsl:template>

  <xsl:template match="hi[@rend='double_u']">
    <span style="border-style: none none double none;">
      <xsl:apply-templates/>
    </span>
  </xsl:template>


  <xsl:template match="hi[@rend='tashed']">
    <span style="border-bottom: dashed thin;">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="hi[@rend='line']">
    <hr width="50%" noshade="noshade" align="left">
      <xsl:apply-templates/>
    </hr>
  </xsl:template>

  <xsl:template match="hi[@rend='shortline']">
    <hr width="15%" noshade="noshade">
      <xsl:apply-templates/>
    </hr>
  </xsl:template>

  <!--Visualizzazione cancellatura-->
  <xsl:template match="del">
    <strike>
      <xsl:apply-templates/>
    </strike>
  </xsl:template>

  <!--Visualizzazione diverao allineamento dei paragrafi: destro e centrato-->
  <xsl:template match="p[@rend='right']">
    <p align="right">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="p[@rend='center']">
    <p align="center">
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="cell[@rend='right']">
    <span align="right">
      <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="add[@place='margin-left']"> /<xsl:apply-templates/>/ [margine sinistro]</xsl:template>

  <xsl:template match="add[@place='margin-right']"> /<xsl:apply-templates/>/ [margine destro] </xsl:template>

  <xsl:template match="add[@place='margin-top']"> /<xsl:apply-templates/>/ [margine superiore] </xsl:template>

  <xsl:template match="add[@place='margin-bottom']"> /<xsl:apply-templates/>/ [margine inferiore] </xsl:template>

  <xsl:template match="add[@place='supralinear']">
    <font color="black">\</font>
    <xsl:apply-templates/>
    <font color="black">/</font>
  </xsl:template>

  <xsl:template match="add[@place='underlinear']"> /<xsl:apply-templates/>\ </xsl:template>

  <xsl:template match="add[@place='back']"> /<xsl:apply-templates/>/ [sul verso] </xsl:template>

  <xsl:template match="add[@place='overturned']"> /<xsl:apply-templates/>/ [capovolto] </xsl:template>

  <xsl:template match="add[not(@place)]"> / <xsl:for-each
      select="node()[local-name() != supplied] | text()">
      <xsl:apply-templates select="."/>
    </xsl:for-each> / <xsl:apply-templates select="supplied"/></xsl:template>

  <xsl:template match="//supplied">[<xsl:apply-templates/>]</xsl:template>

  <xsl:template match="add[not(@place)]"> / <xsl:for-each
      select="node()[local-name() != 'supplied'] | text()">
      <xsl:apply-templates select="."/>
    </xsl:for-each> / <xsl:apply-templates select="supplied"/></xsl:template>

  <!--Creazione di un link per l'apertura di immagini GIF. Si suppone che le immagini siano raccolte dentro una cartella chiamata img. Il link si apre in una nuova finestra-->

  <xsl:template match="//xref">
    <xsl:choose>
      <xsl:when test="@exclude"><xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>

        <xsl:choose>
          <xsl:when test="@targType='img'">
            <a target="_blank">
              <xsl:attribute name="href">img/<xsl:apply-templates select="@to"/>.gif
              </xsl:attribute>
            </a>
          </xsl:when>
          <!--  </xsl:template>-->

          <!--Creazione di un link che rimanda a altre lettere-->
          <!--<xsl:template match="//xref[@targType='letter']">-->
          <xsl:when test="@targType='letter'">
            <a>
              <xsl:attribute name="href">
                <xsl:apply-templates select="@to"/>.html</xsl:attribute>
              <xsl:apply-templates/>
            </a>
          </xsl:when>
          <!--  </xsl:template>-->



          <xsl:otherwise>
            <xsl:choose>
              <xsl:when
                test="child::persName[@type!='character'] | ancestor::persName[@type!='character'] | ancestor::name[@key='publisher'] | child::name[@key='publisher'] | child::persName[not(@type)] | ancestor::persName[not(@type)]">
                <a>
                  <xsl:attribute name="href">indici/indice-nomi.html#<xsl:apply-templates
                      select="@to"/>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="child::* | child::text()">
                      <xsl:apply-templates select="child::* | child::text()"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                  </xsl:choose>

                </a>
              </xsl:when>

              <xsl:when
                test="child::persName[@type='character'] | ancestor::persName[@type='character']">
                <a>
                  <xsl:attribute name="href">indici/indice-opere.html#<xsl:apply-templates
                      select="@to"/>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="child::* | child::text()">
                      <xsl:apply-templates select="child::* | child::text()"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                  </xsl:choose>

                </a>
              </xsl:when>
              <xsl:when
                test="child::placeName | ancestor::placeName | ancestor::placeLet |  child::name[@key='theatre'] | ancestor::name[@key='theatre']">
                <a>
                  <xsl:attribute name="href">indici/indice-luoghi.html#<xsl:apply-templates
                      select="@to"/>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="child::* | child::text()">
                      <xsl:apply-templates select="child::* | child::text()"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </a>
              </xsl:when>
              <xsl:when test="child::title[@type] | child::title | ancestor::title[@type] | ancestor::title">
                <a>
                  <xsl:attribute name="href">indici/indice-opere.html#<xsl:apply-templates
                      select="@to"/>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="child::* | child::text()">
                      <xsl:apply-templates select="child::* | child::text()"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </a>
              </xsl:when>
              <xsl:when test="ancestor::bibl | ancestor::author">
                <a>
                  <xsl:attribute name="href">indici/indice-bibliografia.html#<xsl:apply-templates
                      select="@to"/>
                  </xsl:attribute>
                  <xsl:choose>
                    <xsl:when test="child::* | child::text()">
                      <xsl:apply-templates select="child::* | child::text()"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="normalize-space(.)"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </a>
              </xsl:when>
            </xsl:choose>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>



  <xsl:template match="//seg">
    <xsl:choose>
      <xsl:when test="xref[@targType='hand']">
        <font color="blue">
          <xsl:apply-templates/>
        </font>
      </xsl:when>
    </xsl:choose>
  </xsl:template>


  <!--  <xsl:template match="seg/xref[@targType='hand']">
 
        <font color="blue"><xsl:apply-templates select="child::text()" />   </font>

  </xsl:template>-->


  <xsl:template match="//biblScope">
    <xsl:choose>
      <xsl:when test="@type='letter'">
        <xsl:text> </xsl:text><xsl:apply-templates />
      </xsl:when>
      <xsl:otherwise><xsl:text>, </xsl:text><xsl:apply-templates/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!--Tabelle-->
  <xsl:template match="//table">
    <xsl:choose>
      <xsl:when test="//table[@rend='center']">
        <p align="center">
          <table style="font: normal normal normal 12px Verdana,helvetica,sans-serif;">
            <xsl:apply-templates/>
          </table>
        </p>
      </xsl:when>
      <xsl:when test="//table[@rend='right']">
        <p align="right">
          <table style="font: normal normal normal 12px Verdana,helvetica,sans-serif;">
            <xsl:apply-templates/>
          </table>
        </p>
      </xsl:when>
      <xsl:when test="//table[@rend='center']">
        <p align="center">
          <table style="font: normal normal normal 12px Verdana,helvetica,sans-serif;">
            <xsl:apply-templates/>
          </table>
        </p>
      </xsl:when>
      <xsl:otherwise>
        <table style="font: normal normal normal 12px Verdana,helvetica,sans-serif;">
          <xsl:apply-templates/>
        </table>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="//row">
    <tr>
      <xsl:apply-templates/>
    </tr>
  </xsl:template>

  <xsl:template match="//cell[@rend='right']">
    <td style="text-align:right">
      <xsl:apply-templates/>
    </td>
  </xsl:template>
  <xsl:template match="//cell[@rend='center']">
    <td style="text-align:center">
      <xsl:apply-templates/>
    </td>

  </xsl:template>

  <xsl:template match="//cell[not(@rend)]">
    <td>
      <xsl:apply-templates/>
    </td>

  </xsl:template>

  <!-- Visualizzazione strofe e versi-->
  <xsl:template match="//l">
    <xsl:apply-templates/>
    <br/>
  </xsl:template>

  <xsl:template match="//lg">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="//ps">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <!--Creazione del rimando alle note a piè di pagina-->
  <xsl:template match="note">
    <xsl:variable name="identifier">
      <xsl:choose>
        <xsl:when test="@id">
          <xsl:value-of select="@id"/>
        </xsl:when>

        <xsl:when test="ancestor::back">
          <xsl:number level="any" count="note[@place='foot']" from="back"/>
        </xsl:when>
        <xsl:when test="ancestor::front">
          <xsl:number level="any" count="note[@place='foot']" from="front"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:number level="any" count="note[@place='foot']" from="teiHeader"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <sup>
      <a href="#{concat('Note',$identifier)}">
        <xsl:value-of select="normalize-space($identifier)"/>
      </a>
    </sup>
  </xsl:template>

</xsl:stylesheet>
