<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
  Versione file XSLT: 1.2.01.04
  Versione formato fattura: 1.2
  Versione specifiche tecniche
    AdE: 1.6.2
    FatturaPA: 1.3
  Versione Quanto: 2021.1.0
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  <xsl:output method="xml"
              indent="yes"
              encoding="utf-8"/>


  <!--=================== Variabili e template utilizzati per formattazione, conversione e simili ===================-->

  <!-- Nuova riga -->
  <xsl:variable name="newline">
    <xsl:text>&#10;</xsl:text>
  </xsl:variable>

  <!-- Formattazione date (senza indicazione dell'ora) -->
  <xsl:template name="IS_formatDateNoTime">
    <xsl:param name="dateIn" />
    <!-- input format yyyy-MM-ddThh:mm:ss+... -->
    <!-- output format yyyy-MM-dd -->

    <xsl:variable name="yyyy">
      <xsl:value-of select="substring($dateIn,1,4)" />
    </xsl:variable>
    <xsl:variable name="MM">
      <xsl:value-of select="substring($dateIn,6,2)" />
    </xsl:variable>
    <xsl:variable name="dd">
      <xsl:value-of select="substring($dateIn,9,2)" />
    </xsl:variable>

    <xsl:value-of select="$yyyy" />
    <xsl:value-of select="'-'" />
    <xsl:value-of select="$MM" />
    <xsl:value-of select="'-'" />
    <xsl:value-of select="$dd" />
  </xsl:template>

  <!-- Inversione di numeri booleani (0 -> 1; maggiore di 0 -> 0) -->
  <xsl:template name="IS_invertBooleanNumber">
    <xsl:param name="numberIn" />

    <xsl:if test="$numberIn = 0">
      <xsl:value-of select="1" />
    </xsl:if>
    <xsl:if test="$numberIn > 0">
      <xsl:value-of select="0" />
    </xsl:if>
  </xsl:template>

  <!-- Conversione in stringa di numeri booleani (0 ->'NO'; maggiore di 0 -> 'SI') -->
  <xsl:template name="IS_convertBooleanNumberToYesNoString">
    <xsl:param name="numberIn" />

    <xsl:if test="$numberIn = 0">
      <xsl:value-of select="'NO'" />
    </xsl:if>
    <xsl:if test="$numberIn > 0">
      <xsl:value-of select="'SI'" />
    </xsl:if>
  </xsl:template>

  <xsl:template name="IS_Repeat">
    <xsl:param name="output" />
    <xsl:param name="count" />
    <xsl:if test="$count &gt; 0">
      <xsl:value-of select="$output" />
      <xsl:call-template name="IS_Repeat">
        <xsl:with-param name="output" select="$output" />
        <xsl:with-param name="count" select="$count - 1" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

  <!-- ::::: Sostituzione caratteri non validi ::::: -->

  <!-- Elenco caratteri alfabetici -->
  <xsl:variable name="Var_AlphabeticChars">
    <xsl:value-of select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'" />
  </xsl:variable>

  <!-- Elenco caratteri numerici -->
  <xsl:variable name="Var_NumericChars">
    <xsl:value-of select="'0123456789'" />
  </xsl:variable>

  <!-- Elenco Simboli 'Basic Latin' 1 -->
  <xsl:variable name="Var_SymbolicBasicLatinChars_1">
    <xsl:value-of select="'&#32;&#33;&quot;&#35;&#36;&#37;&amp;&#42;&#43;&#44;&#45;&#46;&#47;&#58;&#59;&lt;&#61;&gt;&#63;&#64;&#91;&#92;&#93;&#94;&#95;&#96;&#123;&#124;&#125;&#126;'" />
  </xsl:variable>

  <!-- Elenco Simboli 'Basic Latin' 2 -->
  <xsl:variable name="Var_SymbolicBasicLatinChars_2">'()</xsl:variable>
  <!-- In SymbolicBasicLatinChars_2 ci sono caratteri che, inclusi in SymbolicBasicLatinChars_1, danno problemi (errori da interprete xslt) -->

  <!-- Elenco caratteri 'Basic Latin' -->
  <xsl:variable name="Var_BasicLatinChars">
    <xsl:value-of select="concat($Var_AlphabeticChars, $Var_NumericChars, $Var_SymbolicBasicLatinChars_1, $Var_SymbolicBasicLatinChars_2)" />
  </xsl:variable>

  <!-- Elenco caratteri 'Latin-1 Supplement' -->
  <xsl:variable name="Var_Latin1SupplementChars">
    <xsl:value-of select="'&#161;&#162;&#163;&#164;&#165;&#166;&#167;&#168;&#169;&#170;&#171;&#172;&#173;&#174;&#175;&#176;&#177;&#178;&#179;&#180;&#181;&#182;&#183;&#184;&#185;&#186;&#187;&#188;&#189;&#190;&#191;&#192;&#193;&#194;&#195;&#196;&#197;&#198;&#199;&#200;&#201;&#202;&#203;&#204;&#205;&#206;&#207;&#208;&#209;&#210;&#211;&#212;&#213;&#214;&#215;&#216;&#217;&#218;&#219;&#220;&#221;&#222;&#223;&#224;&#225;&#226;&#227;&#228;&#229;&#230;&#231;&#232;&#233;&#234;&#235;&#236;&#237;&#238;&#239;&#240;&#241;&#242;&#243;&#244;&#245;&#246;&#247;&#248;&#249;&#250;&#251;&#252;&#253;&#254;&#255;'" />
  </xsl:variable>

  <!-- Carattere per rimpiazzare caratteri non validi -->
  <xsl:variable name="Var_InvalidReplacementChar">
    <xsl:value-of select="''" />
    <!-- Valore nullo -->
  </xsl:variable>

  <!-- Sostituzione caratteri non validi per 'Basic Latin' -->
  <xsl:template name="IS_convertInvalidBasicLatin">
    <xsl:param name="stringIn" />

    <xsl:variable name="transInvalid_1" select="'àèéòùì°&#8364;'" />
    <xsl:variable name="transReplacement_1" select="'aeeoui E'" />
    <xsl:variable name="output_1">
      <xsl:value-of select="translate($stringIn, $transInvalid_1, $transReplacement_1)" />
    </xsl:variable>

    <xsl:variable name="transInvalid_2" select="translate($output_1, $Var_BasicLatinChars,'')"/>
    <xsl:variable name="transReplacement_2">
      <xsl:call-template name="IS_Repeat">
        <xsl:with-param name="output" select="$Var_InvalidReplacementChar" />
        <xsl:with-param name="count" select="string-length($transInvalid_2)" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="translate($output_1, $transInvalid_2, $transReplacement_2)"/>
  </xsl:template>

  <!-- Sostituzione caratteri non validi per 'Basic Latin' + 'Latin-1 Supplement' -->
  <xsl:template name="IS_convertInvalidBasicAndLatin1Supplement">
    <xsl:param name="stringIn" />

    <xsl:variable name="transInvalid_1" select="'&#8364;'" />
    <xsl:variable name="transReplacement_1" select="'E'" />
    <xsl:variable name="output_1">
      <xsl:value-of select="translate($stringIn, $transInvalid_1, $transReplacement_1)" />
    </xsl:variable>

    <xsl:variable name="transInvalid_2" select="translate($output_1, concat($Var_BasicLatinChars, $Var_Latin1SupplementChars),'')"/>
    <xsl:variable name="transReplacement_2">
      <xsl:call-template name="IS_Repeat">
        <xsl:with-param name="output" select="$Var_InvalidReplacementChar" />
        <xsl:with-param name="count" select="string-length($transInvalid_2)" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:value-of select="translate($output_1, $transInvalid_2, $transReplacement_2)"/>
  </xsl:template>


  <!--============ Dati non associabili a una variabile fissa o ripetuti più volte ============-->

  <!-- PK testata record corrente-->
  <xsl:template name="Value_N_ID_DITTA_Current">
    <xsl:value-of select="N_ID_DITTA"/>
  </xsl:template>
  <xsl:template name="Value_N_TIPO_DOCV_BASE_Current">
    <xsl:value-of select="N_TIPO_DOCV_BASE"/>
  </xsl:template>
  <xsl:template name="Value_N_ID_TP_DOCV_Current">
    <xsl:value-of select="N_ID_TP_DOCV"/>
  </xsl:template>
  <xsl:template name="Value_C_ANNO_ESERCIZIO_Current">
    <xsl:value-of select="C_ANNO_ESERCIZIO"/>
  </xsl:template>
  <xsl:template name="Value_N_ID_SEDE_Current">
    <xsl:value-of select="N_ID_SEDE"/>
  </xsl:template>
  <xsl:template name="Value_N_NUM_DOCV_Current">
    <xsl:value-of select="N_NUM_DOCV"/>
  </xsl:template>

  <!-- PK testata record precedente -->
  <xsl:template name="Value_N_ID_DITTA_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/N_ID_DITTA"/>
  </xsl:template>
  <xsl:template name="Value_N_TIPO_DOCV_BASE_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/N_TIPO_DOCV_BASE"/>
  </xsl:template>
  <xsl:template name="Value_N_ID_TP_DOCV_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/N_ID_TP_DOCV"/>
  </xsl:template>
  <xsl:template name="Value_C_ANNO_ESERCIZIO_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/C_ANNO_ESERCIZIO"/>
  </xsl:template>
  <xsl:template name="Value_N_ID_SEDE_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/N_ID_SEDE"/>
  </xsl:template>
  <xsl:template name="Value_N_NUM_DOCV_Preceding">
    <xsl:value-of select="preceding-sibling::QuantoDataRow[1]/N_NUM_DOCV"/>
  </xsl:template>

  <!-- Controllo ripetizione testata documento -->
  <xsl:template name="Check_TDocv_Repeat">
    <xsl:variable name="N_ID_DITTA_Current">
      <xsl:call-template name="Value_N_ID_DITTA_Current"/>
    </xsl:variable>
    <xsl:variable name="N_TIPO_DOCV_BASE_Current">
      <xsl:call-template name="Value_N_TIPO_DOCV_BASE_Current"/>
    </xsl:variable>
    <xsl:variable name="N_ID_TP_DOCV_Current">
      <xsl:call-template name="Value_N_ID_TP_DOCV_Current"/>
    </xsl:variable>
    <xsl:variable name="C_ANNO_ESERCIZIO_Current">
      <xsl:call-template name="Value_C_ANNO_ESERCIZIO_Current"/>
    </xsl:variable>
    <xsl:variable name="N_ID_SEDE_Current">
      <xsl:call-template name="Value_N_ID_SEDE_Current"/>
    </xsl:variable>
    <xsl:variable name="N_NUM_DOCV_Current">
      <xsl:call-template name="Value_N_NUM_DOCV_Current"/>
    </xsl:variable>

    <xsl:variable name="N_ID_DITTA_Preceding">
      <xsl:call-template name="Value_N_ID_DITTA_Preceding"/>
    </xsl:variable>
    <xsl:variable name="N_TIPO_DOCV_BASE_Preceding">
      <xsl:call-template name="Value_N_TIPO_DOCV_BASE_Preceding"/>
    </xsl:variable>
    <xsl:variable name="N_ID_TP_DOCV_Preceding">
      <xsl:call-template name="Value_N_ID_TP_DOCV_Preceding"/>
    </xsl:variable>
    <xsl:variable name="C_ANNO_ESERCIZIO_Preceding">
      <xsl:call-template name="Value_C_ANNO_ESERCIZIO_Preceding"/>
    </xsl:variable>
    <xsl:variable name="N_ID_SEDE_Preceding">
      <xsl:call-template name="Value_N_ID_SEDE_Preceding"/>
    </xsl:variable>
    <xsl:variable name="N_NUM_DOCV_Preceding">
      <xsl:call-template name="Value_N_NUM_DOCV_Preceding"/>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="
                $N_ID_DITTA_Current = $N_ID_DITTA_Preceding and
                $N_TIPO_DOCV_BASE_Current = $N_TIPO_DOCV_BASE_Preceding and
                $N_ID_TP_DOCV_Current = $N_ID_TP_DOCV_Preceding and
                $C_ANNO_ESERCIZIO_Current = $C_ANNO_ESERCIZIO_Preceding and
                $N_ID_SEDE_Current = $N_ID_SEDE_Preceding and
                $N_NUM_DOCV_Current = $N_NUM_DOCV_Preceding
                ">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Id riga record corrente -->
  <xsl:template name="Value_N_ID_R_DOCV_Current">
    <xsl:value-of  select="N_ID_R_DOCV"/>
  </xsl:template>

  <!-- Id riga record precedente -->
  <xsl:template name="Value_N_ID_R_DOCV_Preceding">
    <xsl:value-of  select="preceding-sibling::QuantoDataRow[1]/N_ID_R_DOCV"/>
  </xsl:template>

  <!-- Controllo ripetizione riga documento -->
  <xsl:template name="Check_RDocv_Repeat">
    <xsl:variable name="N_ID_R_DOCV_Current">
      <xsl:call-template name="Value_N_ID_R_DOCV_Current"/>
    </xsl:variable>
    <xsl:variable name="N_ID_R_DOCV_Preceding">
      <xsl:call-template name="Value_N_ID_R_DOCV_Preceding"/>
    </xsl:variable>
    <xsl:variable name="TDocv_Repeat_InRDocv">
      <xsl:call-template name="Check_TDocv_Repeat" />
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="
                $TDocv_Repeat_InRDocv = 1 and
                $N_ID_R_DOCV_Current = $N_ID_R_DOCV_Preceding">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo natura giuridica/persona fisica -->
  <xsl:template name="Check_NaturaGiuridica">
    <xsl:param name="Sesso" />

    <xsl:choose>
      <xsl:when test="$Sesso = 'G'">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo contributo da indicare in fattura -->
  <xsl:template name="Check_ContribInFatt">
    <xsl:choose>
      <xsl:when test="C_DES_CONTRIB != '' and N_FLAG_CONTRIB_IN_R_DOCV = 0">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo tipo documento nota credito -->
  <xsl:template name="Check_NotaCredito">
    <xsl:choose>
      <xsl:when test="C_ID_TP_DOC_FTPA = 'TD04'">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo fattura in valuta con conversione importi -->
  <xsl:template name="Check_ConvVal">
    <xsl:choose>
      <xsl:when test="N_CHECK_CONV_VAL_FTPA = 1">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>


  <!-- IBAN Linea di Credito -->
  <xsl:template name="Value_IBAN_LineaCredito">
    <xsl:choose>
      <xsl:when test="C_IBAN_LC != ''">
        <xsl:value-of select="C_IBAN_LC" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="C_ID_IBAN_NAZIONE_LC != '' and C_CHECK_IBAN_LC != ''">
          <xsl:value-of select="C_ID_IBAN_NAZIONE_LC" />
          <xsl:value-of select="C_CHECK_IBAN_LC" />
          <xsl:value-of select="C_CIN_LC" />
          <xsl:value-of select="C_ABI_LC" />
          <xsl:value-of select="C_CAB_LIN_CREDIT" />
          <xsl:value-of select="C_CONTO_CORRENTE_LC" />
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- IBAN banca d'appoggio -->
  <xsl:template name="Value_IBAN_BancaAppoggio">
    <xsl:choose>
      <xsl:when test="C_IBAN != ''">
        <xsl:value-of select="C_IBAN" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="C_ID_IBAN_NAZIONE_LC != '' and C_CHECK_IBAN_LC != ''">
          <xsl:value-of select="C_ID_IBAN_NAZIONE" />
          <xsl:value-of select="C_CHECK_IBAN" />
          <xsl:value-of select="C_CIN" />
          <xsl:value-of select="C_ABI" />
          <xsl:value-of select="C_CAB" />
          <xsl:value-of select="C_CONTO_CORRENTE" />
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Quantità da indicare sulla riga -->
  <xsl:template name="Value_Qta_RDocv">
    <xsl:choose>
      <xsl:when test="N_IMPORTO_X_QMAG_V = 1">
        <xsl:value-of select="N_QTA_MAG_R_DOCV" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="N_QTA_USR_R_DOCV" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo segno negativo quantità da indicare sulla riga -->
  <xsl:template name="Check_QtaRDocvLTZero">
    <xsl:variable name="QtaRDocv">
      <xsl:call-template name="Value_Qta_RDocv" />
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$QtaRDocv &lt; 0">
        <xsl:value-of select="'1'" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="'0'" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Unità di misura da indicare sulla riga -->
  <xsl:template name="Value_UnitaMisura_RDocv">
    <xsl:variable name="QtaRDocv">
      <xsl:call-template name="Value_Qta_RDocv" />
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$QtaRDocv = 0">
        <xsl:value-of select="''" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="(not(C_ID_UM) or N_IMPORTO_X_QMAG_V = 1) ">
            <xsl:value-of  select="C_ID_UM_ARTICOLO" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of  select="C_ID_UM" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo split payment -->
  <xsl:template name="Check_SplitPayment">
    <xsl:param name="AliquotaIVA" />
    <xsl:param name="NumRigaIVA" />

    <xsl:variable name="SplitCliente">
      <xsl:choose>
        <xsl:when test="$AliquotaIVA = 0">
          <xsl:value-of select="0"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="substring(C_STR_SPLIT_PAY, 1, 1) = '1'">
              <xsl:value-of select="1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="0"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="SplitRigaIVA">
      <xsl:choose>
        <xsl:when test="$NumRigaIVA > 0">
          <xsl:choose>
            <xsl:when test="substring(C_STR_SPLIT_PAY, $NumRigaIVA + 1, 1) = '1'">
              <xsl:value-of select="1"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="0"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="0"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="
                $SplitCliente = 1 or
                $SplitRigaIVA = 1
                ">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="0"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Elemento 'Ritenuta' (generico) -->
  <xsl:template name="Element_Ritenuta_Generic">
    <xsl:param name="TotNumRigheRtnt" />
    <xsl:param name="FlagNoRtnt" />

    <xsl:if test="$TotNumRigheRtnt > 0 and $FlagNoRtnt = 0">
      <Ritenuta>

        <xsl:variable name="FlagInverted">
          <xsl:call-template name="IS_invertBooleanNumber">
            <xsl:with-param name="numberIn" select="$FlagNoRtnt" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:call-template name="IS_convertBooleanNumberToYesNoString">
          <xsl:with-param name="numberIn" select="$FlagInverted" />
        </xsl:call-template>

      </Ritenuta>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Codice nazione Italia -->
  <xsl:variable name="Var_IdNazioneItalia">
    <xsl:value-of select="'IT'" />
  </xsl:variable>

  <!-- Controllo nazione Italia -->
  <xsl:template name="Check_NazioneItalia">
    <xsl:param name="IdNazione" />

    <xsl:choose>
      <xsl:when test="normalize-space($IdNazione) = $Var_IdNazioneItalia">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="0"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Nazione (con default in caso di valore nullo) -->
  <xsl:template name="Value_Nazione">
    <xsl:param name="IdNazione" />

    <xsl:choose>
      <xsl:when test="$IdNazione != ''">
        <xsl:value-of select="normalize-space($IdNazione)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$Var_IdNazioneItalia" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- CAP destinatario estero -->
  <xsl:variable name="Var_CapDestinatarioEstero">
    <xsl:value-of select="'00000'" />
  </xsl:variable>

  <!-- Codice destinatario estero -->
  <xsl:variable name="Var_CodiceDestinatarioEstero">
    <xsl:value-of select="'XXXXXXX'" />
  </xsl:variable>

  <!-- Controllo codice destinatario estero -->
  <xsl:template name="Check_CodiceDestinatarioEstero">
    <xsl:param name="CodiceDestinatario" />

    <xsl:choose>
      <xsl:when test="$CodiceDestinatario = $Var_CodiceDestinatarioEstero">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="0"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo per determinare se fattura emessa da cessionario/committente -->
  <xsl:template name="Check_EmessaCC">
    <xsl:choose>
      <xsl:when test="N_FLAG_EMESSA_DA_CC_FTPA = 1">
        <xsl:value-of select="1"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="0"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- Dato cedente/prestatore (generico) -->
  <!-- Ritorna il valore del primo parametro o del secondo, a seconda che la fattura sia emessa da CC oppure no -->
  <xsl:template name="Value_DatoCedentePrestatore_Generic">
    <xsl:param name="DatoDaFornitore" />
    <xsl:param name="DatoDaDitta" />
    
    <xsl:variable name="IsCC">
      <xsl:call-template name="Check_EmessaCC"/>
    </xsl:variable>
    
    <xsl:choose>
      <xsl:when test="$IsCC = 1">
        <xsl:value-of select="$DatoDaFornitore"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$DatoDaDitta"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Controllo formato (precede l'inizio della lettura dati) -->
  <xsl:template name="Check_VersoPrivati">
    <xsl:value-of select="GENFILE_FATTURE_V1_2_00/QuantoDataRow[1]/N_FLAG_TP_FORMATO_FTPA"/>
  </xsl:template>



  <!--======================= Elementi da inserire in file destinazione =======================-->


  <!--::::::::::::::::::::::::::::::::::::::::: Root :::::::::::::::::::::::::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- FatturaElettronicaHeader (in FatturaElettronica) -->
  <xsl:template name="Block_FatturaElettronicaHeader_FatturaElettronica">
    <xsl:if test="position() = 1">
      <xsl:call-template name="Block_FatturaElettronicaHeader" />
    </xsl:if>

  </xsl:template>

  <!-- FatturaElettronicaBody (in FatturaElettronica) -->
  <xsl:template name="Block_FatturaElettronicaBody_FatturaElettronica">
    <xsl:variable name="TDocv_Repeat">
      <xsl:call-template name="Check_TDocv_Repeat" />
    </xsl:variable>

    <xsl:if test="position() = 1 or $TDocv_Repeat = 0">
      <xsl:call-template name="Block_FatturaElettronicaBody"/>
    </xsl:if>

  </xsl:template>

  <!-- Root -->
  <xsl:template name="Block_FatturaElettronica">
    <xsl:choose>
      <xsl:when test="$IsVersoPrivati = 1">
        <xsl:call-template name="Block_FatturaElettronica_PR" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="Block_FatturaElettronica_PA" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:variable name="IsVersoPrivati">
    <xsl:call-template name="Check_VersoPrivati"/>
  </xsl:variable>

  <xsl:template name="Block_FatturaElettronica_PA">
    <p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
                          xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          versione="FPA12"
                          xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd ">
      <xsl:value-of select="$newline" />

      <xsl:for-each select="GENFILE_FATTURE_V1_2_00/QuantoDataRow">

        <!-- Header -->
        <xsl:call-template name="Block_FatturaElettronicaHeader_FatturaElettronica" />

        <!-- Body -->
        <xsl:call-template name="Block_FatturaElettronicaBody_FatturaElettronica" />

      </xsl:for-each>

      <xsl:value-of select="$newline" />
    </p:FatturaElettronica>

  </xsl:template>

  <xsl:template name="Block_FatturaElettronica_PR">
    <p:FatturaElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
                          xmlns:p="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          versione="FPR12"
                          xsi:schemaLocation="http://ivaservizi.agenziaentrate.gov.it/docs/xsd/fatture/v1.2 fatturaordinaria_v1.2.xsd ">
      <xsl:value-of select="$newline" />

      <xsl:for-each select="GENFILE_FATTURE_V1_2_00/QuantoDataRow">

        <!-- Header -->
        <xsl:call-template name="Block_FatturaElettronicaHeader_FatturaElettronica" />

        <!-- Body -->
        <xsl:call-template name="Block_FatturaElettronicaBody_FatturaElettronica" />

      </xsl:for-each>

    </p:FatturaElettronica>

  </xsl:template>


  <!--::::::::::::::::::::::::::::::::::::::::: Header :::::::::::::::::::::::::::::::::::::::::-->

  <!--::::::::::::::: FatturaElettronicaHeader :::::::::::::::-->

  <!-- Blocchi -->

  <!-- FatturaElettronicaHeader -->
  <xsl:template name="Block_FatturaElettronicaHeader">
    <FatturaElettronicaHeader>
      <xsl:value-of select="$newline" />

      <!-- DatiTrasmissione -->
      <xsl:call-template name="Block_DatiTrasmissione"/>

      <!-- CedentePrestatore -->
      <xsl:call-template name="Block_CedentePrestatore"/>

      <!-- RappresentanteFiscale -->
      <xsl:call-template name="Block_RappresentanteFiscale"/>

      <!-- CessionarioCommittente -->
      <xsl:call-template name="Block_CessionarioCommittente"/>

      <!-- TerzoIntermediarioOSoggettoEmittente -->
      <xsl:call-template name="Block_TerzoIntermediarioOSoggettoEmittente"/>

      <!-- SoggettoEmittente -->
      <xsl:call-template name="Element_SoggettoEmittente"/>

    </FatturaElettronicaHeader>
    <xsl:value-of select="$newline" />

  </xsl:template>


  <!--::::::::::::::::::: DatiTrasmissione :::::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- IdPaese (in IdTrasmittente) -->
  <xsl:template name="Element_IdPaese_IdTrasmittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_TRSMTT_FTPA" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:if test="$IdNazione">
      <IdPaese>
        <xsl:value-of select="$IdNazione" />
      </IdPaese>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- IdCodice (in IdTrasmittente) -->
  <xsl:template name="Element_IdCodice_IdTrasmittente">
    <xsl:if test="C_ID_FISC_TRSMTT_FTPA">
      <IdCodice>
        <xsl:value-of select="C_ID_FISC_TRSMTT_FTPA" />
      </IdCodice>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ProgressivoInvio -->
  <xsl:template name="Element_ProgressivoInvio">
    <ProgressivoInvio>
      <xsl:value-of select="C_ID_INVIO_FTPA" />
    </ProgressivoInvio>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- FormatoTrasmissione -->
  <xsl:template name="Element_FormatoTrasmissione">
    <FormatoTrasmissione>
      <xsl:if test="N_FLAG_TP_FORMATO_FTPA = 0">
        <xsl:value-of select="'FPA12'" />
      </xsl:if>
      <xsl:if test="N_FLAG_TP_FORMATO_FTPA = 1">
        <xsl:value-of select="'FPR12'" />
      </xsl:if>
    </FormatoTrasmissione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceDestinatario -->
  <xsl:template name="Element_CodiceDestinatario">
    <CodiceDestinatario>
      <xsl:value-of select="C_COD_IPA_FTPA" />
    </CodiceDestinatario>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Telefono (in ContattiTrasmittente) -->
  <xsl:template name="Element_Telefono_ContattiTrasmittente">
    <xsl:if test="normalize-space(C_NTEL_TRSMTT_FTPA) != ''">
      <Telefono>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="normalize-space(C_NTEL_TRSMTT_FTPA)" />
        </xsl:call-template>
      </Telefono>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Email (in ContattiTrasmittente) -->
  <xsl:template name="Element_Email_ContattiTrasmittente">
    <xsl:if test="normalize-space(C_EMAIL_TRSMTT_FTPA) != ''">
      <Email>
        <xsl:value-of select="normalize-space(C_EMAIL_TRSMTT_FTPA)" />
      </Email>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- PECDestinatario -->
  <xsl:template name="Element_PECDestinatario">
    <xsl:if test="N_FLAG_PEC_FTPA = 1">
      <PECDestinatario>
        <xsl:value-of select="C_EMAIL_PEC_FTPA" />
      </PECDestinatario>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- ContattiTrasmittente -->
  <xsl:template name="Block_ContattiTrasmittente">
    <xsl:if test="normalize-space(C_NTEL_TRSMTT_FTPA) != '' or normalize-space(C_EMAIL_TRSMTT_FTPA) != ''">
      <ContattiTrasmittente>
        <xsl:value-of select="$newline" />

        <!-- Telefono (in ContattiTrasmittente) -->
        <xsl:call-template name="Element_Telefono_ContattiTrasmittente"/>

        <!-- Email (in ContattiTrasmittente) -->
        <xsl:call-template name="Element_Email_ContattiTrasmittente"/>

      </ContattiTrasmittente>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- DatiTrasmissione -->
  <xsl:template name="Block_DatiTrasmissione">
    <DatiTrasmissione>
      <xsl:value-of select="$newline" />

      <!-- IdTrasmittente -->
      <xsl:if test="C_ID_FISC_TRSMTT_FTPA">
        <IdTrasmittente>
          <xsl:value-of select="$newline" />

          <!-- IdPaese -->
          <xsl:call-template name="Element_IdPaese_IdTrasmittente"/>

          <!-- IdCodice -->
          <xsl:call-template name="Element_IdCodice_IdTrasmittente"/>

        </IdTrasmittente>
        <xsl:value-of select="$newline" />
      </xsl:if>

      <!-- ProgressivoInvio -->
      <xsl:call-template name="Element_ProgressivoInvio"/>

      <!-- FormatoTrasmissione -->
      <xsl:call-template name="Element_FormatoTrasmissione"/>

      <!-- CodiceDestinatario -->
      <xsl:call-template name="Element_CodiceDestinatario"/>

      <!-- ContattiTrasmittente -->
      <xsl:call-template name="Block_ContattiTrasmittente"/>

      <!-- PECDestinatario -->
      <xsl:call-template name="Element_PECDestinatario"/>

    </DatiTrasmissione>
    <xsl:value-of select="$newline" />

  </xsl:template>


  <!--:::::::::::::::::: CedentePrestatore ::::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- IdPaese (in CedentePrestatore) -->
  <xsl:template name="Element_IdPaese_CedentePrestatore">
    <xsl:variable name="IdNazione">
      <xsl:variable name="DatoCedentePrestatore">
        <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
          <xsl:with-param name="DatoDaFornitore" select="C_ID_NAZIONE_PIVA_CC_FTPA" />
          <xsl:with-param name="DatoDaDitta" select="C_DITTA_ID_NAZIONE_PIVA" />
        </xsl:call-template>
      </xsl:variable>      
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="$DatoCedentePrestatore" />
      </xsl:call-template>
    </xsl:variable>
    <IdPaese>
      <xsl:value-of select="$IdNazione" />
    </IdPaese>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- IdCodice (in CedentePrestatore) -->
  <xsl:template name="Element_IdCodice_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_PIVA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_PIVA" />
      </xsl:call-template>
    </xsl:variable>
    
    <IdCodice>
      <xsl:value-of select="normalize-space($DatoCedentePrestatore)" />
    </IdCodice>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceFiscale (in CedentePrestatore) -->
  <xsl:template name="Element_CodiceFiscale_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_CODFISC_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_CODFISC" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="normalize-space($DatoCedentePrestatore) != ''">
      <CodiceFiscale>
        <xsl:value-of select="normalize-space($DatoCedentePrestatore)" />
      </CodiceFiscale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Denominazione (in CedentePrestatore) -->
  <xsl:template name="Element_Denominazione_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_RAGSOC1_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_RAGSOC1" />
      </xsl:call-template>
    </xsl:variable>
    
    <Denominazione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($DatoCedentePrestatore, 1, 80)" />
      </xsl:call-template>
    </Denominazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Nome (in CedentePrestatore) -->
  <xsl:template name="Element_Nome_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_RAGSOC2_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_RAGSOC2" />
      </xsl:call-template>
    </xsl:variable>
    
    <Nome>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($DatoCedentePrestatore, 1, 60)" />
      </xsl:call-template>
    </Nome>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Cognome (in CedentePrestatore) -->
  <xsl:template name="Element_Cognome_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_RAGSOC1_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_RAGSOC1" />
      </xsl:call-template>
    </xsl:variable>
    
    <Cognome>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($DatoCedentePrestatore, 1, 60)" />
      </xsl:call-template>
    </Cognome>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- RegimeFiscale -->
  <xsl:template name="Element_RegimeFiscale">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_ID_REGIME_FISC_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_ID_REGIME_FISC_FTPA" />
      </xsl:call-template>
    </xsl:variable>
    
    <RegimeFiscale>
      <xsl:value-of select="$DatoCedentePrestatore" />
    </RegimeFiscale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Indirizzo (in CedentePrestatore) -->
  <xsl:template name="Element_Indirizzo_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_INDIR_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_INDIRIZZO" />
      </xsl:call-template>
    </xsl:variable>
    
    <Indirizzo>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($DatoCedentePrestatore, 1, 60)" />
      </xsl:call-template>
    </Indirizzo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CAP (in CedentePrestatore) -->
  <xsl:template name="Element_CAP_CedentePrestatore">
    <xsl:variable name="IdNazione_CC">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_CC_FTPA" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="IsNazioneItalia_CC">
      <xsl:call-template name="Check_NazioneItalia">
        <xsl:with-param name="IdNazione" select="$IdNazione_CC" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="CAP_CC">
      <xsl:choose>
        <xsl:when test="$IsNazioneItalia_CC = 0">
          <xsl:value-of select="$Var_CapDestinatarioEstero" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="normalize-space(C_CAP_CC_FTPA)" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="$CAP_CC" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_CAP" />
      </xsl:call-template>
    </xsl:variable>
    
    <CAP>
      <xsl:value-of select="normalize-space($DatoCedentePrestatore)" />
    </CAP>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Comune (in CedentePrestatore) -->
  <xsl:template name="Element_Comune_CedentePrestatore">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_CITTA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_DITTA_CITTA" />
      </xsl:call-template>
    </xsl:variable>
    
    <Comune>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($DatoCedentePrestatore, 1, 60)" />
      </xsl:call-template>
    </Comune>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Provincia (in CedentePrestatore) -->
  <xsl:template name="Element_Provincia_CedentePrestatore">
    <xsl:variable name="IdNazione">
      <xsl:variable name="DatoCedentePrestatore">
        <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
          <xsl:with-param name="DatoDaFornitore" select="C_ID_NAZIONE_CC_FTPA" />
          <xsl:with-param name="DatoDaDitta" select="C_DITTA_ID_NAZIONE" />
        </xsl:call-template>
      </xsl:variable>      
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="$DatoCedentePrestatore" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="IsNazioneItalia">
      <xsl:call-template name="Check_NazioneItalia">
        <xsl:with-param name="IdNazione" select="$IdNazione" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$IsNazioneItalia = 1">
      <xsl:variable name="DatoCedentePrestatore2">
        <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
          <xsl:with-param name="DatoDaFornitore" select="C_ID_PROVINCIA_CC_FTPA" />
          <xsl:with-param name="DatoDaDitta" select="C_DITTA_ID_PROV" />
        </xsl:call-template>
      </xsl:variable>
      <xsl:if test="normalize-space($DatoCedentePrestatore2) != ''">
        <Provincia>
          <xsl:value-of select="normalize-space($DatoCedentePrestatore2)" />
        </Provincia>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Nazione (in CedentePrestatore) -->
  <xsl:template name="Element_Nazione_CedentePrestatore">
    <xsl:variable name="IdNazione">
      <xsl:variable name="DatoCedentePrestatore">
        <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
          <xsl:with-param name="DatoDaFornitore" select="C_ID_NAZIONE_CC_FTPA" />
          <xsl:with-param name="DatoDaDitta" select="C_DITTA_ID_NAZIONE" />
        </xsl:call-template>
      </xsl:variable>
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="$DatoCedentePrestatore" />
      </xsl:call-template>
    </xsl:variable>
    <Nazione>
      <xsl:value-of select="$IdNazione" />
    </Nazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ufficio (in IscrizioneREA) -->
  <xsl:template name="Element_Ufficio_IscrizioneREA">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_ID_PROVINCIA_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_ID_PROVINCIA_REA" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <Ufficio>
        <xsl:value-of select="$DatoCedentePrestatore" />
      </Ufficio>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- NumeroREA -->
  <xsl:template name="Element_NumeroREA">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_NUM_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_NUM_REA" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <NumeroREA>
        <xsl:value-of select="$DatoCedentePrestatore" />
      </NumeroREA>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CapitaleSociale -->
  <xsl:template name="Element_CapitaleSociale">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="N_CAP_SOC_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="N_CAP_SOC_REA" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <CapitaleSociale>
        <xsl:value-of select='format-number($DatoCedentePrestatore, "0.00####")' />
      </CapitaleSociale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- SocioUnico -->
  <xsl:template name="Element_SocioUnico">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_TP_SRL_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_TP_SRL_REA" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <SocioUnico>
        <xsl:value-of select="$DatoCedentePrestatore" />
      </SocioUnico>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- StatoLiquidazione -->
  <xsl:template name="Element_StatoLiquidazione">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_NUM_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_NUM_REA" />
      </xsl:call-template>
    </xsl:variable>
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <xsl:variable name="DatoCedentePrestatore2">
        <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
          <xsl:with-param name="DatoDaFornitore" select="N_FLAG_LIQ_REA_CC_FTPA" />
          <xsl:with-param name="DatoDaDitta" select="N_FLAG_LIQ_REA" />
        </xsl:call-template>
      </xsl:variable>
      
      <StatoLiquidazione>
        <xsl:choose>
          <xsl:when test="$DatoCedentePrestatore2 = 1">
            <xsl:value-of select="'LS'" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'LN'"/>
          </xsl:otherwise>
        </xsl:choose>
      </StatoLiquidazione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoAmministrazione (in CedentePrestatore) -->
  <xsl:template name="Element_RiferimentoAmministrazione_CedentePrestatore">
    <xsl:if test="C_RIF_AMMNSTRZN_FTPA">
      <RiferimentoAmministrazione>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="C_RIF_AMMNSTRZN_FTPA" />
        </xsl:call-template>
      </RiferimentoAmministrazione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DatiAnagrafici (in CedentePrestatore) -->
  <xsl:template name="Block_DatiAnagrafici_CedentePrestatore">
    <DatiAnagrafici>
      <xsl:value-of select="$newline" />

      <!-- IdFiscaleIVA (in CedentePrestatore) -->
      <IdFiscaleIVA>
        <xsl:value-of select="$newline" />

        <!-- IdPaese (in CedentePrestatore) -->
        <xsl:call-template name="Element_IdPaese_CedentePrestatore"/>

        <!-- IdCodice (in CedentePrestatore) -->
        <xsl:call-template name="Element_IdCodice_CedentePrestatore"/>

      </IdFiscaleIVA>
      <xsl:value-of select="$newline" />

      <!-- CodiceFiscale (in CedentePrestatore) -->
      <xsl:call-template name="Element_CodiceFiscale_CedentePrestatore"/>

      <!-- Anagrafica (in CedentePrestatore) -->
      <Anagrafica>
        <xsl:value-of select="$newline" />

        <!-- Scelta tra denominazione e nome/cognome -->
        <xsl:variable name="NaturaGiuridica">
          <xsl:variable name="DatoCedentePrestatore">
            <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
              <xsl:with-param name="DatoDaFornitore" select="C_SESSO_CC_FTPA" />
              <xsl:with-param name="DatoDaDitta" select="C_DITTA_SESSO" />
            </xsl:call-template>
          </xsl:variable>
          
          <xsl:call-template name="Check_NaturaGiuridica">
            <xsl:with-param name="Sesso" select="$DatoCedentePrestatore" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test="$NaturaGiuridica = 1">

            <!-- Denominazione (in CedentePrestatore) -->
            <xsl:call-template name="Element_Denominazione_CedentePrestatore"/>

          </xsl:when>
          <xsl:otherwise>

            <!-- Nome (in CedentePrestatore) -->
            <xsl:call-template name="Element_Nome_CedentePrestatore"/>

            <!-- Cognome (in CedentePrestatore) -->
            <xsl:call-template name="Element_Cognome_CedentePrestatore"/>

          </xsl:otherwise>
        </xsl:choose>

      </Anagrafica>
      <xsl:value-of select="$newline" />

      <!-- RegimeFiscale -->
      <xsl:call-template name="Element_RegimeFiscale"/>

    </DatiAnagrafici>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- Sede (in CedentePrestatore) -->
  <xsl:template name="Block_Sede_CedentePrestatore">
    <Sede>
      <xsl:value-of select="$newline" />

      <!-- Indirizzo (in CedentePrestatore) -->
      <xsl:call-template name="Element_Indirizzo_CedentePrestatore"/>

      <!-- CAP (in CedentePrestatore) -->
      <xsl:call-template name="Element_CAP_CedentePrestatore"/>

      <!-- Comune (in CedentePrestatore) -->
      <xsl:call-template name="Element_Comune_CedentePrestatore"/>

      <!-- Provincia (in CedentePrestatore) -->
      <xsl:call-template name="Element_Provincia_CedentePrestatore"/>

      <!-- Nazione (in CedentePrestatore) -->
      <xsl:call-template name="Element_Nazione_CedentePrestatore"/>

    </Sede>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- StabileOrganizzazione (in CedentePrestatore) -->
  <xsl:template name="Block_StabileOrganizzazione_CedentePrestatore">
    <!-- Non gestito -->
  </xsl:template>

  <!-- IscrizioneREA -->
  <xsl:template name="Block_IscrizioneREA">
    <xsl:variable name="DatoCedentePrestatore">
      <xsl:call-template name="Value_DatoCedentePrestatore_Generic">
        <xsl:with-param name="DatoDaFornitore" select="C_NUM_REA_CC_FTPA" />
        <xsl:with-param name="DatoDaDitta" select="C_NUM_REA" />
      </xsl:call-template>
    </xsl:variable>    
    
    <xsl:if test="$DatoCedentePrestatore != ''">
      <IscrizioneREA>
        <xsl:value-of select="$newline" />

        <!-- Ufficio (in IscrizioneREA) -->
        <xsl:call-template name="Element_Ufficio_IscrizioneREA"/>

        <!-- NumeroREA -->
        <xsl:call-template name="Element_NumeroREA"/>

        <!-- CapitaleSociale -->
        <xsl:call-template name="Element_CapitaleSociale"/>

        <!-- SocioUnico -->
        <xsl:call-template name="Element_SocioUnico"/>

        <!-- StatoLiquidazione -->
        <xsl:call-template name="Element_StatoLiquidazione"/>

      </IscrizioneREA>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>

  <!-- Contatti (in CedentePrestatore) -->
  <xsl:template name="Block_Contatti_CedentePrestatore">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CedentePrestatore -->
  <xsl:template name="Block_CedentePrestatore">
    <CedentePrestatore>
      <xsl:value-of select="$newline" />

      <!-- DatiAnagrafici (in CedentePrestatore) -->
      <xsl:call-template name="Block_DatiAnagrafici_CedentePrestatore"/>

      <!-- Sede (in CedentePrestatore) -->
      <xsl:call-template name="Block_Sede_CedentePrestatore"/>

      <!-- StabileOrganizzazione (in CedentePrestatore) -->
      <xsl:call-template name="Block_StabileOrganizzazione_CedentePrestatore"/>

      <!-- IscrizioneREA -->
      <xsl:call-template name="Block_IscrizioneREA"/>

      <!-- Contatti (in CedentePrestatore) -->
      <xsl:call-template name="Block_Contatti_CedentePrestatore"/>

      <!-- RiferimentoAmministrazione (in CedentePrestatore) -->
      <xsl:call-template name="Element_RiferimentoAmministrazione_CedentePrestatore"/>

    </CedentePrestatore>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!--:::::::::::::::::: RappresentanteFiscale ::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- RappresentanteFiscale -->
  <xsl:template name="Block_RappresentanteFiscale">
    <!-- Non gestito -->
  </xsl:template>

  <!--:::::::::::::::: CessionarioCommittente ::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- IdPaese (in CessionarioCommittente) -->
  <xsl:template name="Element_IdPaese_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_PIVA_INT" />
      </xsl:call-template>
    </xsl:variable>
    <IdPaese>
      <xsl:value-of select="$IdNazione" />
    </IdPaese>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- IdCodice (in CessionarioCommittente) -->
  <xsl:template name="Element_IdCodice_CessionarioCommittente">
    <IdCodice>
      <xsl:value-of select="normalize-space(C_PIVA_INT)" />
    </IdCodice>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceFiscale (in CessionarioCommittente) -->
  <xsl:template name="Element_CodiceFiscale_CessionarioCommittente">
    <xsl:if test="normalize-space(C_CODFISC_INT) != ''">
      <CodiceFiscale>
        <xsl:value-of select="normalize-space(C_CODFISC_INT)" />
      </CodiceFiscale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Denominazione (in CessionarioCommittente) -->
  <xsl:template name="Element_Denominazione_CessionarioCommittente">
    <Denominazione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_INT, 1, 80)" />
      </xsl:call-template>
    </Denominazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Nome (in CessionarioCommittente) -->
  <xsl:template name="Element_Nome_CessionarioCommittente">
    <xsl:if test="C_RAGSOC2_INT">
      <Nome>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="substring(C_RAGSOC2_INT, 1, 60)" />
        </xsl:call-template>
      </Nome>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Cognome (in CessionarioCommittente) -->
  <xsl:template name="Element_Cognome_CessionarioCommittente">
    <Cognome>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_INT, 1, 60)" />
      </xsl:call-template>
    </Cognome>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Indirizzo (in CessionarioCommittente) -->
  <xsl:template name="Element_Indirizzo_CessionarioCommittente">
    <Indirizzo>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_INDIR_INT, 1, 60)" />
      </xsl:call-template>
    </Indirizzo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CAP (in CessionarioCommittente) -->
  <xsl:template name="Element_CAP_CessionarioCommittente">
    <xsl:variable name="IsDestinatarioEstero">
      <xsl:call-template name="Check_CodiceDestinatarioEstero">
        <xsl:with-param name="CodiceDestinatario" select="C_COD_IPA_FTPA" />
      </xsl:call-template>
    </xsl:variable>

    <CAP>
      <xsl:choose>
        <xsl:when test="$IsDestinatarioEstero = 1">
          <xsl:value-of select="$Var_CapDestinatarioEstero" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="normalize-space(C_CAP_INT)" />
        </xsl:otherwise>
      </xsl:choose>
    </CAP>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Comune (in CessionarioCommittente) -->
  <xsl:template name="Element_Comune_CessionarioCommittente">
    <Comune>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_CITTA_INT, 1, 60)" />
      </xsl:call-template>
    </Comune>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Provincia (in CessionarioCommittente) -->
  <xsl:template name="Element_Provincia_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_INT" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="IsNazioneItalia">
      <xsl:call-template name="Check_NazioneItalia">
        <xsl:with-param name="IdNazione" select="$IdNazione" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$IsNazioneItalia = 1">
      <xsl:if test="normalize-space(C_ID_PROVINCIA_INT) != ''">
        <Provincia>
          <xsl:value-of select="normalize-space(C_ID_PROVINCIA_INT)" />
        </Provincia>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Nazione (in CessionarioCommittente) -->
  <xsl:template name="Element_Nazione_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_INT" />
      </xsl:call-template>
    </xsl:variable>
    <Nazione>
      <xsl:value-of select="$IdNazione" />
    </Nazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Indirizzo (in StabileOrganizzazione in CessionarioCommittente) -->
  <xsl:template name="Element_Indirizzo_StabileOrganizzazione_CessionarioCommittente">
    <Indirizzo>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_INDIR_STBL_ORG_FTPA, 1, 60)" />
      </xsl:call-template>
    </Indirizzo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CAP (in StabileOrganizzazione in CessionarioCommittente) -->
  <xsl:template name="Element_CAP_StabileOrganizzazione_CessionarioCommittente">
    <CAP>
      <xsl:value-of select="normalize-space(C_CAP_STBL_ORG_FTPA)" />
    </CAP>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Comune (in StabileOrganizzazione in CessionarioCommittente) -->
  <xsl:template name="Element_Comune_StabileOrganizzazione_CessionarioCommittente">
    <Comune>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_CITTA_STBL_ORG_FTPA, 1, 60)" />
      </xsl:call-template>
    </Comune>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Provincia (in StabileOrganizzazione in CessionarioCommittente) -->
  <xsl:template name="Element_Provincia_StabileOrganizzazione_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_STBL_ORG_FTPA" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="IsNazioneItalia">
      <xsl:call-template name="Check_NazioneItalia">
        <xsl:with-param name="IdNazione" select="$IdNazione" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$IsNazioneItalia = 1">
      <xsl:if test="normalize-space(C_ID_PROVINCIA_STBL_ORG_FTPA) != ''">
        <Provincia>
          <xsl:value-of select="normalize-space(C_ID_PROVINCIA_STBL_ORG_FTPA)" />
        </Provincia>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Nazione (in StabileOrganizzazione in CessionarioCommittente) -->
  <xsl:template name="Element_Nazione_StabileOrganizzazione_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_STBL_ORG_FTPA" />
      </xsl:call-template>
    </xsl:variable>
    <Nazione>
      <xsl:value-of select="$IdNazione" />
    </Nazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- IdPaese (in RappresentanteFiscale in CessionarioCommittente) -->
  <xsl:template name="Element_IdPaese_RappresentanteFiscale_CessionarioCommittente">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_PIVA_RAPPR_FISC_FTPA" />
      </xsl:call-template>
    </xsl:variable>
    <IdPaese>
      <xsl:value-of select="$IdNazione" />
    </IdPaese>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- IdCodice (in RappresentanteFiscale in CessionarioCommittente) -->
  <xsl:template name="Element_IdCodice_RappresentanteFiscale_CessionarioCommittente">
    <IdCodice>
      <xsl:choose>
        <xsl:when test="normalize-space(C_PIVA_RAPPR_FISC_FTPA) != ''">
          <xsl:value-of select="normalize-space(C_PIVA_RAPPR_FISC_FTPA)" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="normalize-space(C_CODFISC_RAPPR_FISC_FTPA)" />
        </xsl:otherwise>
      </xsl:choose>
    </IdCodice>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Denominazione (in RappresentanteFiscale in CessionarioCommittente) -->
  <xsl:template name="Element_Denominazione_RappresentanteFiscale_CessionarioCommittente">
    <Denominazione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_RAPPR_FISC_FTPA, 1, 80)" />
      </xsl:call-template>
    </Denominazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Nome (in RappresentanteFiscale in CessionarioCommittente) -->
  <xsl:template name="Element_Nome_RappresentanteFiscale_CessionarioCommittente">
    <xsl:if test="C_RAGSOC2_RAPPR_FISC_FTPA">
      <Nome>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="substring(C_RAGSOC2_RAPPR_FISC_FTPA, 1, 60)" />
        </xsl:call-template>
      </Nome>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Cognome (in RappresentanteFiscale in CessionarioCommittente) -->
  <xsl:template name="Element_Cognome_RappresentanteFiscale_CessionarioCommittente">
    <Cognome>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_RAPPR_FISC_FTPA, 1, 60)" />
      </xsl:call-template>
    </Cognome>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Blocchi -->

  <!-- DatiAnagrafici (in CessionarioCommittente) -->
  <xsl:template name="Block_DatiAnagrafici_CessionarioCommittente">
    <DatiAnagrafici>
      <xsl:value-of select="$newline" />

      <!-- Id fiscale -->
      <xsl:if test="normalize-space(C_PIVA_INT) != ''">

        <!-- IdFiscaleIVA (in CessionarioCommittente) -->
        <IdFiscaleIVA>
          <xsl:value-of select="$newline" />

          <!-- IdPaese (in CessionarioCommittente) -->
          <xsl:call-template name="Element_IdPaese_CessionarioCommittente"/>

          <!-- IdCodice (in CessionarioCommittente) -->
          <xsl:call-template name="Element_IdCodice_CessionarioCommittente"/>

        </IdFiscaleIVA>
        <xsl:value-of select="$newline" />

      </xsl:if>

      <!-- CodiceFiscale (in CessionarioCommittente) -->
      <xsl:call-template name="Element_CodiceFiscale_CessionarioCommittente"/>

      <!-- Anagrafica (in CessionarioCommittente) -->
      <Anagrafica>
        <xsl:value-of select="$newline" />

        <!-- Scelta tra denominazione e nome/cognome -->
        <xsl:variable name="NaturaGiuridica">
          <xsl:call-template name="Check_NaturaGiuridica">
            <xsl:with-param name="Sesso" select="C_SESSO_INT" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test="$NaturaGiuridica = 1">

            <!-- Denominazione (in CessionarioCommittente) -->
            <xsl:call-template name="Element_Denominazione_CessionarioCommittente"/>

          </xsl:when>
          <xsl:otherwise>

            <!-- Nome (in CessionarioCommittente) -->
            <xsl:call-template name="Element_Nome_CessionarioCommittente"/>

            <!-- Cognome (in CessionarioCommittente) -->
            <xsl:call-template name="Element_Cognome_CessionarioCommittente"/>

          </xsl:otherwise>
        </xsl:choose>

      </Anagrafica>
      <xsl:value-of select="$newline" />

    </DatiAnagrafici>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- Sede (in CessionarioCommittente) -->
  <xsl:template name="Block_Sede_CessionarioCommittente">
    <Sede>
      <xsl:value-of select="$newline" />

      <!-- Indirizzo (in CessionarioCommittente) -->
      <xsl:call-template name="Element_Indirizzo_CessionarioCommittente"/>

      <!-- CAP (in CessionarioCommittente) -->
      <xsl:call-template name="Element_CAP_CessionarioCommittente"/>

      <!-- Comune (in CessionarioCommittente) -->
      <xsl:call-template name="Element_Comune_CessionarioCommittente"/>

      <!-- Provincia (in CessionarioCommittente) -->
      <xsl:call-template name="Element_Provincia_CessionarioCommittente"/>

      <!-- Nazione (in CessionarioCommittente) -->
      <xsl:call-template name="Element_Nazione_CessionarioCommittente"/>

    </Sede>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- StabileOrganizzazione (in CessionarioCommittente) -->
  <xsl:template name="Block_StabileOrganizzazione_CessionarioCommittente">
    <xsl:if test="N_ID_NOMI_STBL_ORG_FTPA">
      <StabileOrganizzazione>
        <xsl:value-of select="$newline" />

        <!-- Indirizzo (in StabileOrganizzazione in CessionarioCommittente) -->
        <xsl:call-template name="Element_Indirizzo_StabileOrganizzazione_CessionarioCommittente"/>

        <!-- CAP (in StabileOrganizzazione in CessionarioCommittente) -->
        <xsl:call-template name="Element_CAP_StabileOrganizzazione_CessionarioCommittente"/>

        <!-- Comune (in StabileOrganizzazione in CessionarioCommittente) -->
        <xsl:call-template name="Element_Comune_StabileOrganizzazione_CessionarioCommittente"/>

        <!-- Provincia (in StabileOrganizzazione in CessionarioCommittente) -->
        <xsl:call-template name="Element_Provincia_StabileOrganizzazione_CessionarioCommittente"/>

        <!-- Nazione (in StabileOrganizzazione in CessionarioCommittente) -->
        <xsl:call-template name="Element_Nazione_StabileOrganizzazione_CessionarioCommittente"/>

      </StabileOrganizzazione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RappresentanteFiscale (in CessionarioCommittente) -->
  <xsl:template name="Block_RappresentanteFiscale_CessionarioCommittente">
    <xsl:if test="N_ID_NOMI_RAPPR_FISC_FTPA">
      <RappresentanteFiscale>
        <xsl:value-of select="$newline" />

        <!-- IdFiscaleIVA (in RappresentanteFiscale in CessionarioCommittente) -->
        <IdFiscaleIVA>
          <xsl:value-of select="$newline" />

          <!-- IdPaese (in RappresentanteFiscale CessionarioCommittente) -->
          <xsl:call-template name="Element_IdPaese_RappresentanteFiscale_CessionarioCommittente"/>

          <!-- IdCodice (in RappresentanteFiscale in CessionarioCommittente) -->
          <xsl:call-template name="Element_IdCodice_RappresentanteFiscale_CessionarioCommittente"/>

        </IdFiscaleIVA>
        <xsl:value-of select="$newline" />

        <!-- Scelta tra denominazione e nome/cognome -->
        <xsl:variable name="NaturaGiuridicaRapprFisc">
          <xsl:call-template name="Check_NaturaGiuridica">
            <xsl:with-param name="Sesso" select="C_SESSO_RAPPR_FISC_FTPA" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:choose>
          <xsl:when test="$NaturaGiuridicaRapprFisc = 1">

            <!-- Denominazione (in RappresentanteFiscale in CessionarioCommittente) -->
            <xsl:call-template name="Element_Denominazione_RappresentanteFiscale_CessionarioCommittente"/>

          </xsl:when>
          <xsl:otherwise>

            <!-- Nome (in RappresentanteFiscale in CessionarioCommittente) -->
            <xsl:call-template name="Element_Nome_RappresentanteFiscale_CessionarioCommittente"/>

            <!-- Cognome (in RappresentanteFiscale in CessionarioCommittente) -->
            <xsl:call-template name="Element_Cognome_RappresentanteFiscale_CessionarioCommittente"/>

          </xsl:otherwise>
        </xsl:choose>

      </RappresentanteFiscale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CessionarioCommittente -->
  <xsl:template name="Block_CessionarioCommittente">
    <CessionarioCommittente>
      <xsl:value-of select="$newline" />

      <!-- DatiAnagrafici (in CessionarioCommittente) -->
      <xsl:call-template name="Block_DatiAnagrafici_CessionarioCommittente"/>

      <!-- Sede (in CessionarioCommittente) -->
      <xsl:call-template name="Block_Sede_CessionarioCommittente"/>

      <!-- StabileOrganizzazione (in CessionarioCommittente) -->
      <xsl:call-template name="Block_StabileOrganizzazione_CessionarioCommittente"/>

      <!-- RappresentanteFiscale (in CessionarioCommittente) -->
      <xsl:call-template name="Block_RappresentanteFiscale_CessionarioCommittente"/>

    </CessionarioCommittente>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!--:::::::::::::::::: TerzoIntermediarioOSoggettoEmittente ::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- TerzoIntermediarioOSoggettoEmittente -->
  <xsl:template name="Block_TerzoIntermediarioOSoggettoEmittente">
    <xsl:variable name="IsCC">
      <xsl:call-template name="Check_EmessaCC"/>
    </xsl:variable>

    <xsl:if test="$IsCC = 1">
      <TerzoIntermediarioOSoggettoEmittente>
        
        <!-- DatiAnagrafici (in CessionarioCommittente) -->
        <xsl:call-template name="Block_DatiAnagrafici_CessionarioCommittente"/>
        
      </TerzoIntermediarioOSoggettoEmittente>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!--:::::::::::::::::: SoggettoEmittente ::::::::::::::::::-->

  <!-- Elementi -->

  <!-- SoggettoEmittente -->
  <xsl:template name="Element_SoggettoEmittente">
    <xsl:variable name="IsCC">
      <xsl:call-template name="Check_EmessaCC"/>
    </xsl:variable>
    
    <xsl:if test="$IsCC = 1">
      <SoggettoEmittente>
        <xsl:value-of select="'CC'" />
      </SoggettoEmittente>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!--::::::::::::::::::::::::::::::::::::::::: Body :::::::::::::::::::::::::::::::::::::::::-->


  <!--:::::::::::::::: FatturaElettronicaBody ::::::::::::::::-->

  <!-- Blocchi -->

  <!-- FatturaElettronicaBody -->
  <xsl:template name="Block_FatturaElettronicaBody">
    <xsl:value-of select="$newline" />
    <FatturaElettronicaBody>
      <xsl:value-of select="$newline" />

      <!-- DatiGenerali -->
      <xsl:call-template name="Block_DatiGenerali"/>

      <!-- DatiBeniServizi -->
      <xsl:call-template name="Block_DatiBeniServizi"/>

      <!-- DatiVeicoli -->
      <xsl:call-template name="Block_DatiVeicoli"/>

      <!-- DatiPagamento (in FatturaElettronicaBody) -->
      <xsl:call-template name="Block_DatiPagamento_Gloscad"/>

      <!-- Allegati -->
      <xsl:call-template name="Block_Allegati"/>

      <xsl:value-of select="$newline" />
    </FatturaElettronicaBody>
    <xsl:value-of select="$newline" />

  </xsl:template>


  <!--::::::::::::::::::::: DatiGenerali :::::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- RifAltriDoc (in DatiGenerali) -->
  <xsl:template name="Block_RifAltriDoc_DatiGenerali">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:if  test="N_TOT_NUM_RIGHE_RIF_ALTRI_DOC_FTPA > 0">
      <xsl:for-each select="following-sibling::QuantoDataRow[
                    N_ID_DITTA = $Ditta and
                    N_TIPO_DOCV_BASE = $TipoDocvBase and
                    N_ID_TP_DOCV = $TipoDocv and
                    C_ANNO_ESERCIZIO = $Esercizio and
                    N_ID_SEDE = $Sede and
                    N_NUM_DOCV = $NumDocv and                  
                    C_TIPO_RIGHE = 'A' and
                    N_SEL_RIF_ALTRI_DOC_FTPA != 6]">
        <xsl:sort select="N_SEL_RIF_ALTRI_DOC_FTPA"/>
        <xsl:call-template name="Block_RifAltriDoc"/>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>

  <!-- DatiSAL (in DatiGenerali) -->
  <xsl:template name="Block_DatiSAL_DatiGenerali">
    <!-- Non gestito -->
  </xsl:template>

  <!-- RifAltriDoc_DDT (in DatiGenerali) -->
  <xsl:template name="Block_RifAltriDoc_DDT_DatiGenerali">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:if  test="N_TOT_NUM_RIGHE_RIF_ALTRI_DOC_FTPA > 0">
      <xsl:for-each select="following-sibling::QuantoDataRow[
                    N_ID_DITTA = $Ditta and
                    N_TIPO_DOCV_BASE = $TipoDocvBase and
                    N_ID_TP_DOCV = $TipoDocv and
                    C_ANNO_ESERCIZIO = $Esercizio and
                    N_ID_SEDE = $Sede and
                    N_NUM_DOCV = $NumDocv and                  
                    C_TIPO_RIGHE = 'A' and
                    N_SEL_RIF_ALTRI_DOC_FTPA = 6]">
        <xsl:call-template name="Block_RifAltriDoc"/>
      </xsl:for-each>
    </xsl:if>

  </xsl:template>

  <!-- DatiDDT (in DatiGenerali) -->
  <xsl:template name="Block_DatiDDT_DatiGenerali">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:call-template name="Block_DatiDDT"/>

    <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'R']">

      <xsl:variable name="RDocv_Repeat">
        <xsl:call-template name="Check_RDocv_Repeat" />
      </xsl:variable>

      <xsl:if test="$RDocv_Repeat = 0">
        <xsl:call-template name="Block_DatiDDT" />
      </xsl:if>

    </xsl:for-each>

  </xsl:template>



  <!-- DatiTrasporto (in DatiGenerali) -->
  <xsl:template name="Block_DatiTrasporto_DatiGenerali">
    <xsl:if test="D_DATA_STAMPA">
      <DatiTrasporto>
        <xsl:value-of select="$newline" />

        <!-- DatiAnagraficiVettore -->
        <xsl:call-template name="Block_DatiAnagraficiVettore"/>

        <!-- MezzoTrasporto -->
        <xsl:call-template name="Element_MezzoTrasporto"/>

        <!-- CausaleTrasporto -->
        <xsl:call-template name="Element_CausaleTrasporto"/>

        <!-- NumeroColli -->
        <xsl:call-template name="Element_NumeroColli"/>

        <!-- Descrizione (in DatiTrasporto) -->
        <xsl:call-template name="Element_Descrizione_DatiTrasporto"/>

        <!-- UnitaMisuraPeso -->
        <xsl:call-template name="Element_UnitaMisuraPeso"/>

        <!-- PesoLordo -->
        <xsl:call-template name="Element_PesoLordo"/>

        <!-- PesoNetto -->
        <xsl:call-template name="Element_PesoNetto"/>

        <!-- DataOraRitiro -->
        <xsl:call-template name="Element_DataOraRitiro"/>

        <!-- DataInizioTrasporto -->
        <xsl:call-template name="Element_DataInizioTrasporto"/>

        <!-- TipoResa -->
        <xsl:call-template name="Element_TipoResa"/>

        <!-- IndirizzoResa -->
        <xsl:call-template name="Block_IndirizzoResa"/>

        <!-- DataOraConsegna -->
        <xsl:call-template name="Element_DataOraConsegna"/>

      </DatiTrasporto>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- FatturaPrincipale (in DatiGenerali) -->
  <xsl:template name="Block_FatturaPrincipale_DatiGenerali">
    <!-- Non gestito -->
  </xsl:template>

  <!-- DatiGenerali -->
  <xsl:template name="Block_DatiGenerali">
    <DatiGenerali>
      <xsl:value-of select="$newline" />

      <!-- DatiGeneraliDocumento -->
      <xsl:call-template name="Block_DatiGeneraliDocumento"/>

      <!-- Rif. Altri Documenti (in DatiGenerali) -->
      <xsl:call-template name="Block_RifAltriDoc_DatiGenerali">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <!-- DatiSAL (in DatiGenerali) -->
      <xsl:call-template name="Block_DatiSAL_DatiGenerali"/>
      
      <!-- RifAltriDoc_DDT (in DatiGenerali) -->
      <xsl:call-template name="Block_RifAltriDoc_DDT_DatiGenerali">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <!-- DatiDDT (in DatiGenerali) -->
      <xsl:call-template name="Block_DatiDDT_DatiGenerali">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <!-- DatiTrasporto (in DatiGenerali) -->
      <xsl:call-template name="Block_DatiTrasporto_DatiGenerali"/>

      <!-- FatturaPrincipale (in DatiGenerali) -->
      <xsl:call-template name="Block_FatturaPrincipale_DatiGenerali"/>

    </DatiGenerali>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!--:::::::::: DatiGenerali/DatiGeneraliDocumento ::::::::::-->

  <!-- Elementi -->

  <!-- TipoDocumento -->
  <xsl:template name="Element_TipoDocumento">
    <TipoDocumento>
      <xsl:value-of  select="C_ID_TP_DOC_FTPA"/>
    </TipoDocumento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Divisa -->
  <xsl:template name="Element_Divisa">
    <Divisa>
      <xsl:choose>
        <xsl:when test="C_ID_VALUTA">
          <xsl:variable name="IsConvVal">
            <xsl:call-template name="Check_ConvVal"/>
          </xsl:variable>
          <xsl:choose>
            <xsl:when test="$IsConvVal = 1">
              <xsl:value-of  select="C_COD_MB_ISO"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:choose>
                <xsl:when test="N_CAMBIO = 1">
                  <xsl:value-of  select="C_COD_VALUTA_ISO"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of  select="''"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select="C_COD_MB_ISO"/>
        </xsl:otherwise>
      </xsl:choose>
    </Divisa>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Data (in DatiGeneraliDocumento) -->
  <xsl:template name="Element_Data_DatiGeneraliDocumento">
    <Data>
      <xsl:call-template name="IS_formatDateNoTime">
        <xsl:with-param name="dateIn" select="D_DATA_DOCV" />
      </xsl:call-template>
    </Data>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Numero (in DatiGeneraliDocumento) -->
  <xsl:template name="Element_Numero_DatiGeneraliDocumento">
    <xsl:choose>
      <xsl:when test="N_FLAG_ANN_SOL = 1">
        <Numero>
          <xsl:value-of select='concat(format-number(N_NUM_DOCV_ANN_SOL, "#"), C_SUFFISSO_NUM_DOC)' />
        </Numero>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:otherwise>
        <Numero>
          <xsl:value-of select='concat(format-number(N_NUM_DOCV, "#"), C_SUFFISSO_NUM_DOC)' />
        </Numero>
        <xsl:value-of select="$newline" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- TipoRitenuta -->
  <xsl:template name="Element_TipoRitenuta">
    <TipoRitenuta>
      <xsl:value-of  select='C_TIPO_RTNT_V'/>
    </TipoRitenuta>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- ImportoRitenuta -->
  <xsl:template name="Element_ImportoRitenuta">
    <ImportoRitenuta>
      <xsl:value-of  select='format-number(N_IMPORTO_RTNT_V, "0.00#")'/>
    </ImportoRitenuta>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaRitenuta -->
  <xsl:template name="Element_AliquotaRitenuta">
    <AliquotaRitenuta>
      <xsl:value-of  select='format-number(N_ALIQUOTA_RTNT_V, "0.00")'/>
    </AliquotaRitenuta>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CausalePagamento -->
  <xsl:template name="Element_CausalePagamento">
    <CausalePagamento>
      <xsl:value-of  select='C_ID_CAUS_RTNT_V'/>
    </CausalePagamento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- BolloVirtuale -->
  <xsl:template name="Element_BolloVirtuale">
    <xsl:if test="N_FLAG_BOLLO_VRTL_FTPA != 0">
      <BolloVirtuale>
        <xsl:value-of  select="'SI'" />
      </BolloVirtuale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ImportoBollo -->
  <xsl:template name="Element_ImportoBollo">
    <xsl:if test="N_IMPORTO_BOLLO != 0">
      <ImportoBollo>
        <xsl:value-of  select='format-number(N_IMPORTO_BOLLO, "0.00#")'/>
      </ImportoBollo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- TipoCassa -->
  <xsl:template name="Element_TipoCassa">
    <TipoCassa>
      <xsl:value-of  select='C_COD_TP_CASSA_PRV_V'/>
    </TipoCassa>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AlCassa -->
  <xsl:template name="Element_AlCassa">
    <AlCassa>
      <xsl:value-of  select='format-number(N_ALIQUOTA_CASSA_PRV_V, "0.00")'/>
    </AlCassa>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- ImportoContributoCassa -->
  <xsl:template name="Element_ImportoContributoCassa">
    <ImportoContributoCassa>
      <xsl:value-of  select='format-number(N_IMPORTO_CASSA_PRV_V, "0.00#")'/>
    </ImportoContributoCassa>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- ImponibileCassa -->
  <xsl:template name="Element_ImponibileCassa">
    <!-- Non gestito -->
  </xsl:template>

  <!-- AliquotaIVA (in DatiCassaPrevidenziale) -->
  <xsl:template name="Element_AliquotaIVA_DatiCassaPrevidenziale">
    <AliquotaIVA>
      <xsl:value-of  select='format-number(N_ALIQUOTA_IVA_CASSA_PRV_V, "0.00")'/>
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ritenuta (in DatiCassaPrevidenziale) -->
  <xsl:template name="Element_Ritenuta_DatiCassaPrevidenziale">
    <xsl:call-template name="Element_Ritenuta_Generic">
      <xsl:with-param name="TotNumRigheRtnt" select="N_TOT_NUM_RIGHE_RTNT_V" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_CASSA_PRV_V" />
    </xsl:call-template>
  </xsl:template>

  <!-- Natura (in DatiCassaPrevidenziale) -->
  <xsl:template name="Element_Natura_DatiCassaPrevidenziale">
    <xsl:if test="C_ID_NATURA_NONIMP_FTPA_CASSA_PRV_V != ''">
      <Natura>
        <xsl:value-of  select="C_ID_NATURA_NONIMP_FTPA_CASSA_PRV_V"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoAmministrazione (in DatiCassaPrevidenziale) -->
  <xsl:template name="Element_RiferimentoAmministrazione_DatiCassaPrevidenziale">
    <xsl:if test="C_RIF_AMMNSTRZN_FTPA_CASSA_PRV_V">
      <RiferimentoAmministrazione>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="C_RIF_AMMNSTRZN_FTPA_CASSA_PRV_V" />
        </xsl:call-template>
      </RiferimentoAmministrazione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Tipo (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Tipo_ScontoMaggiorazione">    
    <Tipo>
      <xsl:choose>
        <xsl:when test="N_IMPORTO_SCONTO_T_FTPA = 0">
          <xsl:choose>
            <xsl:when test="N_P_SCONTO_T_FTPA &lt; 0">
              <xsl:value-of  select="'MG'"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of  select="'SC'"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:when test="N_IMPORTO_SCONTO_T_FTPA &lt; 0">
          <xsl:value-of  select="'MG'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select="'SC'"/>
        </xsl:otherwise>
      </xsl:choose>
    </Tipo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Percentuale (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Percentuale_ScontoMaggiorazione">
    <xsl:if test="N_P_SCONTO_T_FTPA != 0">
      <Percentuale>
        <xsl:choose>
          <xsl:when test="N_P_SCONTO_T_FTPA &lt; 0">
            <xsl:value-of  select='format-number(-N_P_SCONTO_T_FTPA, "0.00#")'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of  select='format-number(N_P_SCONTO_T_FTPA, "0.00#")'/>
          </xsl:otherwise>
        </xsl:choose>
      </Percentuale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>
  
  <!-- Importo (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Importo_ScontoMaggiorazione">
    <xsl:if test="N_IMPORTO_SCONTO_T_FTPA != 0">
      <Importo>
        <xsl:choose>
          <xsl:when test="N_IMPORTO_SCONTO_T_FTPA &lt; 0">
            <xsl:value-of  select='format-number(-N_IMPORTO_SCONTO_T_FTPA, "0.00######")'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of  select='format-number(N_IMPORTO_SCONTO_T_FTPA, "0.00######")'/>
          </xsl:otherwise>
        </xsl:choose>
      </Importo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ImportoTotaleDocumento -->
  <xsl:template name="Element_ImportoTotaleDocumento">
    <xsl:choose>
      <xsl:when test="N_CAMBIO = 1">
        <ImportoTotaleDocumento>
          <xsl:value-of  select='format-number(N_TOT_IMPONIBILE_MB + N_TOT_IMPOSTA_MB, "0.00#")' />
        </ImportoTotaleDocumento>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of  select="''"/>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <!-- Arrotondamento (in DatiGeneraliDocumento) -->
  <xsl:template name="Element_Arrotondamento_DatiGeneraliDocumento"/>
  <!-- Non gestito -->

  <!-- Causale (in DatiGeneraliDocumento) -->
  <xsl:template name="Element_Causale_DatiGeneraliDocumento">
    <xsl:if test="C_DITTA_DES_CAUS_FTPA">
      <Causale>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="C_DITTA_DES_CAUS_FTPA" />
        </xsl:call-template>
      </Causale>
      <xsl:value-of select="$newline" />
    </xsl:if>
    <xsl:if test="C_DES_CAUS_FTPA">
      <Causale>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="C_DES_CAUS_FTPA" />
        </xsl:call-template>
      </Causale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Art73 -->
  <xsl:template name="Element_Art73">
    <xsl:if test="N_FLAG_ART73_FTPA = 1">
      <Art73>
        <xsl:call-template name="IS_convertBooleanNumberToYesNoString">
          <xsl:with-param name="numberIn" select="N_FLAG_ART73_FTPA" />
        </xsl:call-template>
      </Art73>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DatiRitenuta -->
  <xsl:template name="Block_DatiRitenuta">
    <DatiRitenuta>
      <xsl:value-of select="$newline" />

      <!-- TipoRitenuta -->
      <xsl:call-template name="Element_TipoRitenuta"/>

      <!-- ImportoRitenuta -->
      <xsl:call-template name="Element_ImportoRitenuta"/>

      <!-- AliquotaRitenuta -->
      <xsl:call-template name="Element_AliquotaRitenuta"/>

      <!-- CausalePagamento -->
      <xsl:call-template name="Element_CausalePagamento"/>

    </DatiRitenuta>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- DatiRitenuta (in DatiGeneraliDocumento) -->
  <xsl:template name="Block_DatiRitenuta_DatiGeneraliDocumento">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:if test="N_TOT_NUM_RIGHE_RTNT_V > 0">
      <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'N']">
        <xsl:call-template name="Block_DatiRitenuta"/>
      </xsl:for-each>
    </xsl:if>

  </xsl:template>

  <!-- DatiCassaPrevidenziale -->
  <xsl:template name="Block_DatiCassaPrevidenziale">
    <DatiCassaPrevidenziale>
      <xsl:value-of select="$newline" />

      <!-- TipoCassa -->
      <xsl:call-template name="Element_TipoCassa"/>

      <!-- AlCassa -->
      <xsl:call-template name="Element_AlCassa"/>

      <!-- ImportoContributoCassa -->
      <xsl:call-template name="Element_ImportoContributoCassa"/>

      <!-- ImponibileCassa -->
      <xsl:call-template name="Element_ImponibileCassa"/>

      <!-- AliquotaIVA (in DatiCassaPrevidenziale) -->
      <xsl:call-template name="Element_AliquotaIVA_DatiCassaPrevidenziale"/>

      <!-- Ritenuta (in DatiCassaPrevidenziale) -->
      <xsl:call-template name="Element_Ritenuta_DatiCassaPrevidenziale"/>

      <!-- Natura (in DatiCassaPrevidenziale) -->
      <xsl:call-template name="Element_Natura_DatiCassaPrevidenziale"/>

      <!-- RiferimentoAmministrazione (in DatiCassaPrevidenziale) -->
      <xsl:call-template name="Element_RiferimentoAmministrazione_DatiCassaPrevidenziale"/>

    </DatiCassaPrevidenziale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- DatiCassaPrevidenziale (in DatiGeneraliDocumento) -->
  <xsl:template name="Block_DatiCassaPrevidenziale_DatiGeneraliDocumento">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:if test="N_TOT_NUM_RIGHE_CASSA_PRV_V > 0">
      <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'C']">
        <xsl:call-template name="Block_DatiCassaPrevidenziale"/>
      </xsl:for-each>
    </xsl:if>

  </xsl:template>

  <!-- ScontoMaggiorazione (in DatiGeneraliDocumento) -->
  <xsl:template name="Block_ScontoMaggiorazione_DatiGeneraliDocumento">
    <xsl:if test="N_P_SCONTO_T_FTPA != 0 or N_IMPORTO_SCONTO_T_FTPA != 0">

      <!-- ScontoMaggiorazione -->
      <ScontoMaggiorazione>
        <xsl:value-of select="$newline" />

        <!-- Tipo (in ScontoMaggiorazione) -->
        <xsl:call-template name="Element_Tipo_ScontoMaggiorazione"/>

        <!-- Percentuale (in ScontoMaggiorazione) -->
        <xsl:call-template name="Element_Percentuale_ScontoMaggiorazione"/>

        <!-- Importo (in ScontoMaggiorazione) -->
        <xsl:call-template name="Element_Importo_ScontoMaggiorazione"/>

      </ScontoMaggiorazione>
      <xsl:value-of select="$newline" />

    </xsl:if>
  </xsl:template>

  <!-- DatiGeneraliDocumento -->
  <xsl:template name="Block_DatiGeneraliDocumento">
    <DatiGeneraliDocumento>
      <xsl:value-of select="$newline" />

      <!-- TipoDocumento -->
      <xsl:call-template name="Element_TipoDocumento"/>

      <!-- Divisa -->
      <xsl:call-template name="Element_Divisa"/>

      <!-- Data (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Element_Data_DatiGeneraliDocumento"/>

      <!-- Numero (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Element_Numero_DatiGeneraliDocumento"/>

      <!-- DatiRitenuta (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Block_DatiRitenuta_DatiGeneraliDocumento">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <xsl:if test="N_FLAG_BOLLO_VRTL_FTPA != 0 or
                    N_IMPORTO_BOLLO > 0">

        <!-- DatiBollo -->
        <DatiBollo>
          <xsl:value-of select="$newline" />

          <!-- BolloVirtuale -->
          <xsl:call-template name="Element_BolloVirtuale"/>

          <!-- ImportoBollo -->
          <xsl:call-template name="Element_ImportoBollo"/>

        </DatiBollo>
        <xsl:value-of select="$newline" />

      </xsl:if>

      <!-- DatiCassaPrevidenziale (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Block_DatiCassaPrevidenziale_DatiGeneraliDocumento">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <!-- ScontoMaggiorazione (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Block_ScontoMaggiorazione_DatiGeneraliDocumento"/>

      <!-- ImportoTotaleDocumento -->
      <xsl:call-template name="Element_ImportoTotaleDocumento"/>

      <!-- Arrotondamento (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Element_Arrotondamento_DatiGeneraliDocumento"/>
      <!-- Non gestito -->

      <!-- Causale (in DatiGeneraliDocumento) -->
      <xsl:call-template name="Element_Causale_DatiGeneraliDocumento"/>

      <!-- Art73 -->
      <xsl:call-template name="Element_Art73"/>

    </DatiGeneraliDocumento>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!--
  ::::::::::::::::: DatiGenerali/DatiOrdineAcquisto :::::::::::::::::
  ::::::::::::::::: DatiGenerali/DatiContratto :::::::::::::::::
  ::::::::::::::::: DatiGenerali/DatiConvenzione :::::::::::::::::
  ::::::::::::::::: DatiGenerali/DatiRicezione :::::::::::::::::
  ::::::::::::::::: DatiGenerali/DatiFattureCollegate :::::::::::::::::
  -->

  <!-- Elementi valorizzati -->

  <!-- RiferimentoNumeroLinea -->
  <xsl:template name="Element_RiferimentoNumeroLinea_RifAltriDoc">
    <xsl:if test="N_NUM_LINEA_RIF_ALTRI_DOC_FTPA">
      <RiferimentoNumeroLinea>
        <xsl:value-of  select='format-number(N_NUM_LINEA_RIF_ALTRI_DOC_FTPA, "#")'/>
      </RiferimentoNumeroLinea>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- IdDocumento -->
  <xsl:template name="Element_IdDocumento_RifAltriDoc">
    <xsl:if test="C_NUM_DOC_RIF_ALTRI_DOC_FTPA">
      <IdDocumento>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="C_NUM_DOC_RIF_ALTRI_DOC_FTPA" />
        </xsl:call-template>
      </IdDocumento>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Data -->
  <xsl:template name="Element_Data_RifAltriDoc">
    <xsl:if test="D_DATA_RIF_ALTRI_DOC_FTPA">
      <Data>
        <xsl:call-template name="IS_formatDateNoTime">
          <xsl:with-param name="dateIn" select="D_DATA_RIF_ALTRI_DOC_FTPA" />
        </xsl:call-template>
      </Data>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- NumItem -->
  <xsl:template name="Element_NumItem_RifAltriDoc">
    <xsl:if test="C_NUM_ITEM_RIF_ALTRI_DOC_FTPA">
      <NumItem>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="C_NUM_ITEM_RIF_ALTRI_DOC_FTPA" />
        </xsl:call-template>
      </NumItem>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CodiceCommessaConvenzione -->
  <xsl:template name="Element_CodiceCommessaConvenzione_RifAltriDoc">
    <xsl:if test="C_COD_CMSA_RIF_ALTRI_DOC_FTPA">
      <CodiceCommessaConvenzione>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="C_COD_CMSA_RIF_ALTRI_DOC_FTPA" />
        </xsl:call-template>
      </CodiceCommessaConvenzione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CodiceCUP -->
  <xsl:template name="Element_CodiceCUP_RifAltriDoc">
    <xsl:if test="C_CUP_RIF_ALTRI_DOC_FTPA">
      <CodiceCUP>
        <xsl:value-of  select='C_CUP_RIF_ALTRI_DOC_FTPA'/>
      </CodiceCUP>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CodiceCIG -->
  <xsl:template name="Element_CodiceCIG_RifAltriDoc">
    <xsl:if test="C_CIG_RIF_ALTRI_DOC_FTPA">
      <CodiceCIG>
        <xsl:value-of  select='C_CIG_RIF_ALTRI_DOC_FTPA'/>
      </CodiceCIG>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- RifAltriDoc
  
  DatiOrdineAcquisto
  DatiContratto
  DatiConvenzione
  DatiRicezione
  DatiFattureCollegate 
  
  -->

  <xsl:template name="Block_RifAltriDoc">

    <xsl:choose>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 1">
        <DatiOrdineAcquisto>
          <xsl:value-of select="$newline" />

          <!-- RiferimentoNumeroLinea -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_RifAltriDoc"/>

          <!-- IdDocumento -->
          <xsl:call-template name="Element_IdDocumento_RifAltriDoc"/>

          <!-- Data -->
          <xsl:call-template name="Element_Data_RifAltriDoc"/>

          <!-- NumItem -->
          <xsl:call-template name="Element_NumItem_RifAltriDoc"/>

          <!-- CodiceCommessaConvenzione -->
          <xsl:call-template name="Element_CodiceCommessaConvenzione_RifAltriDoc"/>

          <!-- CodiceCUP -->
          <xsl:call-template name="Element_CodiceCUP_RifAltriDoc"/>

          <!-- CodiceCIG -->
          <xsl:call-template name="Element_CodiceCIG_RifAltriDoc"/>

        </DatiOrdineAcquisto>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 2">
        <DatiContratto>
          <xsl:value-of select="$newline" />

          <!-- RiferimentoNumeroLinea -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_RifAltriDoc"/>

          <!-- IdDocumento -->
          <xsl:call-template name="Element_IdDocumento_RifAltriDoc"/>

          <!-- Data -->
          <xsl:call-template name="Element_Data_RifAltriDoc"/>

          <!-- NumItem -->
          <xsl:call-template name="Element_NumItem_RifAltriDoc"/>

          <!-- CodiceCommessaConvenzione -->
          <xsl:call-template name="Element_CodiceCommessaConvenzione_RifAltriDoc"/>

          <!-- CodiceCUP -->
          <xsl:call-template name="Element_CodiceCUP_RifAltriDoc"/>

          <!-- CodiceCIG -->
          <xsl:call-template name="Element_CodiceCIG_RifAltriDoc"/>

        </DatiContratto>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 3">
        <DatiConvenzione>
          <xsl:value-of select="$newline" />

          <!-- RiferimentoNumeroLinea -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_RifAltriDoc"/>

          <!-- IdDocumento -->
          <xsl:call-template name="Element_IdDocumento_RifAltriDoc"/>

          <!-- Data -->
          <xsl:call-template name="Element_Data_RifAltriDoc"/>

          <!-- NumItem -->
          <xsl:call-template name="Element_NumItem_RifAltriDoc"/>

          <!-- CodiceCommessaConvenzione -->
          <xsl:call-template name="Element_CodiceCommessaConvenzione_RifAltriDoc"/>

          <!-- CodiceCUP -->
          <xsl:call-template name="Element_CodiceCUP_RifAltriDoc"/>

          <!-- CodiceCIG -->
          <xsl:call-template name="Element_CodiceCIG_RifAltriDoc"/>

        </DatiConvenzione>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 4">
        <DatiRicezione>
          <xsl:value-of select="$newline" />

          <!-- RiferimentoNumeroLinea -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_RifAltriDoc"/>

          <!-- IdDocumento -->
          <xsl:call-template name="Element_IdDocumento_RifAltriDoc"/>

          <!-- Data -->
          <xsl:call-template name="Element_Data_RifAltriDoc"/>

          <!-- NumItem -->
          <xsl:call-template name="Element_NumItem_RifAltriDoc"/>

          <!-- CodiceCommessaConvenzione -->
          <xsl:call-template name="Element_CodiceCommessaConvenzione_RifAltriDoc"/>

          <!-- CodiceCUP -->
          <xsl:call-template name="Element_CodiceCUP_RifAltriDoc"/>

          <!-- CodiceCIG -->
          <xsl:call-template name="Element_CodiceCIG_RifAltriDoc"/>

        </DatiRicezione>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 5">
        <DatiFattureCollegate>
          <xsl:value-of select="$newline" />

          <!-- RiferimentoNumeroLinea -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_RifAltriDoc"/>

          <!-- IdDocumento -->
          <xsl:call-template name="Element_IdDocumento_RifAltriDoc"/>

          <!-- Data -->
          <xsl:call-template name="Element_Data_RifAltriDoc"/>

          <!-- NumItem -->
          <xsl:call-template name="Element_NumItem_RifAltriDoc"/>

          <!-- CodiceCommessaConvenzione -->
          <xsl:call-template name="Element_CodiceCommessaConvenzione_RifAltriDoc"/>

          <!-- CodiceCUP -->
          <xsl:call-template name="Element_CodiceCUP_RifAltriDoc"/>

          <!-- CodiceCIG -->
          <xsl:call-template name="Element_CodiceCIG_RifAltriDoc"/>

        </DatiFattureCollegate>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:when test="N_SEL_RIF_ALTRI_DOC_FTPA = 6">
        <DatiDDT>
          <xsl:value-of select="$newline" />

          <!-- NumeroDDT -->
          <xsl:call-template name="Element_NumeroDDT">
            <xsl:with-param name="IsRifAltriDoc" select="1" />
          </xsl:call-template>

          <!-- DataDDT -->
          <xsl:call-template name="Element_DataDDT">
            <xsl:with-param name="IsRifAltriDoc" select="1" />
          </xsl:call-template>

          <!-- RiferimentoNumeroLinea (in DatiDDT) -->
          <xsl:call-template name="Element_RiferimentoNumeroLinea_DatiDDT">
            <xsl:with-param name="IsRifAltriDoc" select="1" />
          </xsl:call-template>

        </DatiDDT>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:otherwise></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!--::::::::::::::::: DatiGenerali/DatiSAL :::::::::::::::::-->
  <!-- Non gestito -->

  <!--::::::::::::::::: DatiGenerali/DatiDDT :::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- NumeroDDT -->
  <xsl:template name="Element_NumeroDDT">
    <xsl:param name="IsRifAltriDoc" />

    <xsl:choose>
      <xsl:when test="$IsRifAltriDoc = 1">
        <NumeroDDT>
          <xsl:call-template name="IS_convertInvalidBasicLatin">
            <xsl:with-param name="stringIn" select="C_NUM_DOC_RIF_ALTRI_DOC_FTPA" />
          </xsl:call-template>
        </NumeroDDT>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="N_FLAG_ANN_SOL_P = 1">
            <NumeroDDT>
              <xsl:value-of select='concat(format-number(N_NUM_DOCV_ANN_SOL_P, "#"), C_SUFFISSO_NUM_DOC_P)' />
            </NumeroDDT>
            <xsl:value-of select="$newline" />
          </xsl:when>
          <xsl:otherwise>
            <NumeroDDT>
              <xsl:value-of select='concat(format-number(N_NUM_DOCV_P, "#"), C_SUFFISSO_NUM_DOC_P)' />
            </NumeroDDT>
            <xsl:value-of select="$newline" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
    
    
  </xsl:template>

  <!-- DataDDT -->
  <xsl:template name="Element_DataDDT">
    <xsl:param name="IsRifAltriDoc" />

    <xsl:choose>
      <xsl:when test="$IsRifAltriDoc = 1">
        <DataDDT>
          <xsl:call-template name="IS_formatDateNoTime">
            <xsl:with-param name="dateIn" select="D_DATA_RIF_ALTRI_DOC_FTPA" />
          </xsl:call-template>
        </DataDDT>
        <xsl:value-of select="$newline" />
      </xsl:when>
      <xsl:otherwise>
        <DataDDT>
          <xsl:call-template name="IS_formatDateNoTime">
            <xsl:with-param name="dateIn" select="D_DATA_DOCV_P" />
          </xsl:call-template>
        </DataDDT>
        <xsl:value-of select="$newline" />
      </xsl:otherwise>
    </xsl:choose>   
    
  </xsl:template>

  <!-- RiferimentoNumeroLinea (in DatiDDT) -->
  <xsl:template name="Element_RiferimentoNumeroLinea_DatiDDT">
    <xsl:param name="IsRifAltriDoc" />

    <xsl:choose>
      <xsl:when test="$IsRifAltriDoc = 1">
        <xsl:if test="N_NUM_LINEA_RIF_ALTRI_DOC_FTPA">
          <RiferimentoNumeroLinea>
            <xsl:value-of  select='format-number(N_NUM_LINEA_RIF_ALTRI_DOC_FTPA, "#")'/>
          </RiferimentoNumeroLinea>
          <xsl:value-of select="$newline" />
        </xsl:if>
      </xsl:when>
      <xsl:otherwise>
        <RiferimentoNumeroLinea>
          <xsl:value-of  select='format-number(N_PROGR_R_DOCV, "#")'/>
        </RiferimentoNumeroLinea>
        <xsl:value-of select="$newline" />
      </xsl:otherwise>
    </xsl:choose>
    
  </xsl:template>


  <!-- Blocchi -->

  <!-- DatiDDT -->
  <xsl:template name="Block_DatiDDT">

    <xsl:if test="
            N_TIPO_DOCV_BASE_P = 3 and
            N_TIPO_R_DOCV != 1 and
            N_FLAG_NO_RIF_DDT_IN_FTPA_P != 1">

      <DatiDDT>
        <xsl:value-of select="$newline" />

        <!-- NumeroDDT -->
        <xsl:call-template name="Element_NumeroDDT">
          <xsl:with-param name="IsRifAltriDoc" select="0" />
        </xsl:call-template>

        <!-- DataDDT -->
        <xsl:call-template name="Element_DataDDT">
          <xsl:with-param name="IsRifAltriDoc" select="0" />
        </xsl:call-template>

        <!-- RiferimentoNumeroLinea (in DatiDDT) -->
        <xsl:call-template name="Element_RiferimentoNumeroLinea_DatiDDT">
          <xsl:with-param name="IsRifAltriDoc" select="0" />
        </xsl:call-template>

      </DatiDDT>
      <xsl:value-of select="$newline" />

    </xsl:if>

  </xsl:template>

  <!--::::::::::::::::: DatiGenerali/DatiTrasporto :::::::::::::::::-->

  <!-- Elementi -->

  <!-- IdPaese (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_IdPaese_DatiAnagraficiVettore">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_PIVA_V1" />
      </xsl:call-template>
    </xsl:variable>
    <IdPaese>
      <xsl:value-of select="$IdNazione" />
    </IdPaese>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- IdCodice (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_IdCodice_DatiAnagraficiVettore">
    <IdCodice>
      <xsl:value-of select="normalize-space(C_PIVA_V1)" />
    </IdCodice>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceFiscale (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_CodiceFiscale_DatiAnagraficiVettore">
    <xsl:if test="normalize-space(C_CODFISC_V1) != ''">
      <CodiceFiscale>
        <xsl:value-of select="normalize-space(C_CODFISC_V1)" />
      </CodiceFiscale>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Denominazione (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_Denominazione_DatiAnagraficiVettore">
    <Denominazione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_V1, 1, 80)" />
      </xsl:call-template>
    </Denominazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Nome (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_Nome_DatiAnagraficiVettore">
    <xsl:if test="C_RAGSOC2_V1">
      <Nome>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="substring(C_RAGSOC2_V1, 1, 60)" />
        </xsl:call-template>
      </Nome>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Cognome (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_Cognome_DatiAnagraficiVettore">
    <Cognome>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_RAGSOC1_V1, 1, 60)" />
      </xsl:call-template>
    </Cognome>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- NumeroLicenzaGuida (in DatiAnagraficiVettore) -->
  <xsl:template name="Element_NumeroLicenzaGuida_DatiAnagraficiVettore">
    <!-- Non gestito -->
  </xsl:template>

  <!-- MezzoTrasporto -->
  <xsl:template name="Element_MezzoTrasporto">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CausaleTrasporto -->
  <xsl:template name="Element_CausaleTrasporto">
    <!-- Non gestito -->
  </xsl:template>

  <!-- NumeroColli -->
  <xsl:template name="Element_NumeroColli">
    <xsl:if test="N_NUMERO_COLLI > 0">
      <NumeroColli>
        <xsl:value-of select='format-number(N_NUMERO_COLLI, "#")' />
      </NumeroColli>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Descrizione (in DatiTrasporto) -->
  <xsl:template name="Element_Descrizione_DatiTrasporto">
    <xsl:if test="N_ID_ASP_MERCE">
      <Descrizione>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="C_DES_ASP_MERCE" />
        </xsl:call-template>
      </Descrizione>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- UnitaMisuraPeso -->
  <xsl:template name="Element_UnitaMisuraPeso">
    <xsl:if test="N_PESO_LORDO_KG > 0 or N_PESO_NETTO_KG > 0">
      <UnitaMisuraPeso>
        <xsl:value-of select="'kg'" />
      </UnitaMisuraPeso>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- PesoLordo -->
  <xsl:template name="Element_PesoLordo">
    <xsl:if test="N_PESO_LORDO_KG > 0">
      <PesoLordo>
        <xsl:value-of select='format-number(N_PESO_LORDO_KG, "0.00")' />
      </PesoLordo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- PesoNetto -->
  <xsl:template name="Element_PesoNetto">
    <xsl:if test="N_PESO_NETTO_KG > 0">
      <PesoNetto>
        <xsl:value-of select='format-number(N_PESO_NETTO_KG, "0.00")' />
      </PesoNetto>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- DataOraRitiro -->
  <xsl:template name="Element_DataOraRitiro">
    <xsl:if test="D_DATA_STAMPA">
      <DataOraRitiro>
        <xsl:value-of select="D_DATA_STAMPA" />
      </DataOraRitiro>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- DataInizioTrasporto -->
  <xsl:template name="Element_DataInizioTrasporto">
    <xsl:if test="D_DATA_STAMPA">
      <DataInizioTrasporto>
        <xsl:call-template name="IS_formatDateNoTime">
          <xsl:with-param name="dateIn" select="D_DATA_STAMPA" />
        </xsl:call-template>
      </DataInizioTrasporto>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- TipoResa -->
  <xsl:template name="Element_TipoResa">
    <xsl:if test="C_COD_INCOTERM_MOD_RESA">
      <TipoResa>
        <xsl:value-of select="C_COD_INCOTERM_MOD_RESA" />
      </TipoResa>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Indirizzo (in IndirizzoResa) -->
  <xsl:template name="Element_Indirizzo_IndirizzoResa">
    <Indirizzo>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_INDIR_DC, 1, 60)" />
      </xsl:call-template>
    </Indirizzo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CAP (in IndirizzoResa) -->
  <xsl:template name="Element_CAP_IndirizzoResa">
    <CAP>
      <xsl:value-of select="normalize-space(C_CAP_DC)" />
    </CAP>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Comune (in IndirizzoResa) -->
  <xsl:template name="Element_Comune_IndirizzoResa">
    <Comune>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_CITTA_DC, 1, 60)" />
      </xsl:call-template>
    </Comune>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Provincia (in IndirizzoResa) -->
  <xsl:template name="Element_Provincia_IndirizzoResa">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_DC" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="IsNazioneItalia">
      <xsl:call-template name="Check_NazioneItalia">
        <xsl:with-param name="IdNazione" select="$IdNazione" />
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="$IsNazioneItalia = 1">
      <xsl:if test="normalize-space(C_ID_PROVINCIA_DC) != ''">
        <Provincia>
          <xsl:value-of select="normalize-space(C_ID_PROVINCIA_DC)" />
        </Provincia>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- Nazione (in IndirizzoResa) -->
  <xsl:template name="Element_Nazione_IndirizzoResa">
    <xsl:variable name="IdNazione">
      <xsl:call-template name="Value_Nazione">
        <xsl:with-param name="IdNazione" select="C_ID_NAZIONE_DC" />
      </xsl:call-template>
    </xsl:variable>
    <Nazione>
      <xsl:value-of select="$IdNazione" />
    </Nazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- DataOraConsegna -->
  <xsl:template name="Element_DataOraConsegna">
    <!-- Non gestito -->
  </xsl:template>

  <!-- Blocchi -->

  <!-- DatiAnagraficiVettore -->
  <xsl:template name="Block_DatiAnagraficiVettore">
    <xsl:if test="N_ID_VETTORE_1">
      <DatiAnagraficiVettore>
        <xsl:value-of select="$newline" />

        <!-- IdFiscaleIVA (in DatiAnagraficiVettore) -->
        <IdFiscaleIVA>
          <xsl:value-of select="$newline" />

          <!-- IdPaese (in DatiAnagraficiVettore) -->
          <xsl:call-template name="Element_IdPaese_DatiAnagraficiVettore"/>

          <!-- IdCodice (in DatiAnagraficiVettore) -->
          <xsl:call-template name="Element_IdCodice_DatiAnagraficiVettore"/>

        </IdFiscaleIVA>
        <xsl:value-of select="$newline" />

        <!-- CodiceFiscale (in DatiAnagraficiVettore) -->
        <xsl:call-template name="Element_CodiceFiscale_DatiAnagraficiVettore"/>

        <!-- Anagrafica (in DatiAnagraficiVettore) -->
        <Anagrafica>
          <xsl:value-of select="$newline" />

          <!-- Scelta tra denominazione e nome/cognome -->
          <xsl:variable name="NaturaGiuridica">
            <xsl:call-template name="Check_NaturaGiuridica">
              <xsl:with-param name="Sesso" select="C_SESSO_V1" />
            </xsl:call-template>
          </xsl:variable>

          <xsl:choose>
            <xsl:when test="$NaturaGiuridica = 1">

              <!-- Denominazione (in DatiAnagraficiVettore) -->
              <xsl:call-template name="Element_Denominazione_DatiAnagraficiVettore"/>

            </xsl:when>
            <xsl:otherwise>

              <!-- Nome (in DatiAnagraficiVettore) -->
              <xsl:call-template name="Element_Nome_DatiAnagraficiVettore"/>

              <!-- Cognome (in DatiAnagraficiVettore) -->
              <xsl:call-template name="Element_Cognome_DatiAnagraficiVettore"/>

            </xsl:otherwise>
          </xsl:choose>

        </Anagrafica>
        <xsl:value-of select="$newline" />

        <!-- NumeroLicenzaGuida (in DatiAnagraficiVettore) -->
        <xsl:call-template name="Element_NumeroLicenzaGuida_DatiAnagraficiVettore"/>

      </DatiAnagraficiVettore>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>

  <!-- IndirizzoResa -->
  <xsl:template name="Block_IndirizzoResa">
    <xsl:if test="C_RAGSOC1_DC">
      <IndirizzoResa>
        <xsl:value-of select="$newline" />

        <!-- Indirizzo (in IndirizzoResa) -->
        <xsl:call-template name="Element_Indirizzo_IndirizzoResa"/>

        <!-- CAP (in IndirizzoResa) -->
        <xsl:call-template name="Element_CAP_IndirizzoResa"/>

        <!-- Comune (in IndirizzoResa) -->
        <xsl:call-template name="Element_Comune_IndirizzoResa"/>

        <!-- Provincia (in IndirizzoResa) -->
        <xsl:call-template name="Element_Provincia_IndirizzoResa"/>

        <!-- Nazione (in IndirizzoResa) -->
        <xsl:call-template name="Element_Nazione_IndirizzoResa"/>

        <xsl:value-of select="$newline" />
      </IndirizzoResa>
      <xsl:value-of select="$newline" />

    </xsl:if>

  </xsl:template>




  <!--::::::::::::::::: DatiGenerali/FatturaPrincipale :::::::::::::::::-->
  <!-- Non gestito -->



  <!--::::::::::::::::::: DatiBeniServizi :::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- DettaglioLinee da righe documento (in DatiBeniServizi) -->
  <xsl:template name="Block_DettaglioLinee_RDocv_DatiBeniServizi">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:call-template name="Block_DettaglioLinee_RDocv"/>

    <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'R']">

      <xsl:variable name="RDocv_Repeat">
        <xsl:call-template name="Check_RDocv_Repeat" />
      </xsl:variable>

      <xsl:if test="$RDocv_Repeat = 0">
        <xsl:call-template name="Block_DettaglioLinee_RDocv" />
      </xsl:if>

    </xsl:for-each>

  </xsl:template>

  <!-- DettaglioLinee da rettifiche IVA agevolata (in DatiBeniServizi) -->
  <xsl:template name="Block_DettaglioLinee_RettificheIvaAg_DatiBeniServizi">
    <xsl:param name="NumeroLinea_Start" />

    <xsl:variable name="AddFirst">
      <xsl:choose>
        <xsl:when test="C_TIPO_RIGHE = 'G'">
          <xsl:value-of select="'1'" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'0'" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:if test="$AddFirst = 1">
      <xsl:call-template name="Block_DettaglioLinee_RettificheIvaAg">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + $AddFirst" />
      </xsl:call-template>
    </xsl:if>

    <xsl:for-each select="following-sibling::QuantoDataRow[   
                  C_TIPO_RIGHE = 'G']">
      <xsl:call-template name="Block_DettaglioLinee_RettificheIvaAg">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + $AddFirst + position()" />
      </xsl:call-template>
    </xsl:for-each>

  </xsl:template>

  <!-- DettaglioLinee da contributi (in DatiBeniServizi) -->
  <xsl:template name="Block_DettaglioLinee_Contrib_DatiBeniServizi">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />
    <xsl:param name="NumeroLinea_Start" />

    <xsl:variable name="AddFirst">
      <xsl:call-template name="Check_ContribInFatt" />
    </xsl:variable>

    <xsl:if test="$AddFirst = 1">
      <xsl:call-template name="Block_DettaglioLinee_Contrib">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + $AddFirst" />
      </xsl:call-template>
    </xsl:if>

    <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'R' and
                  C_DES_CONTRIB != '' and
                  N_FLAG_CONTRIB_IN_R_DOCV = 0]">

      <xsl:call-template name="Block_DettaglioLinee_Contrib">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + $AddFirst + position()" />
      </xsl:call-template>

    </xsl:for-each>

  </xsl:template>

  <!-- Numero righe contributi (in DatiBeniServizi) -->
  <xsl:template name="Count_DettaglioLinee_Contrib_DatiBeniServizi">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:variable name="AddFirst">
      <xsl:call-template name="Check_ContribInFatt" />
    </xsl:variable>

    <xsl:value-of select="count(
                  following-sibling::QuantoDataRow[
                  N_ID_DITTA = $Ditta and
                  N_TIPO_DOCV_BASE = $TipoDocvBase and
                  N_ID_TP_DOCV = $TipoDocv and
                  C_ANNO_ESERCIZIO = $Esercizio and
                  N_ID_SEDE = $Sede and
                  N_NUM_DOCV = $NumDocv and                  
                  C_TIPO_RIGHE = 'R' and
                  C_DES_CONTRIB != '' and
                  N_FLAG_CONTRIB_IN_R_DOCV = 0] ) + $AddFirst"/>

  </xsl:template>

  <!-- DettaglioLinee da spese accessorie (in DatiBeniServizi) -->
  <xsl:template name="Block_DettaglioLinee_SpAcc_DatiBeniServizi">
    <xsl:param name="NumeroLinea_Start" />

    <!-- DettaglioLinee - Spesa accessoria n.1 -->
    <xsl:call-template name="Block_DettaglioLinee_SpAcc">
      <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + 1" />
      <xsl:with-param name="Descrizione" select="C_DES_SP_ACC_DOCV_1" />
      <xsl:with-param name="Importo" select="N_IMPORTO_SP_ACC_1" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_SP_1" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_SP_ACC_1" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_SP_ACC_1" />
    </xsl:call-template>

    <!-- DettaglioLinee - Spesa accessoria n.2 -->
    <xsl:call-template name="Block_DettaglioLinee_SpAcc">
      <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + 2" />
      <xsl:with-param name="Descrizione" select="C_DES_SP_ACC_DOCV_2" />
      <xsl:with-param name="Importo" select="N_IMPORTO_SP_ACC_2" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_SP_2" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_SP_ACC_2" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_SP_ACC_2" />
    </xsl:call-template>

    <!-- DettaglioLinee - Spesa accessoria n.3 -->
    <xsl:call-template name="Block_DettaglioLinee_SpAcc">
      <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + 3" />
      <xsl:with-param name="Descrizione" select="C_DES_SP_ACC_DOCV_3" />
      <xsl:with-param name="Importo" select="N_IMPORTO_SP_ACC_3" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_SP_3" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_SP_ACC_3" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_SP_ACC_3" />
    </xsl:call-template>

    <!-- DettaglioLinee - Spesa accessoria n.4 -->
    <xsl:call-template name="Block_DettaglioLinee_SpAcc">
      <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + 4" />
      <xsl:with-param name="Descrizione" select="C_DES_SP_ACC_DOCV_4" />
      <xsl:with-param name="Importo" select="N_IMPORTO_SP_ACC_4" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_SP_4" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_SP_ACC_4" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_SP_ACC_4" />
    </xsl:call-template>

  </xsl:template>

  <!-- DettaglioLinee da dichiararazione cliente -->
  <xsl:template name="Block_DettaglioLinee_DicCli_DatiBeniServizi">
    <xsl:param name="NumeroLinea_Start" />

    <xsl:call-template name="Block_DettaglioLinee_DicCli">
      <xsl:with-param name="NumeroLinea" select="$NumeroLinea_Start + 1" />
    </xsl:call-template>

  </xsl:template>

  <!-- DatiRiepilogo (in DatiBeniServizi) -->
  <xsl:template name="Block_DatiRiepilogo_DatiBeniServizi">

    <!-- DatiRiepilogo - Riga IVA n.1 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_1" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_1" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_1" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_1" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_1" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_1" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_1" />
      <xsl:with-param name="NumeroRigaIVA" select="1" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.2 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_2" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_2" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_2" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_2" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_2" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_2" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_2" />
      <xsl:with-param name="NumeroRigaIVA" select="2" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.3 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_3" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_3" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_3" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_3" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_3" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_3" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_3" />
      <xsl:with-param name="NumeroRigaIVA" select="3" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.4 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_4" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_4" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_4" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_4" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_4" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_4" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_4" />
      <xsl:with-param name="NumeroRigaIVA" select="4" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.5 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_5" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_5" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_5" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_5" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_5" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_5" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_5" />
      <xsl:with-param name="NumeroRigaIVA" select="5" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.6 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_6" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_6" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_6" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_6" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_6" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_6" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_6" />
      <xsl:with-param name="NumeroRigaIVA" select="6" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.7 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_7" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_7" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_7" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_7" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_7" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_7" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_7" />
      <xsl:with-param name="NumeroRigaIVA" select="7" />
    </xsl:call-template>

    <!-- DatiRiepilogo - Riga IVA n.8 -->
    <xsl:call-template name="Block_DatiRiepilogo">
      <xsl:with-param name="CodiceIVA" select="C_ID_CODICE_IVA_8" />
      <xsl:with-param name="AliquotaIVA" select="N_ALIQUOTA_8" />
      <xsl:with-param name="Natura" select="C_ID_NATURA_NONIMP_FTPA_8" />
      <xsl:with-param name="SpeseAccessorie" select="0" />
      <xsl:with-param name="Arrotondamento" select="N_ROUND_FTPA_8" />
      <xsl:with-param name="Imponibile" select="N_IMPONIBILE_8" />
      <xsl:with-param name="Imposta" select="N_IMPOSTA_8" />
      <xsl:with-param name="EsigibilitaIVA" select="C_ID_SPEC_OPT_CAUS_CONT" />
      <xsl:with-param name="RiferimentoNormativo" select="C_RIF_NORM_NONIMP_FTPA_8" />
      <xsl:with-param name="NumeroRigaIVA" select="8" />
    </xsl:call-template>

  </xsl:template>


  <!-- DatiBeniServizi -->
  <xsl:template name="Block_DatiBeniServizi">
    <DatiBeniServizi>
      <xsl:value-of select="$newline" />

      <!-- DettaglioLinee da righe documento -->
      <xsl:call-template name="Block_DettaglioLinee_RDocv_DatiBeniServizi">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
      </xsl:call-template>

      <!-- Numero linee inserite finora -->
      <xsl:variable name="NumeroLineeI" select="N_TOT_NUM_RIGHE_NORM + N_TOT_NUM_RIGHE_DETT" />

      <!-- DettaglioLinee da rettifiche per IVA agevolata -->
      <xsl:call-template name="Block_DettaglioLinee_RettificheIvaAg_DatiBeniServizi">
        <xsl:with-param name="NumeroLinea_Start" select="$NumeroLineeI" />
      </xsl:call-template>

      <!-- Numero linee inserite finora -->
      <xsl:variable name="NumeroLineeII" select="$NumeroLineeI + N_TOT_NUM_RIGHE_RETTIFICA_IVA_AG" />

      <!-- DettaglioLinee da contributi -->
      <xsl:call-template name="Block_DettaglioLinee_Contrib_DatiBeniServizi">
        <xsl:with-param name="Ditta" select="N_ID_DITTA" />
        <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
        <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
        <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
        <xsl:with-param name="Sede" select="N_ID_SEDE" />
        <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
        <xsl:with-param name="NumeroLinea_Start" select="$NumeroLineeII" />
      </xsl:call-template>

      <!-- Numero linee inserite finora -->
      <xsl:variable name="NumeroContrib">
        <xsl:call-template name="Count_DettaglioLinee_Contrib_DatiBeniServizi">
          <xsl:with-param name="Ditta" select="N_ID_DITTA" />
          <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
          <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
          <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
          <xsl:with-param name="Sede" select="N_ID_SEDE" />
          <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
        </xsl:call-template>
      </xsl:variable>
      <xsl:variable name="NumeroLineeIII" select="$NumeroLineeII + $NumeroContrib" />

      <!-- DettaglioLinee da spese accessorie -->
      <xsl:call-template name="Block_DettaglioLinee_SpAcc_DatiBeniServizi">
        <xsl:with-param name="NumeroLinea_Start" select="$NumeroLineeIII" />
      </xsl:call-template>

      <!-- Numero linee inserite finora -->
      <xsl:variable name="NumeroSpAcc">
        <xsl:choose>
          <xsl:when test="C_DES_SP_ACC_DOCV_4 != ''">
            <xsl:value-of select="4"/>
          </xsl:when>
          <xsl:when test="C_DES_SP_ACC_DOCV_3 != ''">
            <xsl:value-of select="3"/>
          </xsl:when>
          <xsl:when test="C_DES_SP_ACC_DOCV_2 != ''">
            <xsl:value-of select="2"/>
          </xsl:when>
          <xsl:when test="C_DES_SP_ACC_DOCV_1 != ''">
            <xsl:value-of select="1"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="0"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="NumeroLineeIV" select="$NumeroLineeIII + $NumeroSpAcc" />

      <!-- DettaglioLinee da dichiararazione cliente -->
      <xsl:call-template name="Block_DettaglioLinee_DicCli_DatiBeniServizi">
        <xsl:with-param name="NumeroLinea_Start" select="$NumeroLineeIV" />
      </xsl:call-template>

      <!-- DatiRiepilogo -->
      <xsl:call-template name="Block_DatiRiepilogo_DatiBeniServizi"/>

    </DatiBeniServizi>
    <xsl:value-of select="$newline" />

  </xsl:template>


  <!--:::::::::::: DatiBeniServizi/DettaglioLinee ::::::::::::-->

  <!--....... Righe documento .......-->

  <!-- Elementi valorizzati -->

  <!-- NumeroLinea -->
  <xsl:template name="Element_NumeroLinea_RDocv">
    <NumeroLinea>
      <xsl:value-of  select='format-number(N_PROGR_R_DOCV, "#")'/>
    </NumeroLinea>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceTipo (in CodiceArticolo) -->
  <xsl:template name="Element_CodiceTipo_CodiceArticolo_RDocv">
    <CodiceTipo>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="C_TP_COD_ART_FTPA" />
      </xsl:call-template>
    </CodiceTipo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceValore (in CodiceArticolo) -->
  <xsl:template name="Element_CodiceValore_CodiceArticolo_RDocv">
    <CodiceValore>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="substring(C_COD_ART_FTPA,1,35)" />
      </xsl:call-template>
    </CodiceValore>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceTipo (in CodiceArticolo 1) -->
  <xsl:template name="Element_CodiceTipo_CodiceArticolo_1_RDocv">
    <CodiceTipo>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="C_TP_COD_ART_FTPA_1" />
      </xsl:call-template>
    </CodiceTipo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceValore (in CodiceArticolo 1) -->
  <xsl:template name="Element_CodiceValore_CodiceArticolo_1_RDocv">
    <CodiceValore>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="substring(C_COD_ART_FTPA_1,1,35)" />
      </xsl:call-template>
    </CodiceValore>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceTipo (in CodiceArticolo 2) -->
  <xsl:template name="Element_CodiceTipo_CodiceArticolo_2_RDocv">
    <CodiceTipo>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="C_TP_COD_ART_FTPA_2" />
      </xsl:call-template>
    </CodiceTipo>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodiceValore (in CodiceArticolo 2) -->
  <xsl:template name="Element_CodiceValore_CodiceArticolo_2_RDocv">
    <CodiceValore>
      <xsl:call-template name="IS_convertInvalidBasicLatin">
        <xsl:with-param name="stringIn" select="substring(C_COD_ART_FTPA_2,1,35)" />
      </xsl:call-template>
    </CodiceValore>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Descrizione -->
  <xsl:template name="Element_Descrizione_RDocv">
    <Descrizione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_DES_R_DOCV,1,1000)" />
      </xsl:call-template>
    </Descrizione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Quantita -->
  <xsl:template name="Element_Quantita_RDocv">
    <xsl:variable name="QtaRDocv">
      <xsl:call-template name="Value_Qta_RDocv" />
    </xsl:variable>
    <xsl:variable name="IsQtaRDocvLTZero">
      <xsl:call-template name="Check_QtaRDocvLTZero" />
    </xsl:variable>

    <xsl:if test="$QtaRDocv != 0">
      <xsl:choose>
        <xsl:when test="$IsQtaRDocvLTZero = 1">
          <Quantita>
            <xsl:value-of  select='format-number(-$QtaRDocv, "0.00####")' />
          </Quantita>
        </xsl:when>
        <xsl:otherwise>
          <Quantita>
            <xsl:value-of  select='format-number($QtaRDocv, "0.00####")' />
          </Quantita>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

  <!-- UnitaMisura -->
  <xsl:template name="Element_UnitaMisura_RDocv">
    <xsl:variable name="UnitaMisura">
      <xsl:call-template name="Value_UnitaMisura_RDocv" />
    </xsl:variable>

    <xsl:if test="$UnitaMisura != ''">
      <UnitaMisura>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="$UnitaMisura" />
        </xsl:call-template>
      </UnitaMisura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- DataInizioPeriodo -->
  <xsl:template name="Element_DataInizioPeriodo_RDocv">
    <xsl:if test="N_FLAG_SERVIZIO != 0">
      <xsl:if test="D_DATA_CONS_MIN_R">
        <DataInizioPeriodo>
          <xsl:call-template name="IS_formatDateNoTime">
            <xsl:with-param name="dateIn" select="D_DATA_CONS_MIN_R" />
          </xsl:call-template>
        </DataInizioPeriodo>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- DataFinePeriodo -->
  <xsl:template name="Element_DataFinePeriodo_RDocv">
    <xsl:if test="N_FLAG_SERVIZIO != 0">
      <xsl:if test="D_DATA_CONS_MAX_R">
        <DataFinePeriodo>
          <xsl:call-template name="IS_formatDateNoTime">
            <xsl:with-param name="dateIn" select="D_DATA_CONS_MAX_R" />
          </xsl:call-template>
        </DataFinePeriodo>
        <xsl:value-of select="$newline" />
      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- PrezzoUnitario -->
  <xsl:template name="Element_PrezzoUnitario_RDocv">
    <xsl:variable name="IsQtaRDocvLTZero">
      <xsl:call-template name="Check_QtaRDocvLTZero" />
    </xsl:variable>

    <xsl:variable name="PrezzoUnitario">
      <xsl:choose>
        <xsl:when test="N_FLAG_PRZZ_A_SR = 1">
          <xsl:value-of  select='N_PREZZO_R_DOCV - N_DIFF_PREZZO_R_DOCV' />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select='N_PREZZO_R_DOCV' />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="$IsQtaRDocvLTZero = 1">
        <PrezzoUnitario>
          <xsl:value-of  select='format-number(-$PrezzoUnitario, "0.00######")' />
        </PrezzoUnitario>
      </xsl:when>
      <xsl:otherwise>
        <PrezzoUnitario>
          <xsl:value-of  select='format-number($PrezzoUnitario, "0.00######")' />
        </PrezzoUnitario>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Tipo (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Tipo_ScontoMaggiorazione_RDocv">
    <xsl:param name="SignedImp" />

    <Tipo>
      <xsl:choose>
        <xsl:when test="$SignedImp &lt; 0">
          <xsl:value-of  select="'MG'" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select="'SC'" />
        </xsl:otherwise>
      </xsl:choose>
    </Tipo>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- Percentuale (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Percentuale_ScontoMaggiorazione_RDocv">
    <xsl:param name="SignedPerc" />

    <Percentuale>
      <xsl:choose>
        <xsl:when test="$SignedPerc &lt; 0">
          <xsl:value-of  select='format-number(-$SignedPerc, "0.00")' />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select='format-number($SignedPerc, "0.00")' />
        </xsl:otherwise>
      </xsl:choose>
    </Percentuale>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- Importo (in ScontoMaggiorazione) -->
  <xsl:template name="Element_Importo_ScontoMaggiorazione_RDocv">
    <xsl:param name="SignedImp" />

    <Importo>
      <xsl:choose>
        <xsl:when test="$SignedImp &lt; 0">
          <xsl:value-of  select='format-number(-$SignedImp, "0.00")' />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select='format-number($SignedImp, "0.00")' />
        </xsl:otherwise>
      </xsl:choose>
    </Importo>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- PrezzoTotale -->
  <xsl:template name="Element_PrezzoTotale_RDocv">
    <PrezzoTotale>
      <xsl:value-of  select='format-number(N_IMPORTO_R_DOCV, "0.00######")' />
    </PrezzoTotale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaIVA -->
  <xsl:template name="Element_AliquotaIVA_RDocv">
    <AliquotaIVA>
      <xsl:value-of  select='format-number(N_ALIQUOTA_R, "0.00")' />
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ritenuta (in DettaglioLinee) -->
  <xsl:template name="Element_Ritenuta_DettaglioLinee">
    <xsl:call-template name="Element_Ritenuta_Generic">
      <xsl:with-param name="TotNumRigheRtnt" select="N_TOT_NUM_RIGHE_RTNT_V" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_R" />
    </xsl:call-template>
  </xsl:template>

  <!-- Natura (in DettaglioLinee) -->
  <xsl:template name="Element_Natura_DettaglioLinee_RDocv">
    <xsl:if test="C_ID_NATURA_NONIMP_FTPA_R != ''">
      <Natura>
        <xsl:value-of  select="C_ID_NATURA_NONIMP_FTPA_R"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoAmministrazione (in DettaglioLinee) -->
  <xsl:template name="Element_RiferimentoAmministrazione_RDocv">
    <!-- Non gestito -->
  </xsl:template>

  <!-- TipoDato (in AltriDatiGestionali) -->
  <xsl:template name="Element_TipoDato_AltriDatiGestionali">
    <xsl:param name="TipoDato" />

    <xsl:if test="$TipoDato">
      <TipoDato>
        <xsl:call-template name="IS_convertInvalidBasicLatin">
          <xsl:with-param name="stringIn" select="$TipoDato" />
        </xsl:call-template>
      </TipoDato>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoTesto (in AltriDatiGestionali) -->
  <xsl:template name="Element_RiferimentoTesto_AltriDatiGestionali">
    <xsl:param name="RiferimentoTesto" />

    <xsl:if test="$RiferimentoTesto">
      <RiferimentoTesto>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="substring($RiferimentoTesto, 1, 60)" />
        </xsl:call-template>
      </RiferimentoTesto>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoNumero (in AltriDatiGestionali) -->
  <xsl:template name="Element_RiferimentoNumero_AltriDatiGestionali">
    <xsl:param name="RiferimentoNumero" />

    <xsl:if test="$RiferimentoNumero">
      <RiferimentoNumero>
        <xsl:value-of  select="format-number($RiferimentoNumero, '0.00######')"/>
      </RiferimentoNumero>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoData (in AltriDatiGestionali) -->
  <xsl:template name="Element_RiferimentoData_AltriDatiGestionali">
    <xsl:param name="RiferimentoData" />

    <xsl:if test="$RiferimentoData">
      <RiferimentoData>
        <xsl:call-template name="IS_formatDateNoTime">
          <xsl:with-param name="dateIn" select="$RiferimentoData" />
        </xsl:call-template>
      </RiferimentoData>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoBool (in AltriDatiGestionali) -->
  <xsl:template name="Element_RiferimentoBool_AltriDatiGestionali">
    <xsl:param name="RiferimentoBool" />

    <xsl:if test="$RiferimentoBool > 0">
      <RiferimentoNumero>
        <xsl:value-of  select="format-number($RiferimentoBool, '0.00######')"/>
      </RiferimentoNumero>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- CodiceArticolo -->
  <xsl:template name="Block_CodiceArticolo_RDocv">
    <xsl:if test="C_TP_COD_ART_FTPA != '' and C_COD_ART_FTPA != ''">
      <CodiceArticolo>
        <xsl:value-of select="$newline" />
        <xsl:call-template name="Element_CodiceTipo_CodiceArticolo_RDocv"/>
        <xsl:call-template name="Element_CodiceValore_CodiceArticolo_RDocv"/>
      </CodiceArticolo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CodiceArticolo_1 -->
  <xsl:template name="Block_CodiceArticolo_1_RDocv">
    <xsl:if test="C_TP_COD_ART_FTPA_1 != '' and C_COD_ART_FTPA_1 != ''">
      <CodiceArticolo>
        <xsl:value-of select="$newline" />
        <xsl:call-template name="Element_CodiceTipo_CodiceArticolo_1_RDocv"/>
        <xsl:call-template name="Element_CodiceValore_CodiceArticolo_1_RDocv"/>
      </CodiceArticolo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- CodiceArticolo -->
  <xsl:template name="Block_CodiceArticolo_2_RDocv">
    <xsl:if test="C_TP_COD_ART_FTPA_2 != '' and C_COD_ART_FTPA_2 != ''">
      <CodiceArticolo>
        <xsl:value-of select="$newline" />
        <xsl:call-template name="Element_CodiceTipo_CodiceArticolo_2_RDocv"/>
        <xsl:call-template name="Element_CodiceValore_CodiceArticolo_2_RDocv"/>
      </CodiceArticolo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ScontoMaggiorazione -->
  <xsl:template name="Block_ScontoMaggiorazione_RDocv">
    <xsl:param name="IsImp" />
    <xsl:param name="SignedPercOImp" />

    <ScontoMaggiorazione>
      <xsl:value-of select="$newline" />

      <!-- Tipo (in ScontoMaggiorazione) -->
      <xsl:call-template name="Element_Tipo_ScontoMaggiorazione_RDocv">
        <xsl:with-param name="SignedImp" select="$SignedPercOImp" />
      </xsl:call-template>

      <!-- Percentuale (in ScontoMaggiorazione) -->
      <xsl:if test="$IsImp = 0">
        <xsl:call-template name="Element_Percentuale_ScontoMaggiorazione_RDocv">
          <xsl:with-param name="SignedPerc" select="$SignedPercOImp" />
        </xsl:call-template>
      </xsl:if>

      <!-- Importo (in ScontoMaggiorazione) -->
      <xsl:if test="$IsImp = 1">
        <xsl:call-template name="Element_Importo_ScontoMaggiorazione_RDocv">
          <xsl:with-param name="SignedImp" select="$SignedPercOImp" />
        </xsl:call-template>
      </xsl:if>

    </ScontoMaggiorazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- ScontoMaggiorazione (serie) -->
  <xsl:template name="Block_ScontoMaggiorazione_Series_RDocv">
    <xsl:variable name="IsConvVal">
      <xsl:call-template name="Check_ConvVal"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="N_FLAG_PRZZ_A_SR = 1">
      </xsl:when>
      <xsl:when test="$IsConvVal = 1">
      </xsl:when>
      <xsl:otherwise>
        <xsl:if test="N_DIFF_IMPORTO_R_DOCV != 0">
          <xsl:variable name="QtaRDocv">
            <xsl:call-template name="Value_Qta_RDocv" />
          </xsl:variable>

          <xsl:choose>
            <xsl:when test="$QtaRDocv = 0 or N_PREZZO_R_DOCV = 0">

              <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                <xsl:with-param name="IsImp" select="1" />
                <xsl:with-param name="SignedPercOImp" select="N_DIFF_IMPORTO_R_DOCV" />
              </xsl:call-template>

            </xsl:when>
            <xsl:otherwise>

              <xsl:if test="N_P_SCONTO_1_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_1_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_2_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_2_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_3_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_3_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_EXTRA_FTPA_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_EXTRA_FTPA_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_EXTRA_FTPA_2_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_EXTRA_FTPA_2_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_EXTRA_FTPA_3_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_EXTRA_FTPA_3_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_EXTRA_FTPA_4_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_EXTRA_FTPA_4_R" />
                </xsl:call-template>
              </xsl:if>

              <xsl:if test="N_P_SCONTO_EXTRA_FTPA_5_R != 0">
                <xsl:call-template name="Block_ScontoMaggiorazione_RDocv">
                  <xsl:with-param name="IsImp" select="0" />
                  <xsl:with-param name="SignedPercOImp" select="N_P_SCONTO_EXTRA_FTPA_5_R" />
                </xsl:call-template>
              </xsl:if>
            </xsl:otherwise>
          </xsl:choose>

        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- AltriDatiGestionali -->
  <xsl:template name="Block_AltriDatiGestionali_RDocv">
    <xsl:param name="TipoDato" />
    <xsl:param name="RiferimentoTesto" />
    <xsl:param name="RiferimentoNumero" />
    <xsl:param name="RiferimentoData" />
    <xsl:param name="RiferimentoBool" />

    <xsl:if test="$TipoDato">
      <xsl:if test="$RiferimentoTesto or $RiferimentoNumero or $RiferimentoData or $RiferimentoBool > 0">
        <AltriDatiGestionali>
          <xsl:value-of select="$newline" />

          <!-- TipoDato (in AltriDatiGestionali) -->
          <xsl:call-template name="Element_TipoDato_AltriDatiGestionali">
            <xsl:with-param name="TipoDato" select="$TipoDato" />
          </xsl:call-template>

          <!-- RiferimentoTesto (in AltriDatiGestionali) -->
          <xsl:call-template name="Element_RiferimentoTesto_AltriDatiGestionali">
            <xsl:with-param name="RiferimentoTesto" select="$RiferimentoTesto" />
          </xsl:call-template>

          <!-- RiferimentoNumero (in AltriDatiGestionali) -->
          <xsl:call-template name="Element_RiferimentoNumero_AltriDatiGestionali">
            <xsl:with-param name="RiferimentoNumero" select="$RiferimentoNumero" />
          </xsl:call-template>

          <!-- RiferimentoBool (in AltriDatiGestionali) -->
          <xsl:call-template name="Element_RiferimentoBool_AltriDatiGestionali">
            <xsl:with-param name="RiferimentoBool" select="$RiferimentoBool" />
          </xsl:call-template>

          <!-- RiferimentoData (in AltriDatiGestionali) -->
          <xsl:call-template name="Element_RiferimentoData_AltriDatiGestionali">
            <xsl:with-param name="RiferimentoData" select="$RiferimentoData" />
          </xsl:call-template>

        </AltriDatiGestionali>
        <xsl:value-of select="$newline" />

      </xsl:if>
    </xsl:if>
  </xsl:template>

  <!-- AltriDatiGestionali (in DettaglioLinee) -->
  <xsl:template name="Block_AltriDatiGestionali_DettaglioLinee_RDocv">
    <xsl:param name="ProgrTDocv" />
    <xsl:param name="ProgrRDocv" />
    
    <xsl:variable name="AddFirst">
      <xsl:choose>
        <xsl:when test="C_TIPO_RIGHE_RCHILD = 'RA'">
          <xsl:value-of select="'1'" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'0'" />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:if test="$AddFirst = 1">
      <xsl:call-template name="Block_AltriDatiGestionali_RDocv">
        <xsl:with-param name="TipoDato" select="C_TIPO_DATO_FTPA_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoTesto" select="C_VAL_STR_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoNumero" select="N_VAL_NUM_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoData" select="D_VAL_DAT_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoBool" select="N_VAL_NUM_BOOL_DOCV_ADHOC" />
      </xsl:call-template>
      <xsl:if test="N_VAL_NUM_TO_DOCV_ADHOC">
        <xsl:call-template name="Block_AltriDatiGestionali_RDocv">
          <xsl:with-param name="TipoDato" select="C_TIPO_DATO_FTPA_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoTesto" select="C_VAL_STR_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoNumero" select="N_VAL_NUM_TO_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoData" select="D_VAL_DAT_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoBool" select="N_VAL_NUM_BOOL_DOCV_ADHOC" />
        </xsl:call-template>
      </xsl:if>
    </xsl:if>

    <xsl:for-each select="following-sibling::QuantoDataRow[
                  N_PROGR_T_DOCV = $ProgrTDocv and
                  N_PROGR_R_DOCV = $ProgrRDocv and
                  C_TIPO_RIGHE_RCHILD = 'RA']">
      <xsl:call-template name="Block_AltriDatiGestionali_RDocv">
        <xsl:with-param name="TipoDato" select="C_TIPO_DATO_FTPA_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoTesto" select="C_VAL_STR_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoNumero" select="N_VAL_NUM_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoData" select="D_VAL_DAT_DOCV_ADHOC" />
        <xsl:with-param name="RiferimentoBool" select="N_VAL_NUM_BOOL_DOCV_ADHOC" />
      </xsl:call-template>
      <xsl:if test="N_VAL_NUM_TO_DOCV_ADHOC">
        <xsl:call-template name="Block_AltriDatiGestionali_RDocv">
          <xsl:with-param name="TipoDato" select="C_TIPO_DATO_FTPA_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoTesto" select="C_VAL_STR_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoNumero" select="N_VAL_NUM_TO_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoData" select="D_VAL_DAT_DOCV_ADHOC" />
          <xsl:with-param name="RiferimentoBool" select="N_VAL_NUM_BOOL_DOCV_ADHOC" />
        </xsl:call-template>
      </xsl:if>
    </xsl:for-each> 
    
  </xsl:template>

  <!-- DettaglioLinee -->
  <xsl:template name="Block_DettaglioLinee_RDocv">
    <xsl:if test="N_TIPO_R_DOCV != 1">
      <DettaglioLinee>
        <xsl:value-of select="$newline" />

        <!-- NumeroLinea -->
        <xsl:call-template name="Element_NumeroLinea_RDocv"/>

        <!-- CodiceArticolo -->
        <xsl:call-template name="Block_CodiceArticolo_RDocv"/>

        <!-- CodiceArticolo_1 -->
        <xsl:call-template name="Block_CodiceArticolo_1_RDocv"/>

        <!-- CodiceArticolo_2 -->
        <xsl:call-template name="Block_CodiceArticolo_2_RDocv"/>

        <!-- Descrizione -->
        <xsl:call-template name="Element_Descrizione_RDocv"/>

        <!-- Quantita -->
        <xsl:call-template name="Element_Quantita_RDocv"/>

        <!-- UnitaMisura -->
        <xsl:call-template name="Element_UnitaMisura_RDocv"/>

        <!-- DataInizioPeriodo -->
        <xsl:call-template name="Element_DataInizioPeriodo_RDocv"/>

        <!-- DataFinePeriodo -->
        <xsl:call-template name="Element_DataFinePeriodo_RDocv"/>

        <!-- PrezzoUnitario -->
        <xsl:call-template name="Element_PrezzoUnitario_RDocv"/>

        <!-- ScontoMaggiorazione (serie) -->
        <xsl:call-template name="Block_ScontoMaggiorazione_Series_RDocv"/>

        <!-- PrezzoTotale -->
        <xsl:call-template name="Element_PrezzoTotale_RDocv"/>

        <!-- AliquotaIVA -->
        <xsl:call-template name="Element_AliquotaIVA_RDocv"/>

        <!-- Ritenuta (in DettaglioLinee) -->
        <xsl:call-template name="Element_Ritenuta_DettaglioLinee"/>

        <!-- Natura (in DettaglioLinee) -->
        <xsl:call-template name="Element_Natura_DettaglioLinee_RDocv"/>

        <!-- RiferimentoAmministrazione (in DettaglioLinee) -->
        <xsl:call-template name="Element_RiferimentoAmministrazione_RDocv"/>

        <!-- AltriDatiGestionali (in DettaglioLinee) -->
        <xsl:call-template name="Block_AltriDatiGestionali_DettaglioLinee_RDocv">
          <xsl:with-param name="ProgrTDocv" select="N_PROGR_T_DOCV" />
          <xsl:with-param name="ProgrRDocv" select="N_PROGR_R_DOCV" />
        </xsl:call-template>

      </DettaglioLinee>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>


  <!--....... Rettifiche IVA agevolata .......-->

  <!-- Elementi valorizzati -->

  <!-- NumeroLinea -->
  <xsl:template name="Element_NumeroLinea_RettificheIvaAg">
    <xsl:param name="NumeroLinea" />
    <NumeroLinea>
      <xsl:value-of  select="$NumeroLinea"/>
    </NumeroLinea>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- TipoCessionePrestazione -->
  <xsl:template name="Element_TipoCessionePrestazione_RettificheIvaAg">
    <!-- Non viene valorizzato -->
  </xsl:template>

  <!-- Descrizione -->
  <xsl:template name="Element_Descrizione_RettificheIvaAg">
    <Descrizione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_DES_RETTIFICA_IVA_AG,1,1000)" />
      </xsl:call-template>
    </Descrizione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Quantita -->
  <xsl:template name="Element_Quantita_RettificheIvaAg">
    <!-- Non viene valorizzato -->
  </xsl:template>

  <!-- UnitaMisura -->
  <xsl:template name="Element_UnitaMisura_RettificheIvaAg">
    <!-- Non viene valorizzato -->
  </xsl:template>

  <!-- PrezzoUnitario -->
  <xsl:template name="Element_PrezzoUnitario_RettificheIvaAg">
    <PrezzoUnitario>
      <xsl:value-of  select='format-number(N_IMPORTO_RETTIFICA_IVA_AG, "0.00###")'/>
    </PrezzoUnitario>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoTotale -->
  <xsl:template name="Element_PrezzoTotale_RettificheIvaAg">
    <PrezzoTotale>
      <xsl:value-of  select='format-number(N_IMPORTO_RETTIFICA_IVA_AG, "0.00######")'/>
    </PrezzoTotale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaIVA -->
  <xsl:template name="Element_AliquotaIVA_RettificheIvaAg">
    <AliquotaIVA>
      <xsl:value-of  select='format-number(N_ALIQUOTA_RETTIFICA_IVA_AG, "0.00")'/>
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ritenuta -->
  <xsl:template name="Element_Ritenuta_RettificheIvaAg">
    <!-- Non viene valorizzato -->
  </xsl:template>

  <!-- Natura (in DettaglioLinee) -->
  <xsl:template name="Element_Natura_RettificheIvaAg">
    <xsl:if test="C_ID_NATURA_NONIMP_FTPA_RETTIFICA_IVA_AG != ''">
      <Natura>
        <xsl:value-of  select="C_ID_NATURA_NONIMP_FTPA_RETTIFICA_IVA_AG"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DettaglioLinee -->
  <xsl:template name="Block_DettaglioLinee_RettificheIvaAg">
    <xsl:param name="NumeroLinea" />

    <DettaglioLinee>
      <xsl:value-of select="$newline" />

      <!-- NumeroLinea -->
      <xsl:call-template name="Element_NumeroLinea_RettificheIvaAg">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea" />
      </xsl:call-template>

      <!-- TipoCessionePrestazione -->
      <xsl:call-template name="Element_TipoCessionePrestazione_RettificheIvaAg" />

      <!-- Descrizione -->
      <xsl:call-template name="Element_Descrizione_RettificheIvaAg" />

      <!-- Quantita -->
      <xsl:call-template name="Element_Quantita_RettificheIvaAg"/>

      <!-- UnitaMisura -->
      <xsl:call-template name="Element_UnitaMisura_RettificheIvaAg"/>

      <!-- PrezzoUnitario -->
      <xsl:call-template name="Element_PrezzoUnitario_RettificheIvaAg" />

      <!-- PrezzoTotale -->
      <xsl:call-template name="Element_PrezzoTotale_RettificheIvaAg" />

      <!-- AliquotaIVA -->
      <xsl:call-template name="Element_AliquotaIVA_RettificheIvaAg" />

      <!-- Ritenuta (in DettaglioLinee) -->
      <xsl:call-template name="Element_Ritenuta_RettificheIvaAg"/>

      <!-- Natura (in DettaglioLinee) -->
      <xsl:call-template name="Element_Natura_RettificheIvaAg"/>

    </DettaglioLinee>
    <xsl:value-of select="$newline" />

  </xsl:template>




  <!--.......... Contributi ..........-->

  <!-- Elementi valorizzati -->

  <!-- NumeroLinea -->
  <xsl:template name="Element_NumeroLinea_Contrib">
    <xsl:param name="NumeroLinea" />

    <NumeroLinea>
      <xsl:value-of  select="$NumeroLinea"/>
    </NumeroLinea>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- TipoCessionePrestazione -->
  <xsl:template name="Element_TipoCessionePrestazione_Contrib">
    <TipoCessionePrestazione>
      <xsl:value-of  select="'AC'"/>
    </TipoCessionePrestazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Descrizione -->
  <xsl:template name="Element_Descrizione_Contrib">
    <Descrizione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_DES_CONTRIB,1,1000)" />
      </xsl:call-template>
    </Descrizione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Quantita -->
  <xsl:template name="Element_Quantita_Contrib">
    <xsl:choose>
      <xsl:when test="N_QTA_TOT_CONTRIB &lt; 0">
        <Quantita>
          <xsl:value-of  select='format-number(-N_QTA_TOT_CONTRIB, "0.00####")' />
        </Quantita>
      </xsl:when>
      <xsl:otherwise>
        <Quantita>
          <xsl:value-of  select='format-number(N_QTA_TOT_CONTRIB, "0.00####")' />
        </Quantita>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- UnitaMisura -->
  <xsl:template name="Element_UnitaMisura_Contrib">
    <xsl:if test="N_QTA_TOT_CONTRIB != 0">
      <UnitaMisura>
        <xsl:value-of  select="C_ID_UM_CONTRIB" />
      </UnitaMisura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- PrezzoUnitario -->
  <xsl:template name="Element_PrezzoUnitario_Contrib">
    <xsl:choose>
      <xsl:when test="N_QTA_TOT_CONTRIB &lt; 0">
        <PrezzoUnitario>
          <xsl:value-of  select='format-number(-N_CST_UNI_CONTRIB, "0.00######")'/>
        </PrezzoUnitario>
      </xsl:when>
      <xsl:otherwise>
        <PrezzoUnitario>
          <xsl:value-of  select='format-number(N_CST_UNI_CONTRIB, "0.00######")'/>
        </PrezzoUnitario>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoTotale -->
  <xsl:template name="Element_PrezzoTotale_Contrib">
    <PrezzoTotale>
      <xsl:value-of  select='format-number(N_IMPORTO_CONTRIB, "0.00######")'/>
    </PrezzoTotale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaIVA -->
  <xsl:template name="Element_AliquotaIVA_Contrib">
    <AliquotaIVA>
      <xsl:value-of  select='format-number(N_ALIQUOTA_CONTRIB, "0.00")' />
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ritenuta (in DettaglioLinee) -->
  <xsl:template name="Element_Ritenuta_DettaglioLinee_Contrib">
    <xsl:call-template name="Element_Ritenuta_Generic">
      <xsl:with-param name="TotNumRigheRtnt" select="N_TOT_NUM_RIGHE_RTNT_V" />
      <xsl:with-param name="FlagNoRtnt" select="N_FLAG_NO_RTNT_V_CONTRIB" />
    </xsl:call-template>
  </xsl:template>

  <!-- Natura (in DettaglioLinee) -->
  <xsl:template name="Element_Natura_DettaglioLinee_Contrib">
    <xsl:if test="C_ID_NATURA_NONIMP_FTPA_CONTRIB != ''">
      <Natura>
        <xsl:value-of  select="C_ID_NATURA_NONIMP_FTPA_CONTRIB"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DettaglioLinee -->
  <xsl:template name="Block_DettaglioLinee_Contrib">
    <xsl:param name="NumeroLinea" />

    <DettaglioLinee>
      <xsl:value-of select="$newline" />

      <!-- NumeroLinea -->
      <xsl:call-template name="Element_NumeroLinea_Contrib">
        <xsl:with-param name="NumeroLinea" select="$NumeroLinea" />
      </xsl:call-template>

      <!-- TipoCessionePrestazione -->
      <xsl:call-template name="Element_TipoCessionePrestazione_Contrib" />

      <!-- Descrizione -->
      <xsl:call-template name="Element_Descrizione_Contrib" />

      <!-- Quantita -->
      <xsl:call-template name="Element_Quantita_Contrib"/>

      <!-- UnitaMisura -->
      <xsl:call-template name="Element_UnitaMisura_Contrib"/>

      <!-- PrezzoUnitario -->
      <xsl:call-template name="Element_PrezzoUnitario_Contrib" />

      <!-- PrezzoTotale -->
      <xsl:call-template name="Element_PrezzoTotale_Contrib" />

      <!-- AliquotaIVA -->
      <xsl:call-template name="Element_AliquotaIVA_Contrib" />

      <!-- Ritenuta (in DettaglioLinee) -->
      <xsl:call-template name="Element_Ritenuta_DettaglioLinee_Contrib"/>

      <!-- Natura (in DettaglioLinee) -->
      <xsl:call-template name="Element_Natura_DettaglioLinee_Contrib"/>

    </DettaglioLinee>
    <xsl:value-of select="$newline" />

  </xsl:template>


  <!--....... Spese accessorie .......-->

  <!-- Elementi valorizzati -->

  <!-- NumeroLinea -->
  <xsl:template name="Element_NumeroLinea_SpAcc">
    <xsl:param name="NumeroLinea" />
    <NumeroLinea>
      <xsl:value-of  select="$NumeroLinea"/>
    </NumeroLinea>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- TipoCessionePrestazione -->
  <xsl:template name="Element_TipoCessionePrestazione_SpAcc">
    <TipoCessionePrestazione>
      <xsl:value-of  select="'AC'"/>
    </TipoCessionePrestazione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Descrizione -->
  <xsl:template name="Element_Descrizione_SpAcc">
    <xsl:param name="Descrizione" />
    <Descrizione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring($Descrizione,1,1000)" />
      </xsl:call-template>
    </Descrizione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoUnitario -->
  <xsl:template name="Element_PrezzoUnitario_SpAcc">
    <xsl:param name="Prezzo" />
    <PrezzoUnitario>
      <xsl:value-of  select='format-number($Prezzo, "0.00######")'/>
    </PrezzoUnitario>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoTotale -->
  <xsl:template name="Element_PrezzoTotale_SpAcc">
    <xsl:param name="Importo" />
    <PrezzoTotale>
      <xsl:value-of  select='format-number($Importo, "0.00######")'/>
    </PrezzoTotale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaIVA -->
  <xsl:template name="Element_AliquotaIVA_SpAcc">
    <xsl:param name="AliquotaIVA" />
    <AliquotaIVA>
      <xsl:value-of  select='format-number($AliquotaIVA, "0.00")'/>
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Ritenuta (in DettaglioLinee) -->
  <xsl:template name="Element_Ritenuta_DettaglioLinee_SpAcc">
    <xsl:param name="FlagNoRtnt" />
    <xsl:call-template name="Element_Ritenuta_Generic">
      <xsl:with-param name="TotNumRigheRtnt" select="N_TOT_NUM_RIGHE_RTNT_V" />
      <xsl:with-param name="FlagNoRtnt" select="$FlagNoRtnt" />
    </xsl:call-template>
  </xsl:template>

  <!-- Natura (in DettaglioLinee) -->
  <xsl:template name="Element_Natura_DettaglioLinee_SpAcc">
    <xsl:param name="Natura" />
    <xsl:if test="$Natura != ''">
      <Natura>
        <xsl:value-of  select="$Natura"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DettaglioLinee -->
  <xsl:template name="Block_DettaglioLinee_SpAcc">
    <xsl:param name="NumeroLinea" />
    <xsl:param name="Descrizione" />
    <xsl:param name="Importo" />
    <xsl:param name="AliquotaIVA" />
    <xsl:param name="FlagNoRtnt" />
    <xsl:param name="Natura" />

    <xsl:if test="$Descrizione != ''">
      <DettaglioLinee>
        <xsl:value-of select="$newline" />

        <!-- NumeroLinea -->
        <xsl:call-template name="Element_NumeroLinea_SpAcc">
          <xsl:with-param name="NumeroLinea" select="$NumeroLinea" />
        </xsl:call-template>

        <!-- TipoCessionePrestazione -->
        <xsl:call-template name="Element_TipoCessionePrestazione_SpAcc" />

        <!-- Descrizione -->
        <xsl:call-template name="Element_Descrizione_SpAcc">
          <xsl:with-param name="Descrizione" select="$Descrizione" />
        </xsl:call-template>

        <!-- PrezzoUnitario -->
        <xsl:call-template name="Element_PrezzoUnitario_SpAcc">
          <xsl:with-param name="Prezzo" select="$Importo" />
        </xsl:call-template>

        <!-- PrezzoTotale -->
        <xsl:call-template name="Element_PrezzoTotale_SpAcc">
          <xsl:with-param name="Importo" select="$Importo" />
        </xsl:call-template>

        <!-- AliquotaIVA -->
        <xsl:call-template name="Element_AliquotaIVA_SpAcc">
          <xsl:with-param name="AliquotaIVA" select="$AliquotaIVA" />
        </xsl:call-template>

        <!-- Ritenuta (in DettaglioLinee) -->
        <xsl:call-template name="Element_Ritenuta_DettaglioLinee_SpAcc">
          <xsl:with-param name="FlagNoRtnt" select="$FlagNoRtnt" />
        </xsl:call-template>

        <!-- Natura (in DettaglioLinee) -->
        <xsl:call-template name="Element_Natura_DettaglioLinee_SpAcc">
          <xsl:with-param name="Natura" select="$Natura" />
        </xsl:call-template>

      </DettaglioLinee>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>

  <!--....... Dichiarazioni clienti .......-->

  <!-- Elementi valorizzati -->

  <!-- NumeroLinea -->
  <xsl:template name="Element_NumeroLinea_DicCli">
    <xsl:param name="NumeroLinea" />
    <NumeroLinea>
      <xsl:value-of  select="$NumeroLinea"/>
    </NumeroLinea>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Descrizione -->
  <xsl:template name="Element_Descrizione_DicCli">
    <Descrizione>
      <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
        <xsl:with-param name="stringIn" select="substring(C_DES_DIC_CLI,1,1000)" />
      </xsl:call-template>
    </Descrizione>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoUnitario -->
  <xsl:template name="Element_PrezzoUnitario_DicCli">
    <PrezzoUnitario>
      <xsl:value-of  select='format-number(0, "0.00######")'/>
    </PrezzoUnitario>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- PrezzoTotale -->
  <xsl:template name="Element_PrezzoTotale_DicCli">
    <PrezzoTotale>
      <xsl:value-of  select='format-number(0, "0.00######")' />
    </PrezzoTotale>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- AliquotaIVA -->
  <xsl:template name="Element_AliquotaIVA_DicCli">
    <AliquotaIVA>
      <xsl:value-of  select='format-number(N_ALIQUOTA_1, "0.00")' />
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Natura (in DettaglioLinee) -->
  <xsl:template name="Element_Natura_DettaglioLinee_DicCli">
    <xsl:if test="C_ID_NATURA_NONIMP_FTPA_1 != ''">
      <Natura>
        <xsl:value-of  select="C_ID_NATURA_NONIMP_FTPA_1"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- Blocchi -->

  <!-- DettaglioLinee -->
  <xsl:template name="Block_DettaglioLinee_DicCli">
    <xsl:param name="NumeroLinea" />

    <xsl:if test="N_FLAG_DIC_CLI_FTPA = 1">
      <DettaglioLinee>
        <xsl:value-of select="$newline" />

        <!-- NumeroLinea -->
        <xsl:call-template name="Element_NumeroLinea_DicCli">
          <xsl:with-param name="NumeroLinea" select="$NumeroLinea" />
        </xsl:call-template>

        <!-- Descrizione -->
        <xsl:call-template name="Element_Descrizione_DicCli"/>

        <!-- PrezzoUnitario -->
        <xsl:call-template name="Element_PrezzoUnitario_DicCli"/>

        <!-- PrezzoTotale -->
        <xsl:call-template name="Element_PrezzoTotale_DicCli"/>

        <!-- AliquotaIVA -->
        <xsl:call-template name="Element_AliquotaIVA_DicCli"/>

        <!-- Natura (in DettaglioLinee) -->
        <xsl:call-template name="Element_Natura_DettaglioLinee_DicCli"/>

      </DettaglioLinee>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>


  <!--:::::::::::: DatiBeniServizi/DatiRiepilogo ::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- AliquotaIVA (in DatiRiepilogo) -->
  <xsl:template name="Element_AliquotaIVA_DatiRiepilogo">
    <xsl:param name="AliquotaIVA" />
    <AliquotaIVA>
      <xsl:value-of  select='format-number($AliquotaIVA, "0.00")'/>
    </AliquotaIVA>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Natura (in DatiRiepilogo) -->
  <xsl:template name="Element_Natura_DatiRiepilogo">
    <xsl:param name="Natura" />
    <xsl:if test="$Natura != ''">
      <Natura>
        <xsl:value-of  select="$Natura"/>
      </Natura>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- SpeseAccessorie  (in DatiRiepilogo) -->
  <xsl:template name="Element_SpeseAccessorie_DatiRiepilogo">
    <xsl:param name="SpeseAccessorie"/>
    <!-- Non gestito -->
  </xsl:template>

  <!-- Arrotondamento  (in DatiRiepilogo) -->
  <xsl:template name="Element_Arrotondamento_DatiRiepilogo">
    <xsl:param name="Arrotondamento"/>
	  <xsl:if test="N_FLAG_APPLICA_IVA_AG = 0">
		  <xsl:if test="$Arrotondamento != 0">
			  <Arrotondamento>
				  <xsl:value-of  select='format-number($Arrotondamento, "0.00######")'/>
			  </Arrotondamento>
		  </xsl:if>
		  <xsl:value-of select="$newline" />
	  </xsl:if>
  </xsl:template>

  <!-- ImponibileImporto  (in DatiRiepilogo) -->
  <xsl:template name="Element_ImponibileImporto_DatiRiepilogo">
    <xsl:param name="Imponibile"/>
    <ImponibileImporto>
      <xsl:value-of  select='format-number($Imponibile, "0.00#")'/>
    </ImponibileImporto>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Imposta (in DatiRiepilogo) -->
  <xsl:template name="Element_Imposta_DatiRiepilogo">
    <xsl:param name="Imposta"/>
    <Imposta>
      <xsl:value-of  select='format-number($Imposta, "0.00#")'/>
    </Imposta>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- EsigibilitaIVA (in DatiRiepilogo) -->
  <xsl:template name="Element_EsigibilitaIVA_DatiRiepilogo">
    <xsl:param name="EsigibilitaIVA"/>
    <xsl:param name="AliquotaIVA" />
    <xsl:param name="NumeroRigaIVA" />

    <xsl:variable name="DiffOImm">
      <xsl:choose>
        <xsl:when test="$EsigibilitaIVA = 'FD'">
          <xsl:value-of  select="'D'"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of  select="'I'"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="IsSplitPayment">
      <xsl:call-template name="Check_SplitPayment">
        <xsl:with-param name="AliquotaIVA" select="$AliquotaIVA" />
        <xsl:with-param name="NumRigaIVA" select="$NumeroRigaIVA" />
      </xsl:call-template>
    </xsl:variable>

    <xsl:if test="$AliquotaIVA > 0">
      <EsigibilitaIVA>
        <xsl:choose>
          <xsl:when test="$IsSplitPayment &gt; 0">
            <xsl:value-of  select="'S'"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$DiffOImm"/>
          </xsl:otherwise>
        </xsl:choose>
      </EsigibilitaIVA>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- RiferimentoNormativo (in DatiRiepilogo) -->
  <xsl:template name="Element_RiferimentoNormativo_DatiRiepilogo">
    <xsl:param name="RiferimentoNormativo"/>
    <xsl:if test="$RiferimentoNormativo != ''">
      <RiferimentoNormativo>
        <xsl:call-template name="IS_convertInvalidBasicAndLatin1Supplement">
          <xsl:with-param name="stringIn" select="substring($RiferimentoNormativo,1,100)" />
        </xsl:call-template>
      </RiferimentoNormativo>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>


  <!-- Blocchi -->

  <!-- DatiRiepilogo -->
  <xsl:template name="Block_DatiRiepilogo">
    <xsl:param name="CodiceIVA" />
    <xsl:param name="AliquotaIVA" />
    <xsl:param name="Natura" />
    <xsl:param name="SpeseAccessorie" />
    <xsl:param name="Arrotondamento" />
    <xsl:param name="Imponibile" />
    <xsl:param name="Imposta" />
    <xsl:param name="EsigibilitaIVA"/>
    <xsl:param name="RiferimentoNormativo"/>
    <xsl:param name="NumeroRigaIVA" />

    <xsl:if test="$CodiceIVA != ''">
      <DatiRiepilogo>
        <xsl:value-of select="$newline" />

        <!-- AliquotaIVA (in DatiRiepilogo) -->
        <xsl:call-template name="Element_AliquotaIVA_DatiRiepilogo">
          <xsl:with-param name="AliquotaIVA" select="$AliquotaIVA" />
        </xsl:call-template>

        <!-- Natura (in DatiRiepilogo) -->
        <xsl:call-template name="Element_Natura_DatiRiepilogo">
          <xsl:with-param name="Natura" select="$Natura" />
        </xsl:call-template>

        <!-- SpeseAccessorie (in DatiRiepilogo) -->
        <xsl:call-template name="Element_SpeseAccessorie_DatiRiepilogo">
          <xsl:with-param name="SpeseAccessorie" select="$SpeseAccessorie" />
        </xsl:call-template>

        <!-- Arrotondamento (in DatiRiepilogo) -->
        <xsl:call-template name="Element_Arrotondamento_DatiRiepilogo">
          <xsl:with-param name="Arrotondamento" select="$Arrotondamento" />
        </xsl:call-template>

        <!-- ImponibileImporto (in DatiRiepilogo) -->
        <xsl:call-template name="Element_ImponibileImporto_DatiRiepilogo">
          <xsl:with-param name="Imponibile" select="$Imponibile" />
        </xsl:call-template>

        <!-- Imposta (in DatiRiepilogo) -->
        <xsl:call-template name="Element_Imposta_DatiRiepilogo">
          <xsl:with-param name="Imposta" select="$Imposta" />
        </xsl:call-template>

        <!-- EsigibilitaIVA (in DatiRiepilogo) -->
        <xsl:call-template name="Element_EsigibilitaIVA_DatiRiepilogo">
          <xsl:with-param name="EsigibilitaIVA" select="$EsigibilitaIVA" />
          <xsl:with-param name="AliquotaIVA" select="$AliquotaIVA" />
          <xsl:with-param name="NumeroRigaIVA" select="$NumeroRigaIVA" />
        </xsl:call-template>

        <!-- RiferimentoNormativo (in DatiRiepilogo) -->
        <xsl:call-template name="Element_RiferimentoNormativo_DatiRiepilogo">
          <xsl:with-param name="RiferimentoNormativo" select="$RiferimentoNormativo" />
        </xsl:call-template>

      </DatiRiepilogo>
      <xsl:value-of select="$newline" />
    </xsl:if>

  </xsl:template>


  <!--:::::::::::::::::::: DatiVeicoli ::::::::::::::::::::-->

  <!-- Blocchi -->

  <!-- DatiVeicoli -->
  <xsl:template name="Block_DatiVeicoli">
    <!-- Non gestito -->
  </xsl:template>


  <!--:::::::::::::::::::: DatiPagamento ::::::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- CondizioniPagamento -->
  <xsl:template name="Element_CondizioniPagamento_Gloscad">
    <CondizioniPagamento>
      <xsl:choose>
        <xsl:when test="N_NUMERO_RATE_GLOSCAD = 1">
          <xsl:value-of select="'TP02'" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="'TP01'" />
        </xsl:otherwise>
      </xsl:choose>
    </CondizioniPagamento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- Beneficiario -->
  <xsl:template name="Element_Beneficiario_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- ModalitaPagamento -->
  <xsl:template name="Element_ModalitaPagamento_Gloscad">
    <ModalitaPagamento>
      <xsl:value-of select="C_ID_MOD_PAG_FTPA_GLOSCAD" />
    </ModalitaPagamento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- DataRiferimentoTerminiPagamento -->
  <xsl:template name="Element_DataRiferimentoTerminiPagamento_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- GiorniTerminiPagamento -->
  <xsl:template name="Element_GiorniTerminiPagamento_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- DataScadenzaPagamento -->
  <xsl:template name="Element_DataScadenzaPagamento_Gloscad">
    <DataScadenzaPagamento>
      <xsl:call-template name="IS_formatDateNoTime">
        <xsl:with-param name="dateIn" select="D_DATA_GLOSCAD" />
      </xsl:call-template>
    </DataScadenzaPagamento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- ImportoPagamento -->
  <xsl:template name="Element_ImportoPagamento_Gloscad">
    <ImportoPagamento>
      <xsl:choose>
        <xsl:when test="C_ID_VALUTA">
          <xsl:value-of select='format-number(N_IMPORTO_VAL_GLOSCAD, "0.00#")' />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select='format-number(N_IMPORTO_MB_GLOSCAD, "0.00#")' />
        </xsl:otherwise>
      </xsl:choose>
    </ImportoPagamento>
    <xsl:value-of select="$newline" />
  </xsl:template>

  <!-- CodUfficioPostale -->
  <xsl:template name="Element_CodUfficioPostale_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CognomeQuietanzante -->
  <xsl:template name="Element_CognomeQuietanzante_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- NomeQuietanzante -->
  <xsl:template name="Element_NomeQuietanzante_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CFQuietanzante -->
  <xsl:template name="Element_CFQuietanzante_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- TitoloQuietanzante -->
  <xsl:template name="Element_TitoloQuietanzante_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- IstitutoFinanziario -->
  <xsl:template name="Element_IstitutoFinanziario_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- IBAN -->
  <xsl:template name="Element_IBAN_Gloscad">
    <xsl:variable name="IsNotaCredito">
      <xsl:call-template name="Check_NotaCredito"/>
    </xsl:variable>
    <xsl:variable name="Iban">
      <xsl:choose>
        <xsl:when test="$IsNotaCredito = 1">
          <xsl:call-template name="Value_IBAN_BancaAppoggio"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="Value_IBAN_LineaCredito"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:if test="$Iban != ''">
      <IBAN>
        <xsl:value-of select="$Iban" />
      </IBAN>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ABI -->
  <xsl:template name="Element_ABI_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CAB -->
  <xsl:template name="Element_CAB_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- BIC -->
  <xsl:template name="Element_BIC_Gloscad">
    <xsl:variable name="IsNotaCredito">
      <xsl:call-template name="Check_NotaCredito"/>
    </xsl:variable>
    <xsl:variable name="Bic">
      <xsl:choose>
        <xsl:when test="$IsNotaCredito = 1">
          <xsl:value-of select="C_BIC"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="C_BIC_LC"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:if test="$Bic != ''">
      <BIC>
        <xsl:value-of select="$Bic"/>
      </BIC>
      <xsl:value-of select="$newline" />
    </xsl:if>
  </xsl:template>

  <!-- ScontoPagamentoAnticipato -->
  <xsl:template name="Element_ScontoPagamentoAnticipato_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- DataLimitePagamentoAnticipato -->
  <xsl:template name="Element_DataLimitePagamentoAnticipato_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- PenalitaPagamentiRitardati -->
  <xsl:template name="Element_PenalitaPagamentiRitardati_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- DataDecorrenzaPenale -->
  <xsl:template name="Element_DataDecorrenzaPenale_Gloscad">
    <!-- Non gestito -->
  </xsl:template>

  <!-- CodicePagamento -->
  <xsl:template name="Element_CodicePagamento_Gloscad">
    <!-- Non gestito -->
  </xsl:template>


  <!-- Blocchi -->

  <!-- DettaglioPagamento -->
  <xsl:template name="Block_DettaglioPagamento_Gloscad">
    <DettaglioPagamento>
      <xsl:value-of select="$newline" />

      <!-- Beneficiario -->
      <xsl:call-template name="Element_Beneficiario_Gloscad"/>

      <!-- ModalitaPagamento -->
      <xsl:call-template name="Element_ModalitaPagamento_Gloscad"/>

      <!-- DataRiferimentoTerminiPagamento -->
      <xsl:call-template name="Element_DataRiferimentoTerminiPagamento_Gloscad"/>

      <!-- GiorniTerminiPagamento -->
      <xsl:call-template name="Element_GiorniTerminiPagamento_Gloscad"/>

      <!-- DataScadenzaPagamento -->
      <xsl:call-template name="Element_DataScadenzaPagamento_Gloscad"/>

      <!-- ImportoPagamento -->
      <xsl:call-template name="Element_ImportoPagamento_Gloscad"/>

      <!-- CodUfficioPostale -->
      <xsl:call-template name="Element_CodUfficioPostale_Gloscad"/>

      <!-- CognomeQuietanzante -->
      <xsl:call-template name="Element_CognomeQuietanzante_Gloscad"/>

      <!-- NomeQuietanzante -->
      <xsl:call-template name="Element_NomeQuietanzante_Gloscad"/>

      <!-- CFQuietanzante -->
      <xsl:call-template name="Element_CFQuietanzante_Gloscad"/>

      <!-- TitoloQuietanzante -->
      <xsl:call-template name="Element_TitoloQuietanzante_Gloscad"/>

      <!-- IstitutoFinanziario -->
      <xsl:call-template name="Element_IstitutoFinanziario_Gloscad"/>

      <!-- IBAN -->
      <xsl:call-template name="Element_IBAN_Gloscad"/>

      <!-- ABI -->
      <xsl:call-template name="Element_ABI_Gloscad"/>

      <!-- CAB -->
      <xsl:call-template name="Element_CAB_Gloscad"/>

      <!-- BIC -->
      <xsl:call-template name="Element_BIC_Gloscad"/>

      <!-- ScontoPagamentoAnticipato -->
      <xsl:call-template name="Element_ScontoPagamentoAnticipato_Gloscad"/>

      <!-- DataLimitePagamentoAnticipato -->
      <xsl:call-template name="Element_DataLimitePagamentoAnticipato_Gloscad"/>

      <!-- PenalitaPagamentiRitardati -->
      <xsl:call-template name="Element_PenalitaPagamentiRitardati_Gloscad"/>

      <!-- DataDecorrenzaPenale -->
      <xsl:call-template name="Element_DataDecorrenzaPenale_Gloscad"/>

      <!-- CodicePagamento -->
      <xsl:call-template name="Element_CodicePagamento_Gloscad"/>

    </DettaglioPagamento>
    <xsl:value-of select="$newline" />

  </xsl:template>

  <!-- DatiPagamento (valore) -->
  <xsl:template name="Value_DatiPagamento_Gloscad">
    <xsl:param name="Ditta" />
    <xsl:param name="TipoDocvBase" />
    <xsl:param name="TipoDocv" />
    <xsl:param name="Esercizio" />
    <xsl:param name="Sede" />
    <xsl:param name="NumDocv" />

    <xsl:for-each select="following-sibling::QuantoDataRow[
                N_ID_DITTA = $Ditta and
                N_TIPO_DOCV_BASE = $TipoDocvBase and
                N_ID_TP_DOCV = $TipoDocv and
                C_ANNO_ESERCIZIO = $Esercizio and
                N_ID_SEDE = $Sede and
                N_NUM_DOCV = $NumDocv and 
                C_TIPO_RIGHE = 'S']">

      <!-- CondizioniPagamento -->
      <xsl:if test="position() = 1">
        <xsl:call-template name="Element_CondizioniPagamento_Gloscad"/>
      </xsl:if>

      <!-- DettaglioPagamento -->
      <xsl:call-template name="Block_DettaglioPagamento_Gloscad" />

    </xsl:for-each>

  </xsl:template>

  <!-- DatiPagamento -->
  <xsl:template name="Block_DatiPagamento_Gloscad">
    <xsl:variable name="IsConvVal">
      <xsl:call-template name="Check_ConvVal"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="$IsConvVal = 1">
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="DatiPagamento">
          <xsl:call-template name="Value_DatiPagamento_Gloscad">
            <xsl:with-param name="Ditta" select="N_ID_DITTA" />
            <xsl:with-param name="TipoDocvBase" select="N_TIPO_DOCV_BASE" />
            <xsl:with-param name="TipoDocv" select="N_ID_TP_DOCV" />
            <xsl:with-param name="Esercizio" select="C_ANNO_ESERCIZIO" />
            <xsl:with-param name="Sede" select="N_ID_SEDE" />
            <xsl:with-param name="NumDocv" select="N_NUM_DOCV" />
          </xsl:call-template>
        </xsl:variable>

        <xsl:if test="$DatiPagamento != ''">
          <DatiPagamento>
            <xsl:value-of select="$newline" />

            <!-- DatiPagamento -->
            <xsl:copy-of  select="$DatiPagamento" />

          </DatiPagamento>
          <xsl:value-of select="$newline" />
        </xsl:if>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>

  <!--:::::::::::::::::::: Allegati ::::::::::::::::::::-->

  <!-- Elementi valorizzati -->

  <!-- Blocchi -->
  <xsl:template name="Block_Allegati">
  </xsl:template>



  <!--==================================== Trasformazione ====================================-->

  <xsl:template match="/">
    <xsl:value-of select="$newline" />
    <xsl:value-of select="$newline" />

    <xsl:call-template name="Block_FatturaElettronica" />

  </xsl:template>

</xsl:stylesheet>

