<?xml version="1.0" encoding="UTF-8"?>
<!--
  Stampa Fatture - foglio di stile UBL (MIT)
  Visualizza in HTML fatture (Invoice) e note di credito (CreditNote) UBL 2.1
  secondo PEPPOL BIS Billing 3.0 / EN 16931.
  La radice ha namespace diversi per fattura e nota di credito: si usa /* e
  local-name(); gli elementi comuni sono nei namespace cac e cbc.
-->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    exclude-result-prefixes="cac cbc">
  <xsl:output method="html" encoding="UTF-8" indent="yes"/>
  <xsl:decimal-format name="it" decimal-separator="," grouping-separator="."/>

  <xsl:variable name="nota" select="local-name(/*) = 'CreditNote'"/>
  <xsl:variable name="divisa" select="/*/cbc:DocumentCurrencyCode"/>

  <xsl:template match="/">
    <html lang="it">
      <head>
        <meta charset="UTF-8"/>
        <title>Fattura elettronica UBL</title>
        <style>
          body { font-family: system-ui, Arial, sans-serif; font-size: 13px; color: #111; margin: 24px; }
          h2 { font-size: 14px; margin: 24px 0 8px; border-bottom: 1px solid #ccc; padding-bottom: 4px; }
          .parti { display: flex; gap: 24px; }
          .parte { flex: 1; border: 1px solid #ddd; border-radius: 6px; padding: 12px; }
          .etichetta { color: #666; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
          .secondario { color: #666; font-size: 12px; }
          table { width: 100%; border-collapse: collapse; }
          th, td { border-bottom: 1px solid #eee; padding: 6px 4px; text-align: left; vertical-align: top; }
          th { background: #f5f5f5; font-size: 11px; text-transform: uppercase; }
          td.n, th.n { text-align: right; white-space: nowrap; }
          table.dati td:first-child { color: #666; width: 30%; }
          table.totali { width: auto; margin-left: auto; min-width: 45%; }
          table.totali tr.finale td { font-size: 16px; font-weight: bold; border-top: 2px solid #111; }
          @media print { body { margin: 0; } }
        </style>
      </head>
      <body>
        <xsl:call-template name="parti"/>
        <xsl:call-template name="documento"/>
        <xsl:call-template name="righe"/>
        <xsl:call-template name="sconti"/>
        <xsl:call-template name="iva"/>
        <xsl:call-template name="totali"/>
        <xsl:call-template name="pagamento"/>
        <xsl:call-template name="allegati"/>
      </body>
    </html>
  </xsl:template>

  <!-- Utilità -->

  <xsl:template name="importo">
    <xsl:param name="v"/>
    <xsl:if test="string($v) != ''">
      <xsl:value-of select="format-number($v, '#.##0,00', 'it')"/>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$divisa"/>
    </xsl:if>
  </xsl:template>

  <!-- AAAA-MM-GG → GG/MM/AAAA -->
  <xsl:template name="data">
    <xsl:param name="d"/>
    <xsl:if test="string-length($d) &gt;= 10">
      <xsl:value-of select="concat(substring($d, 9, 2), '/', substring($d, 6, 2), '/', substring($d, 1, 4))"/>
    </xsl:if>
  </xsl:template>

  <xsl:template name="unita">
    <xsl:param name="c"/>
    <xsl:choose>
      <xsl:when test="$c = 'HUR'">ore</xsl:when>
      <xsl:when test="$c = 'DAY'">giorni</xsl:when>
      <xsl:when test="$c = 'MON'">mesi</xsl:when>
      <xsl:when test="$c = 'C62' or $c = 'H87' or $c = 'EA' or $c = 'XPP'">pz</xsl:when>
      <xsl:when test="$c = 'KGM'">kg</xsl:when>
      <xsl:when test="$c = 'MTR'">m</xsl:when>
      <xsl:when test="$c = 'MTK'">m²</xsl:when>
      <xsl:when test="$c = 'MTQ'">m³</xsl:when>
      <xsl:when test="$c = 'LTR'">l</xsl:when>
      <xsl:when test="$c = 'KWH'">kWh</xsl:when>
      <xsl:when test="$c = 'LS'">a corpo</xsl:when>
      <xsl:otherwise><xsl:value-of select="$c"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name="categoria">
    <xsl:param name="c"/>
    <xsl:choose>
      <xsl:when test="$c/cbc:ID = 'S'"><xsl:value-of select="format-number($c/cbc:Percent, '0,##', 'it')"/>%</xsl:when>
      <xsl:when test="$c/cbc:ID = 'Z'">Aliquota zero</xsl:when>
      <xsl:when test="$c/cbc:ID = 'E'">Esente</xsl:when>
      <xsl:when test="$c/cbc:ID = 'AE'">Inversione contabile</xsl:when>
      <xsl:when test="$c/cbc:ID = 'K'">Intracomunitaria</xsl:when>
      <xsl:when test="$c/cbc:ID = 'G'">Esportazione</xsl:when>
      <xsl:when test="$c/cbc:ID = 'O'">Fuori campo IVA</xsl:when>
      <xsl:when test="$c/cbc:ID = 'B'">Split payment <xsl:value-of select="format-number($c/cbc:Percent, '0,##', 'it')"/>%</xsl:when>
      <xsl:otherwise><xsl:value-of select="concat($c/cbc:ID, ' ', $c/cbc:Percent)"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <!-- Fornitore e cliente -->

  <xsl:template name="soggetto">
    <xsl:param name="p"/>
    <xsl:variable name="nome" select="$p/cac:PartyLegalEntity/cbc:RegistrationName"/>
    <div><strong>
      <xsl:choose>
        <xsl:when test="$nome"><xsl:value-of select="$nome"/></xsl:when>
        <xsl:otherwise><xsl:value-of select="$p/cac:PartyName/cbc:Name"/></xsl:otherwise>
      </xsl:choose>
    </strong></div>
    <xsl:if test="$nome and $p/cac:PartyName/cbc:Name and $p/cac:PartyName/cbc:Name != $nome">
      <div class="secondario"><xsl:value-of select="$p/cac:PartyName/cbc:Name"/></div>
    </xsl:if>
    <xsl:for-each select="$p/cac:PostalAddress">
      <xsl:for-each select="cbc:StreetName | cbc:AdditionalStreetName | cac:AddressLine/cbc:Line">
        <div><xsl:value-of select="."/></div>
      </xsl:for-each>
      <div>
        <xsl:value-of select="normalize-space(concat(cbc:PostalZone, ' ', cbc:CityName))"/>
        <xsl:if test="cbc:CountrySubentity"> (<xsl:value-of select="cbc:CountrySubentity"/>)</xsl:if>
        <xsl:if test="cac:Country/cbc:IdentificationCode != 'IT'">
          <xsl:text> - </xsl:text><xsl:value-of select="cac:Country/cbc:IdentificationCode"/>
        </xsl:if>
      </div>
    </xsl:for-each>
    <xsl:for-each select="$p/cac:PartyTaxScheme[cac:TaxScheme/cbc:ID = 'VAT']/cbc:CompanyID">
      <div>P.IVA <xsl:value-of select="."/></div>
    </xsl:for-each>
    <xsl:for-each select="$p/cac:PartyLegalEntity/cbc:CompanyID">
      <div>
        <xsl:choose>
          <xsl:when test="@schemeID = '0210'">C.F. </xsl:when>
          <xsl:otherwise>Identificativo legale </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of select="."/>
      </div>
    </xsl:for-each>
    <xsl:for-each select="$p/cac:Contact[cbc:Name or cbc:ElectronicMail or cbc:Telephone]">
      <div class="secondario">
        <xsl:value-of select="cbc:Name"/>
        <xsl:if test="cbc:Name and (cbc:ElectronicMail or cbc:Telephone)"> · </xsl:if>
        <xsl:value-of select="cbc:ElectronicMail"/>
        <xsl:if test="cbc:ElectronicMail and cbc:Telephone"> · </xsl:if>
        <xsl:value-of select="cbc:Telephone"/>
      </div>
    </xsl:for-each>
    <xsl:for-each select="$p/cbc:EndpointID">
      <div class="secondario">Indirizzo PEPPOL <xsl:value-of select="concat(@schemeID, ':', .)"/></div>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="parti">
    <div class="parti">
      <div class="parte">
        <div class="etichetta">Fornitore</div>
        <xsl:call-template name="soggetto"><xsl:with-param name="p" select="/*/cac:AccountingSupplierParty/cac:Party"/></xsl:call-template>
      </div>
      <div class="parte">
        <div class="etichetta">Cliente</div>
        <xsl:call-template name="soggetto"><xsl:with-param name="p" select="/*/cac:AccountingCustomerParty/cac:Party"/></xsl:call-template>
      </div>
    </div>
  </xsl:template>

  <!-- Dati del documento -->

  <xsl:template name="documento">
    <xsl:variable name="tipo" select="/*/cbc:InvoiceTypeCode | /*/cbc:CreditNoteTypeCode"/>
    <h2>
      <xsl:choose>
        <xsl:when test="$tipo = '380'">Fattura</xsl:when>
        <xsl:when test="$tipo = '381'">Nota di credito</xsl:when>
        <xsl:when test="$tipo = '383'">Nota di debito</xsl:when>
        <xsl:when test="$tipo = '384'">Fattura rettificativa</xsl:when>
        <xsl:when test="$tipo = '386'">Fattura di acconto</xsl:when>
        <xsl:when test="$tipo = '389'">Autofattura</xsl:when>
        <xsl:when test="$tipo = '326'">Fattura parziale</xsl:when>
        <xsl:when test="$nota">Nota di credito (<xsl:value-of select="$tipo"/>)</xsl:when>
        <xsl:otherwise>Fattura (<xsl:value-of select="$tipo"/>)</xsl:otherwise>
      </xsl:choose>
      n. <xsl:value-of select="/*/cbc:ID"/>
      del <xsl:call-template name="data"><xsl:with-param name="d" select="/*/cbc:IssueDate"/></xsl:call-template>
      <xsl:if test="$divisa != 'EUR'"> (<xsl:value-of select="$divisa"/>)</xsl:if>
    </h2>
    <table class="dati">
      <xsl:for-each select="/*/cbc:DueDate | /*/cac:PaymentMeans/cbc:PaymentDueDate[1]">
        <tr><td>Scadenza</td><td><xsl:call-template name="data"><xsl:with-param name="d" select="."/></xsl:call-template></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cbc:BuyerReference">
        <tr><td>Riferimento del cliente</td><td><xsl:value-of select="."/></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:OrderReference">
        <tr><td>Ordine</td><td><xsl:value-of select="cbc:ID"/>
          <xsl:if test="cbc:SalesOrderID"> (ordine fornitore <xsl:value-of select="cbc:SalesOrderID"/>)</xsl:if></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:ContractDocumentReference">
        <tr><td>Contratto</td><td><xsl:value-of select="cbc:ID"/></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:ProjectReference">
        <tr><td>Progetto</td><td><xsl:value-of select="cbc:ID"/></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:BillingReference/cac:InvoiceDocumentReference">
        <tr><td>Fattura di riferimento</td><td>n. <xsl:value-of select="cbc:ID"/>
          <xsl:if test="cbc:IssueDate"> del <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:IssueDate"/></xsl:call-template></xsl:if></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:DespatchDocumentReference">
        <tr><td>Documento di trasporto</td><td><xsl:value-of select="cbc:ID"/></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:InvoicePeriod">
        <tr><td>Periodo</td><td>
          dal <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:StartDate"/></xsl:call-template>
          al <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:EndDate"/></xsl:call-template></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cac:Delivery">
        <tr><td>Consegna</td><td>
          <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:ActualDeliveryDate"/></xsl:call-template>
          <xsl:for-each select="cac:DeliveryLocation/cac:Address">
            <xsl:text> · </xsl:text>
            <xsl:value-of select="normalize-space(concat(cbc:StreetName, ' ', cbc:PostalZone, ' ', cbc:CityName))"/>
          </xsl:for-each></td></tr>
      </xsl:for-each>
      <xsl:for-each select="/*/cbc:AccountingCost">
        <tr><td>Centro di costo</td><td><xsl:value-of select="."/></td></tr>
      </xsl:for-each>
    </table>
    <xsl:for-each select="/*/cbc:Note"><p><xsl:value-of select="."/></p></xsl:for-each>
  </xsl:template>

  <!-- Righe -->

  <xsl:template name="righe">
    <h2>Dettaglio</h2>
    <table>
      <tr>
        <th>#</th><th>Descrizione</th><th class="n">Quantità</th><th class="n">Prezzo</th><th>IVA</th><th class="n">Importo</th>
      </tr>
      <xsl:for-each select="/*/cac:InvoiceLine | /*/cac:CreditNoteLine">
        <xsl:variable name="q" select="cbc:InvoicedQuantity | cbc:CreditedQuantity"/>
        <tr>
          <td><xsl:value-of select="cbc:ID"/></td>
          <td>
            <xsl:value-of select="cac:Item/cbc:Name"/>
            <xsl:for-each select="cac:Item/cbc:Description">
              <div class="secondario"><xsl:value-of select="."/></div>
            </xsl:for-each>
            <xsl:for-each select="cac:Item/cac:SellersItemIdentification/cbc:ID">
              <div class="secondario">Codice <xsl:value-of select="."/></div>
            </xsl:for-each>
            <xsl:for-each select="cac:InvoicePeriod">
              <div class="secondario">Periodo
                <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:StartDate"/></xsl:call-template> –
                <xsl:call-template name="data"><xsl:with-param name="d" select="cbc:EndDate"/></xsl:call-template></div>
            </xsl:for-each>
            <xsl:for-each select="cac:AllowanceCharge">
              <div class="secondario">
                <xsl:choose>
                  <xsl:when test="cbc:ChargeIndicator = 'true'">Maggiorazione </xsl:when>
                  <xsl:otherwise>Sconto </xsl:otherwise>
                </xsl:choose>
                <xsl:call-template name="importo"><xsl:with-param name="v" select="cbc:Amount"/></xsl:call-template>
                <xsl:if test="cbc:AllowanceChargeReason"> (<xsl:value-of select="cbc:AllowanceChargeReason"/>)</xsl:if>
              </div>
            </xsl:for-each>
            <xsl:for-each select="cbc:Note"><div class="secondario"><xsl:value-of select="."/></div></xsl:for-each>
          </td>
          <td class="n">
            <xsl:value-of select="format-number($q, '#.##0,####', 'it')"/>
            <xsl:text> </xsl:text>
            <xsl:call-template name="unita"><xsl:with-param name="c" select="$q/@unitCode"/></xsl:call-template>
          </td>
          <td class="n">
            <xsl:call-template name="importo"><xsl:with-param name="v" select="cac:Price/cbc:PriceAmount"/></xsl:call-template>
            <xsl:if test="cac:Price/cbc:BaseQuantity and cac:Price/cbc:BaseQuantity != 1">
              <div class="secondario">per <xsl:value-of select="cac:Price/cbc:BaseQuantity"/></div>
            </xsl:if>
          </td>
          <td><xsl:call-template name="categoria"><xsl:with-param name="c" select="cac:Item/cac:ClassifiedTaxCategory"/></xsl:call-template></td>
          <td class="n"><xsl:call-template name="importo"><xsl:with-param name="v" select="cbc:LineExtensionAmount"/></xsl:call-template></td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

  <xsl:template name="sconti">
    <xsl:if test="/*/cac:AllowanceCharge">
      <h2>Sconti e maggiorazioni sul documento</h2>
      <table>
        <tr><th>Tipo</th><th>Motivo</th><th>IVA</th><th class="n">Importo</th></tr>
        <xsl:for-each select="/*/cac:AllowanceCharge">
          <tr>
            <td>
              <xsl:choose>
                <xsl:when test="cbc:ChargeIndicator = 'true'">Maggiorazione</xsl:when>
                <xsl:otherwise>Sconto</xsl:otherwise>
              </xsl:choose>
            </td>
            <td><xsl:value-of select="cbc:AllowanceChargeReason"/></td>
            <td><xsl:call-template name="categoria"><xsl:with-param name="c" select="cac:TaxCategory"/></xsl:call-template></td>
            <td class="n">
              <xsl:if test="cbc:ChargeIndicator != 'true'">− </xsl:if>
              <xsl:call-template name="importo"><xsl:with-param name="v" select="cbc:Amount"/></xsl:call-template>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>

  <!-- IVA e totali -->

  <xsl:template name="iva">
    <xsl:variable name="st" select="/*/cac:TaxTotal/cac:TaxSubtotal"/>
    <xsl:if test="$st">
      <h2>Riepilogo IVA</h2>
      <table>
        <tr><th>Aliquota / categoria</th><th class="n">Imponibile</th><th class="n">Imposta</th><th>Motivo di esenzione</th></tr>
        <xsl:for-each select="$st">
          <tr>
            <td><xsl:call-template name="categoria"><xsl:with-param name="c" select="cac:TaxCategory"/></xsl:call-template></td>
            <td class="n"><xsl:call-template name="importo"><xsl:with-param name="v" select="cbc:TaxableAmount"/></xsl:call-template></td>
            <td class="n"><xsl:call-template name="importo"><xsl:with-param name="v" select="cbc:TaxAmount"/></xsl:call-template></td>
            <td>
              <xsl:value-of select="cac:TaxCategory/cbc:TaxExemptionReason"/>
              <xsl:if test="cac:TaxCategory/cbc:TaxExemptionReasonCode">
                <xsl:text> </xsl:text><span class="secondario">(<xsl:value-of select="cac:TaxCategory/cbc:TaxExemptionReasonCode"/>)</span>
              </xsl:if>
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>

  <xsl:template name="riga-totale">
    <xsl:param name="etichetta"/>
    <xsl:param name="v"/>
    <xsl:param name="segno" select="''"/>
    <xsl:param name="classe" select="''"/>
    <xsl:if test="string($v) != ''">
      <tr class="{$classe}">
        <td><xsl:value-of select="$etichetta"/></td>
        <td class="n"><xsl:value-of select="$segno"/><xsl:call-template name="importo"><xsl:with-param name="v" select="$v"/></xsl:call-template></td>
      </tr>
    </xsl:if>
  </xsl:template>

  <xsl:template name="totali">
    <xsl:variable name="t" select="/*/cac:LegalMonetaryTotal"/>
    <xsl:variable name="iva" select="(/*/cac:TaxTotal[cac:TaxSubtotal] | /*/cac:TaxTotal)[1]/cbc:TaxAmount"/>
    <table class="totali" style="margin-top:16px">
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Totale righe'"/><xsl:with-param name="v" select="$t/cbc:LineExtensionAmount"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Sconti'"/><xsl:with-param name="v" select="$t/cbc:AllowanceTotalAmount"/><xsl:with-param name="segno" select="'− '"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Maggiorazioni'"/><xsl:with-param name="v" select="$t/cbc:ChargeTotalAmount"/><xsl:with-param name="segno" select="'+ '"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Imponibile'"/><xsl:with-param name="v" select="$t/cbc:TaxExclusiveAmount"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'IVA'"/><xsl:with-param name="v" select="$iva"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Totale documento'"/><xsl:with-param name="v" select="$t/cbc:TaxInclusiveAmount"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Acconti già pagati'"/><xsl:with-param name="v" select="$t/cbc:PrepaidAmount"/><xsl:with-param name="segno" select="'− '"/></xsl:call-template>
      <xsl:call-template name="riga-totale"><xsl:with-param name="etichetta" select="'Arrotondamento'"/><xsl:with-param name="v" select="$t/cbc:PayableRoundingAmount"/></xsl:call-template>
      <xsl:call-template name="riga-totale">
        <xsl:with-param name="etichetta">
          <xsl:choose>
            <xsl:when test="$nota">Importo a credito</xsl:when>
            <xsl:otherwise>Da pagare</xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="v" select="$t/cbc:PayableAmount"/>
        <xsl:with-param name="classe" select="'finale'"/>
      </xsl:call-template>
    </table>
  </xsl:template>

  <!-- Pagamento e allegati -->

  <xsl:template name="pagamento">
    <xsl:if test="/*/cac:PaymentMeans or /*/cac:PaymentTerms">
      <h2>Pagamento</h2>
      <table class="dati">
        <xsl:for-each select="/*/cac:PaymentMeans">
          <xsl:variable name="c" select="cbc:PaymentMeansCode"/>
          <tr><td>Modalità</td><td>
            <xsl:choose>
              <xsl:when test="$c/@name"><xsl:value-of select="$c/@name"/></xsl:when>
              <xsl:when test="$c = '30'">Bonifico</xsl:when>
              <xsl:when test="$c = '58'">Bonifico SEPA</xsl:when>
              <xsl:when test="$c = '31' or $c = '49'">Addebito diretto</xsl:when>
              <xsl:when test="$c = '59'">Addebito diretto SEPA</xsl:when>
              <xsl:when test="$c = '42'">Versamento su conto bancario</xsl:when>
              <xsl:when test="$c = '10'">Contanti</xsl:when>
              <xsl:when test="$c = '20'">Assegno</xsl:when>
              <xsl:when test="$c = '48' or $c = '54' or $c = '55'">Carta di pagamento</xsl:when>
              <xsl:when test="$c = '57'">Accordo permanente</xsl:when>
              <xsl:when test="$c = '97'">Compensazione</xsl:when>
              <xsl:otherwise>Codice <xsl:value-of select="$c"/></xsl:otherwise>
            </xsl:choose>
          </td></tr>
          <xsl:for-each select="cbc:PaymentID">
            <tr><td>Causale / riferimento</td><td><xsl:value-of select="."/></td></tr>
          </xsl:for-each>
          <xsl:for-each select="cac:PayeeFinancialAccount">
            <tr><td>IBAN / conto</td><td><xsl:value-of select="cbc:ID"/></td></tr>
            <xsl:for-each select="cbc:Name"><tr><td>Intestatario</td><td><xsl:value-of select="."/></td></tr></xsl:for-each>
            <xsl:for-each select="cac:FinancialInstitutionBranch/cbc:ID"><tr><td>BIC</td><td><xsl:value-of select="."/></td></tr></xsl:for-each>
          </xsl:for-each>
          <xsl:for-each select="cac:PaymentMandate">
            <tr><td>Mandato</td><td><xsl:value-of select="cbc:ID"/>
              <xsl:if test="cac:PayerFinancialAccount/cbc:ID"> · conto addebitato <xsl:value-of select="cac:PayerFinancialAccount/cbc:ID"/></xsl:if></td></tr>
          </xsl:for-each>
        </xsl:for-each>
        <xsl:for-each select="/*/cac:PaymentTerms/cbc:Note">
          <tr><td>Condizioni</td><td><xsl:value-of select="."/></td></tr>
        </xsl:for-each>
      </table>
    </xsl:if>
  </xsl:template>

  <xsl:template name="allegati">
    <xsl:if test="/*/cac:AdditionalDocumentReference">
      <h2>Documenti allegati e riferimenti</h2>
      <ul>
        <xsl:for-each select="/*/cac:AdditionalDocumentReference">
          <li>
            <xsl:value-of select="cbc:ID"/>
            <xsl:if test="cbc:DocumentDescription"> - <xsl:value-of select="cbc:DocumentDescription"/></xsl:if>
            <xsl:for-each select="cac:Attachment/cbc:EmbeddedDocumentBinaryObject/@filename">
              <span class="secondario"> (file <xsl:value-of select="."/>)</span>
            </xsl:for-each>
            <xsl:for-each select="cac:Attachment/cac:ExternalReference/cbc:URI">
              <span class="secondario"> (<xsl:value-of select="."/>)</span>
            </xsl:for-each>
          </li>
        </xsl:for-each>
      </ul>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
