<?xml version="1.0" encoding="UTF-8"?>
<!-- ===== Copyright Notice ===== -->
<!--  
 Copyright (C) Elektronički Računi d.o.o 2019. All Rights Reserved.

Elektronički Računi d.o.o  takes no position regarding the validity or scope of any 
  intellectual property or other rights that might be claimed to pertain 
  to the implementation or use of the technology described in this 
  document or the extent to which any license under such rights 
  might or might not be available; neither does it represent that it has 
  made any effort to identify any such rights. 

This document and translations of it may be copied and furnished to 
  others, in whole or in part, for the purpose of integration with
  Moj eRačun network, provided that the above copyright notice and this 
  paragraph are included on all such copies and derivative works. 
  Other usages of the document or its translations are prohibited.
  However, this document itself may not be modified in any way, 
  such as by removing the copyright notice or references to 
  OASIS 
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xmlns:n2="http://uri.etsi.org/01903/v1.4.1#"  
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
 xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
 xmlns:ihi="urn:invoice:hr:issueplace"
 xmlns:iht="urn:invoice:hr:saptypeID"
 xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
 exclude-result-prefixes="n2 xs xsi xsl ihi i iht" >
	

	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:param name="customerBusinessUnit"/>
    <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    <xsl:variable name="countryPrefixList">
        <xsl:copy-of select="document('codelist/CountryPrefixList.xml')"/>
    </xsl:variable>
    <xsl:variable name="schemeIDList" >
        <xsl:copy-of select="document('codelist/SchemeIDCodeList.xml')"/>
    </xsl:variable>
    
    <xsl:variable name="supplierCountryPrefix">
        <xsl:choose>
            <xsl:when test="normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
                <xsl:value-of select="translate(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
            </xsl:when>
            <xsl:when test="normalize-space(substring(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
                <xsl:value-of select="translate(substring(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    <xsl:variable name="customerCountryPrefix">
        <xsl:choose>
            <xsl:when test="normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode) != ''">
                <xsl:value-of select="translate(//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:IdentificationCode,$uppercase,$lowercase)"/>
            </xsl:when>
            <xsl:when test="normalize-space(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2))!= ''">
                <xsl:value-of select="translate(substring(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID, 1,2),$uppercase,$lowercase)"/>
            </xsl:when>
        </xsl:choose>
    </xsl:variable>
    
    
    <xsl:variable name="supplierCountrySchemeID">
        <xsl:choose>
            <xsl:when test="$schemeIDList//SchemeID[@ID= $supplierCountryPrefix] != ''  ">
                <xsl:value-of select="$schemeIDList//SchemeID[@ID=$supplierCountryPrefix]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    
    <xsl:variable name="customerCountrySchemeID">
        <xsl:choose>
            <xsl:when test="$schemeIDList//SchemeID[@ID= $customerCountryPrefix] != ''  ">
                <xsl:value-of select="$schemeIDList//SchemeID[@ID=$customerCountryPrefix]"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$schemeIDList//SchemeID[@ID='hr']"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    
	<xsl:variable name="SAPTypeID">
		<xsl:choose>
			<xsl:when test="(//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '15538072333' or //cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '99172175603') and //cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '27759560625'">
				<xsl:value-of select="//iht:SAPTypeID"/>
			</xsl:when>
			<xsl:otherwise></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="_defStandardnaStopaPDV">25</xsl:variable>
	
	<xsl:variable name="issueTime">
		<xsl:value-of select="/i:Invoice/cbc:IssueTime" />
	</xsl:variable>
	
	
	<xsl:variable name="invoiceID">
		<xsl:value-of select="/i:Invoice/cbc:ID" />
	</xsl:variable>
    <xsl:variable name="supplierVAT">
        <xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" />
    </xsl:variable>
    
    <xsl:variable name="isOutsideVAT">
        <xsl:choose>
            <xsl:when test="//cac:TaxCategory[normalize-space(cbc:ID/text()) != 'O']">false</xsl:when>
            <xsl:otherwise>true</xsl:otherwise>
        </xsl:choose>
        
    </xsl:variable>
    <xsl:variable name="supplierCompanyId">
        <xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" />
    </xsl:variable>
    
    <xsl:variable name="supplierRegistrationName">
        <xsl:choose>
            <xsl:when test="//cac:AccountingSupplierParty//cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(//cac:AccountingSupplierParty//cac:PartyLegalEntity/cbc:RegistrationName) != '')">
                <xsl:value-of select="normalize-space(//cac:AccountingSupplierParty//cac:PartyLegalEntity/cbc:RegistrationName)" />
            </xsl:when>
            <xsl:when test="//cac:AccountingSupplierParty//cac:PartyName/cbc:Name and (normalize-space(//cac:AccountingSupplierParty//cac:PartyName/cbc:Name) != '')">
                <xsl:value-of select="normalize-space(//cac:AccountingSupplierParty//cac:PartyName/cbc:Name)" />
            </xsl:when>
            
        </xsl:choose>
        
        
    </xsl:variable>
    <xsl:variable name="customerCompanyId">
        <xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" />
    </xsl:variable>
    
    
    <xsl:variable name="customerRegistrationName">
        <xsl:choose>
            <xsl:when test="//cac:AccountingCustomerParty//cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(//cac:AccountingCustomerParty//cac:PartyLegalEntity/cbc:RegistrationName) != '')">
                <xsl:value-of select="normalize-space(//cac:AccountingCustomerParty//cac:PartyLegalEntity/cbc:RegistrationName)" />
            </xsl:when>
            <xsl:when test="//cac:AccountingCustomerParty//cac:PartyName/cbc:Name and (normalize-space(//cac:AccountingCustomerParty//cac:PartyName/cbc:Name) != '')">
                <xsl:value-of select="normalize-space(//cac:AccountingCustomerParty//cac:PartyName/cbc:Name)" />
            </xsl:when>
            
        </xsl:choose>
    </xsl:variable>
	
	<xsl:template match="/">	
        <xsl:apply-templates select="/i:Invoice"/>
	</xsl:template>
    
    
    <xsl:template match="i:Invoice">
    	<CreditNote xmlns="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"			xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
            xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
           >
            <cbc:UBLVersionID>2.1</cbc:UBLVersionID>
            <xsl:call-template name="CustomizationID"/>
            <xsl:call-template name="ProfileID"/>
            <xsl:apply-templates select="cbc:ID" mode="FiscalNumber" />
            <xsl:apply-templates select="cbc:IssueDate" />
            <xsl:apply-templates select="cbc:IssueTime"/>
    		<xsl:apply-templates select="cbc:TaxPointDate" />
            <xsl:apply-templates select="cbc:InvoiceTypeCode" />
            <xsl:apply-templates select="cbc:IssueTime" mode="NoteIssueTime"/>
    		
            <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:AccountingContact/cbc:Note" mode="OperatorName"/>
            <xsl:apply-templates select="//cac:AccountingSupplierParty/cac:AccountingContact/cbc:Name" mode="ResponsiblePerson"/>
            <xsl:apply-templates select="cbc:Note" />
            
            <xsl:apply-templates select="cbc:DocumentCurrencyCode" />
            <xsl:apply-templates select="cbc:TaxCurrencyCode" />
            <xsl:apply-templates select="cbc:AccountingCost" />
            <xsl:apply-templates select="cbc:BuyerReference" />
            <xsl:apply-templates select="cac:InvoicePeriod" />
            
            <xsl:apply-templates select="cac:OrderReference" />
            <xsl:apply-templates select="cac:BillingReference" />
            <xsl:apply-templates select="cac:DespatchDocumentReference" />			
            <xsl:apply-templates select="cac:ReceiptDocumentReference" />
            <xsl:apply-templates select="cac:OriginatorDocumentReference" />
            <xsl:apply-templates select="cac:ContractDocumentReference" />
            <xsl:apply-templates select="ext:UBLExtensions/ext:UBLExtension[not(position()=1)]" mode="Attachment"/>
            <xsl:apply-templates select="cac:AdditionalDocumentReference" />
            <xsl:if test="normalize-space($SAPTypeID) != ''">
                <xsl:apply-templates select="cac:AdditionalDocumentReference" mode="SapTypeID" />
            </xsl:if>
            <xsl:apply-templates select="cac:AccountingSupplierParty" />
            <xsl:apply-templates select="cac:AccountingCustomerParty" />
            
            <xsl:apply-templates select="cac:PayeeParty" />
            <xsl:apply-templates select="cac:TaxRepresentativeParty" />
            
            <xsl:apply-templates select="cac:Delivery" />
            
            <xsl:apply-templates select="cac:PaymentMeans" />
            <xsl:apply-templates select="cac:PaymentTerms" />
            
            <xsl:apply-templates select="cac:AllowanceCharge" />
            
            <xsl:apply-templates select="cac:TaxTotal" />
            <xsl:apply-templates select="cac:LegalMonetaryTotal" />
            
            <xsl:apply-templates select="cac:InvoiceLine" />
            
        </CreditNote>
       
        
      
        
    </xsl:template>
    <xsl:template name="ProfileID">
        <cbc:ProfileID>
            <xsl:choose>
                <xsl:when test="/i:Invoice/cac:OrderReference/cbc:ID">P3</xsl:when>
                <xsl:when test="/i:Invoice/cac:ContractDocumentReference/cbc:ID">P2</xsl:when>
                <xsl:when test="/i:Invoice/cac:DespatchDocumentReference/cbc:ID">P7</xsl:when>
                <xsl:otherwise>P3</xsl:otherwise>
            </xsl:choose>
        </cbc:ProfileID>
    </xsl:template>
    <xsl:template name="CustomizationID">
        <cbc:CustomizationID>urn:cen.eu:en16931:2017</cbc:CustomizationID>
    </xsl:template>
    <xsl:template match="cbc:PaymentDueDate">
    	<cbc:PaymentDueDate>
            <xsl:value-of select="." />
        </cbc:PaymentDueDate>
    </xsl:template>
    <xsl:template match="cbc:IssueTime">
        <cbc:IssueTime><xsl:value-of select="."/></cbc:IssueTime>
    </xsl:template>
    <xsl:template match="cbc:InvoiceTypeCode">
        <cbc:CreditNoteTypeCode>
            <xsl:value-of select="." />
        </cbc:CreditNoteTypeCode>
        
    </xsl:template>
    <xsl:template match="cbc:IssueTime" mode="NoteIssueTime">
        <cbc:Note>
            <xsl:value-of select="concat('Vrijeme izdavanja:',.)"/>
        </cbc:Note>
    </xsl:template>
    <xsl:template match="cbc:Note" mode="OperatorName">
        <cbc:Note>
            <xsl:value-of select="concat('Oznaka operatera:',.)"/>
        </cbc:Note>
    </xsl:template>
    <xsl:template match="cbc:Name" mode="ResponsiblePerson">
        <cbc:Note>
            <xsl:value-of select="concat('Odgovorna osoba:',.)"/>
        </cbc:Note>
    </xsl:template>
    <xsl:template match="ext:UBLExtension" mode="Attachment" >
        <cac:AdditionalDocumentReference>
            <cbc:ID><xsl:value-of select="concat($invoiceID,':Vizualizacija')"/></cbc:ID>
            <cbc:DocumentType>
                <xsl:choose>
                    <xsl:when test="position() = 1">VIZUALIZACIJA IZVORNIKA RAČUNA</xsl:when>
                    <xsl:otherwise><xsl:value-of select="concat('Privitak ', position())"/></xsl:otherwise>
                </xsl:choose>
            </cbc:DocumentType>
            <cac:Attachment>
                <cbc:EmbeddedDocumentBinaryObject>
                    <xsl:attribute name="mimeCode" namespace="">application/pdf</xsl:attribute>
                    
                    <xsl:attribute name="filename" namespace="">
                        <xsl:value-of select="concat(translate($invoiceID, ':/\&amp;', '--_ '), '.PDF')"/>
                    </xsl:attribute>
                    <xsl:value-of select="./ext:ExtensionContent"/>
                </cbc:EmbeddedDocumentBinaryObject>
            </cac:Attachment>
           
        </cac:AdditionalDocumentReference>
        
    </xsl:template>

    <!--Supplier-->
    <xsl:template match="cac:AccountingSupplierParty" >
        <cac:AccountingSupplierParty>
            <xsl:apply-templates select="cac:Party" mode="AccountingSupplierParty" />
            <xsl:apply-templates select="cac:AccountingContact"/>
        </cac:AccountingSupplierParty>
        
    </xsl:template>

    <xsl:template match="cac:Party" mode="AccountingSupplierParty" >
        <cac:Party>
            <xsl:call-template name="SupplierEndpointID" />
            <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierPartyIdentification"/>
            <xsl:apply-templates select="cac:PartyName" />
            <xsl:apply-templates select="cac:PostalAddress" mode="supplierAddress" />
            <xsl:call-template name="SupplierPartyTaxScheme" />
            <xsl:apply-templates select="cac:PartyLegalEntity"/>
            <xsl:apply-templates select="cac:Contact"/>
            <xsl:apply-templates select="cac:Person"/>
        </cac:Party>
    </xsl:template>
    <xsl:template match="cac:AccountingContact">
        <cac:AccountingContact>
            <xsl:apply-templates select="cbc:Name"/>
            <xsl:apply-templates select="cbc:Note"/>
        </cac:AccountingContact>
    </xsl:template>
    <xsl:template match="cac:Contact">
        <cac:Contact>
            <xsl:apply-templates select="cbc:Name"/>
            <xsl:apply-templates select="cbc:Telephone"/>
            <xsl:apply-templates select="cbc:Telefax"/>
            <xsl:apply-templates select="cbc:ElectronicMail"/>
            <xsl:apply-templates select="cbc:Note"/>
            
        </cac:Contact>
    </xsl:template>
    <xsl:template match="cac:Person">
        <cac:Person>
            <xsl:apply-templates select="cbc:FirstName"/>
            <xsl:apply-templates select="cbc:LastName"/>
            <xsl:apply-templates select="cbc:Title"/>
            <xsl:apply-templates select="cbc:MiddleName"/>
            <xsl:apply-templates select="cbc:OtherName"/>
            <xsl:apply-templates select="cbc:NameSuffix"/>
            <xsl:apply-templates select="cbc:JobTitle"/>
            <xsl:apply-templates select="cbc:NationalityID"/>
            <xsl:apply-templates select="cbc:OrganizationDepartment"/>
        </cac:Person>
    </xsl:template>
    <xsl:template name="StreetName">
        <xsl:choose>
            <xsl:when test="cbc:StreetName and (normalize-space(cbc:StreetName) != '')">
                <cbc:StreetName>
                    <xsl:value-of select="cbc:StreetName" />
                    <xsl:if test="cbc:BuildingNumber and (normalize-space(cbc:BuildingNumber) != '')" >
                        <xsl:value-of select="concat(' ', normalize-space(cbc:BuildingNumber))" />
                    </xsl:if>
                </cbc:StreetName>
            </xsl:when>
            <xsl:when test="cbc:AddressLine and (normalize-space(cbc:AddressLine) != '')">
                <cbc:StreetName>
                    <xsl:value-of select="cbc:AddressLine" />
                </cbc:StreetName>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template name="Country">
        <xsl:param name="countryCode" select="'HR'" />
        <cac:Country>
            <xsl:choose>
                <xsl:when test="count($countryPrefixList//CountryPrefix[. = $countryCode])>0">
                    <cbc:IdentificationCode><xsl:value-of select="translate($countryCode,$lowercase,$uppercase)" /></cbc:IdentificationCode>
                </xsl:when>
                <xsl:otherwise>
                    <cbc:IdentificationCode>HR</cbc:IdentificationCode>
                </xsl:otherwise>
                
            </xsl:choose>	
        </cac:Country>
        
        
    </xsl:template>
    <xsl:template match="cac:PostalAddress" mode="supplierAddress" >
        <cac:PostalAddress>
            <!--<xsl:apply-templates select="cbc:StreetName" />-->
            <xsl:call-template name="StreetName" />
            <xsl:apply-templates select="cbc:AdditionalStreetName" />
            <xsl:apply-templates select="cbc:CityName" />
            <xsl:apply-templates select="cbc:PostalZone" />
            <xsl:apply-templates select="cbc:CountrySubentity" />
            <xsl:apply-templates select="cac:AddressLine" />
            <!--<xsl:apply-templates select="cac:Country" />-->
            <xsl:call-template name="Country">
                
                <xsl:with-param name="countryCode" select="$supplierCountryPrefix" />
            </xsl:call-template>
            
        </cac:PostalAddress>
        
    </xsl:template>
    <xsl:template match="cbc:CompanyID" mode="SupplierPartyIdentification">
        <cac:PartyIdentification>
            <cbc:ID>
                <xsl:value-of select="concat($supplierCountrySchemeID,':', $supplierVAT)"/>
                 
            </cbc:ID>
            
        </cac:PartyIdentification>
        
    </xsl:template>
    <xsl:template name="SupplierEndpointID">
        <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierEndpointID" />
    </xsl:template>
    <xsl:template match="cbc:CompanyID" mode="SupplierEndpointID" >
        <cbc:EndpointID>
            <xsl:attribute name="schemeID" namespace="">
                <xsl:value-of select="$supplierCountrySchemeID"/>
            </xsl:attribute>
            <xsl:value-of select="concat($supplierCountrySchemeID,':',$supplierVAT)"/>
        </cbc:EndpointID>
        
    </xsl:template>
    <xsl:template name="SupplierPartyTaxScheme" >
        <cac:PartyTaxScheme>
            <xsl:if test="$isOutsideVAT = 'false'">
                <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="SupplierPartyTaxID"/>
            </xsl:if>
            <cac:TaxScheme>
                <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
            
        </cac:PartyTaxScheme>
        
    </xsl:template>
    <xsl:template match="cbc:CompanyID" mode="SupplierPartyTaxID">
        <cbc:CompanyID>
            <xsl:choose>
                <xsl:when test="count($countryPrefixList//CountryPrefix[. = $supplierCountryPrefix])> 0">
                    <xsl:value-of select="."/>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="concat('HR',$supplierVAT)"/></xsl:otherwise>
            </xsl:choose>
        </cbc:CompanyID>
        
    </xsl:template>
	<xsl:template match="cac:InvoiceLine">
		<cac:CreditNoteLine>
			<xsl:apply-templates select="cbc:ID" mode="ID"/>
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="cbc:InvoicedQuantity" />
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
			<xsl:apply-templates select="cbc:AccountingCost" />
			<xsl:apply-templates select="cac:InvoicePeriod" mode="InvoiceLine" />
			<xsl:apply-templates select="cac:OrderLineReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:DespatchLineReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:DocumentReference" mode="InvoiceLine"/>
			<xsl:apply-templates select="cac:Delivery"/>
			<xsl:apply-templates select="cac:AllowanceCharge" mode="InvoiceLine" />
			
			
		

			<xsl:apply-templates select="cac:Item" mode="InvoiceLine" />
			<xsl:apply-templates select="cac:Price" mode="InvoiceLine" />
			

		</cac:CreditNoteLine>
		
	</xsl:template>	
	
	<xsl:template match="cac:Price" mode="InvoiceLine" >
		<cac:Price>
			<xsl:apply-templates select="cbc:PriceAmount" />
			<xsl:apply-templates select="cbc:BaseQuantity" />
			<xsl:apply-templates select="cac:AllowanceCharge" mode="Price" />
		</cac:Price>
	
	</xsl:template>
	
	<xsl:template match="cac:AllowanceCharge" mode="Price" >
		<cac:AllowanceCharge>
			<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
			<xsl:apply-templates select="cbc:Amount" />
			<xsl:apply-templates select="cbc:BaseAmount" />
			
		</cac:AllowanceCharge>
	
	</xsl:template>

	
	<xsl:template match="cbc:BaseQuantity" >
		<cbc:BaseQuantity>
			<xsl:attribute name="unitCode" namespace="" >
				<xsl:value-of select="@unitCode" />
			</xsl:attribute>
			
			<xsl:value-of select="number(.)" />
		</cbc:BaseQuantity>
	
	</xsl:template>

	<xsl:template match="cac:Item" mode="InvoiceLine">
		<cac:Item>
			<xsl:apply-templates select="cbc:Description" />
			<xsl:apply-templates select="cbc:PackQuantity" />
			<xsl:apply-templates select="cbc:PackSizeNumeric" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:BuyersItemIdentification" />
			<xsl:apply-templates select="cac:SellersItemIdentification" />
			<xsl:apply-templates select="cac:ManufacturersItemIdentification" />
			<xsl:apply-templates select="cac:StandardItemIdentification" />
			<xsl:apply-templates select="cac:AdditionalItemIdentification" />

			<xsl:apply-templates select="cac:OriginCountry" />
			<xsl:apply-templates select="cac:CommodityClassification" />
			
		    <xsl:call-template name="InvoiceLineTaxCategory">
		        <xsl:with-param name="currencyID" select="cbc:LineExtensionAmount/@currencyID" />
		        <xsl:with-param name="taxableAmount" select="cbc:LineExtensionAmount"/>
		        <xsl:with-param name="percent" select="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory/cbc:Percent"/>		
		        <xsl:with-param name="taxCategoryID" select="cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory/cbc:ID" />
		    </xsl:call-template>
			<xsl:apply-templates select="cac:AdditionalItemProperty[not((cbc:Name/text() = 'ManufacturersItemIdentification') or (cbc:Name/text() = 'AdditionalItemIdentification'))]" />
			
			
			
		</cac:Item>
	
	</xsl:template>
	
	<xsl:template match="cac:AdditionalItemProperty">
		<xsl:if test="normalize-space(.) != ''" >
			<cac:AdditionalItemProperty>
				<xsl:apply-templates select="cbc:Name" />
				<xsl:apply-templates select="cbc:Value" />
			
			</cac:AdditionalItemProperty>
		</xsl:if>	
	</xsl:template>
	<xsl:template match="cbc:Value">
	    <cbc:Value>
	        <xsl:choose>
	            <xsl:when test="normalize-space(.) != ''">
	                <xsl:value-of select="."/>
	            </xsl:when>
	            <xsl:otherwise>
	                <xsl:value-of select="../cbc:Name"/>
	            </xsl:otherwise>
	        </xsl:choose>
	    </cbc:Value>
	</xsl:template>
	<xsl:template match="cac:ManufacturersItemIdentification">
		<cac:ManufacturersItemIdentification>
			<cbc:ID><xsl:value-of select="cbc:Value" /></cbc:ID>
		
		</cac:ManufacturersItemIdentification>
	</xsl:template>

	<xsl:template match="cac:AdditionalItemProperty" mode="AdditionalItemIdentification">
		<cac:AdditionalItemIdentification>
			<cbc:ID><xsl:value-of select="cbc:Value" /></cbc:ID>
		
		</cac:AdditionalItemIdentification>
	</xsl:template>
	<xsl:template match="cbc:ID" mode="FiscalNumber">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '15538072333'">
					<xsl:choose>
						<xsl:when test="contains(.,'RIZ')">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="contains(../cac:AdditionalDocumentReference/cbc:ID,'RIZ')">
									<xsl:value-of select="../cac:AdditionalDocumentReference/cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '99172175603'">
					<xsl:choose>
						<xsl:when test="contains(.,'STSI')">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="contains(../cac:AdditionalDocumentReference/cbc:ID,'STSI')">
									<xsl:value-of select="../cac:AdditionalDocumentReference/cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:ID>
	</xsl:template>
	
	<xsl:template match="cac:CommodityClassification" >
		<cac:CommodityClassification>
			<xsl:apply-templates select="cbc:ItemClassificationCode" />
		</cac:CommodityClassification>
	
	</xsl:template>
	
	<xsl:template match="cbc:ItemClassificationCode" >
		<cbc:ItemClassificationCode>
			<xsl:attribute name="listID">
				<xsl:value-of select="@listID" />
			</xsl:attribute>
			<xsl:attribute name="listVersionID">
				<xsl:value-of select="@listVersionID" />
			</xsl:attribute>
			<xsl:value-of select="." />
			
		</cbc:ItemClassificationCode>
	
	</xsl:template>
	
	<xsl:template match="cac:OriginCountry" >
		<cac:OriginCountry>
			<xsl:apply-templates select="cbc:IdentificationCode" />
		</cac:OriginCountry>
	</xsl:template>
	
	<xsl:template match="cac:StandardItemIdentification | cac:AdditionalItemIdentification">
		<xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')" >
			<cac:AdditionalItemIdentification>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID" />
			</cac:AdditionalItemIdentification>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:SellersItemIdentification">
		<cac:SellersItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:SellersItemIdentification>
	</xsl:template>
	
	<xsl:template match="cac:BuyersItemIdentification">
		<cac:BuyersItemIdentification>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
		</cac:BuyersItemIdentification>
	</xsl:template>
	
	<xsl:template match="cac:AllowanceCharge" mode="InvoiceLine" >
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ChargeIndicator" />
			<xsl:apply-templates select="cbc:AllowanceChargeReasonCode" />
			<xsl:apply-templates select="cbc:AllowanceChargeReason" />
			<xsl:apply-templates select="cbc:MultiplierFactorNumeric" />
			<xsl:apply-templates select="cbc:Amount" />
			<xsl:apply-templates select="cbc:BaseAmount" />
		    
		</cac:AllowanceCharge>
			

	
	</xsl:template>
	<xsl:template match="cac:DocumentReference" mode="InvoiceLine">
		<xsl:if test="cbc:ID and (normalize-space(cbc:ID) != '')" >
			<cac:DocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID"/>
			</cac:DocumentReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:DespatchLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID-->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:DespatchLineReference>
				<xsl:apply-templates select="cbc:LineID" />
				<xsl:apply-templates select="cac:DocumentReference" mode = "InvoiceLine"/>
			</cac:DespatchLineReference>
		</xsl:if>
	</xsl:template>
	
	
	<xsl:template match="cac:OrderLineReference" mode="InvoiceLine">
	
	<!--TODO: vidjeti da li staviti neko drugo mapiranje - Vindija navodi Order/cbc:ID-->
		<xsl:if test="cbc:LineID and (normalize-space(cbc:LineID) != '')" >
			<cac:OrderLineReference>
				<xsl:apply-templates select="cbc:LineID" />
			</cac:OrderLineReference>
		</xsl:if>
	</xsl:template>
	
	<xsl:template match="cac:InvoicePeriod" mode="InvoiceLine">
		<xsl:if test="normalize-space(.) != ''" >
			<cac:InvoicePeriod>
				<xsl:apply-templates select="cbc:StartDate" />
				<xsl:apply-templates select="cbc:EndDate" />
			
			</cac:InvoicePeriod>
		</xsl:if>
	</xsl:template>

	
	<xsl:template match="cbc:InvoicedQuantity">
		<cbc:CreditedQuantity>
			<xsl:attribute name="unitCode" namespace="">
				<xsl:value-of select="@unitCode" />
			</xsl:attribute>
			<xsl:value-of select="number(.)" />
			
		</cbc:CreditedQuantity>
	</xsl:template>
	
	<xsl:template match="cac:TaxTotal">
	
		<cac:TaxTotal>
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxSubtotal" mode="TaxTotal" />
		
		</cac:TaxTotal>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:TaxSubtotal" mode="TaxTotal" >
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount" />
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxCategory" mode="TaxTotal" />
		
		</cac:TaxSubtotal>
	
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory" mode="TaxTotal">
	
		<xsl:variable name="taxExemptionReason">
			<xsl:apply-templates select="cbc:TaxExemptionReasonCode" />
			<xsl:apply-templates select="cbc:TaxExemptionReason" />		
		</xsl:variable>
		
		<xsl:call-template name="TaxCategory">
			<xsl:with-param name="taxCategoryID" select="cbc:ID" />
			<xsl:with-param name="percent" select="cbc:Percent" />	
			<xsl:with-param name="taxExemption"	select="$taxExemptionReason" />
		</xsl:call-template>
	
	
	</xsl:template>

	<xsl:template match="cac:AllowanceCharge">
		<xsl:variable name="percent"><xsl:value-of select=".//cac:TaxCategory/cbc:Percent" /></xsl:variable>
		<xsl:if test="(//i:Invoice/cac:TaxTotal//cac:TaxCategory/cbc:Percent[number(text()) = number($percent)] ) or (number(cbc:Amount) > 0)" >
			<cac:AllowanceCharge>
				<xsl:apply-templates select="cbc:ChargeIndicator" />
				<xsl:apply-templates select="cbc:AllowanceChargeReasonCode" />
				<xsl:apply-templates select="cbc:AllowanceChargeReason" />
				<xsl:apply-templates select="cbc:MultiplierFactorNumeric" />
				<xsl:apply-templates select="cbc:Amount" />
				<xsl:apply-templates select="cbc:BaseAmount" />		
				<xsl:variable name="currencyID">
					<xsl:choose>
						<xsl:when test="normalize-space(cbc:Amount/@currencyID) != ''">
							<xsl:value-of select="cbc:Amount/@currencyID" />
						</xsl:when>
						<xsl:otherwise>HRK</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>	
				<xsl:variable name="taxExemptionReason">
					<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReasonCode" />
					<xsl:apply-templates select="cac:TaxCategory/cbc:TaxExemptionReason" />		
				</xsl:variable>
				
				<xsl:call-template name="TaxCategory">
					<xsl:with-param name="taxCategoryID" select="cac:TaxCategory/cbc:ID" />
					<xsl:with-param name="percent" select="cac:TaxCategory/cbc:Percent" />	
					<xsl:with-param name="taxExemption"	select="$taxExemptionReason" />
				</xsl:call-template>
			</cac:AllowanceCharge>
		</xsl:if>
	</xsl:template>
    <xsl:template name="TaxCategory">
        <xsl:param name="taxCategoryID" />
        <xsl:param name="percent" />
        <xsl:param name="taxExemption" />
        
        <xsl:choose>
            <xsl:when test="(normalize-space($taxCategoryID) != '') and 
                (normalize-space($taxCategoryID) != 'S' ) and (normalize-space($taxCategoryID) != 'E')">
                <cac:TaxCategory>
                    <xsl:call-template name="TaxCategoryID">
                        <xsl:with-param name="taxCategoryID"><xsl:value-of select="$taxCategoryID" /></xsl:with-param>
                        
                    </xsl:call-template>
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$percent" />
                    </xsl:call-template>
                    
                    <xsl:if test="normalize-space($taxExemption) != ''" >
                        <xsl:copy-of select="$taxExemption" />
                    </xsl:if>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </xsl:when>
            <xsl:when test="normalize-space(string($percent)) != ''">
                <cac:TaxCategory>
                    <xsl:choose>
                        <xsl:when test="number($percent) = 0">
                            <xsl:call-template name="TaxCategoryID">
                                <xsl:with-param name="taxCategoryID">E</xsl:with-param>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="TaxCategoryID">
                                <xsl:with-param name="taxCategoryID">S</xsl:with-param>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$percent" />
                    </xsl:call-template>
                    
                    <xsl:if test="normalize-space($taxExemption) != ''" >
                        <xsl:copy-of select="$taxExemption" />
                    </xsl:if>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                   
                </cac:TaxCategory>
            </xsl:when>
            <xsl:otherwise>	
                <cac:TaxCategory>
                    <cbc:ID>S</cbc:ID>
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$_defStandardnaStopaPDV" />
                    </xsl:call-template>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:TaxCategory>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
    
    <xsl:template name="InvoiceLineTaxCategory">
        <xsl:param name="taxCategoryID" />
        <xsl:param name="percent" />
        <xsl:param name="taxExemption" />
        
        <xsl:choose>
            <xsl:when test="(normalize-space($taxCategoryID) != '') and 
                (normalize-space($taxCategoryID) != 'S' ) and (normalize-space($taxCategoryID) != 'E')">
                <cac:ClassifiedTaxCategory>
                    <xsl:call-template name="TaxCategoryID">
                        <xsl:with-param name="taxCategoryID"><xsl:value-of select="$taxCategoryID" /></xsl:with-param>
                        
                    </xsl:call-template>
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$percent" />
                    </xsl:call-template>
                    
                    <xsl:if test="normalize-space($taxExemption) != ''" >
                        <xsl:copy-of select="$taxExemption" />
                    </xsl:if>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:ClassifiedTaxCategory>
            </xsl:when>
            <xsl:when test="normalize-space(string($percent)) != ''">
                <cac:ClassifiedTaxCategory>
                    <xsl:choose>
                        <xsl:when test="number($percent) = 0">
                            <xsl:call-template name="TaxCategoryID">
                                <xsl:with-param name="taxCategoryID">E</xsl:with-param>
                            </xsl:call-template>
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:call-template name="TaxCategoryID">
                                <xsl:with-param name="taxCategoryID">S</xsl:with-param>
                            </xsl:call-template>
                        </xsl:otherwise>
                    </xsl:choose>
                    
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$percent" />
                    </xsl:call-template>
                    
                    <xsl:if test="normalize-space($taxExemption) != ''" >
                        <xsl:copy-of select="$taxExemption" />
                    </xsl:if>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                    
                </cac:ClassifiedTaxCategory>
            </xsl:when>
            <xsl:otherwise>	
                <cac:ClassifiedTaxCategory>
                    <cbc:ID>S</cbc:ID>
                    <xsl:call-template name="TaxPercent">
                        <xsl:with-param name="percent" select="$_defStandardnaStopaPDV" />
                    </xsl:call-template>
                    <cac:TaxScheme>
                        <cbc:ID>VAT</cbc:ID>
                    </cac:TaxScheme>
                </cac:ClassifiedTaxCategory>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
    <xsl:template name="TaxPercent" >
        <xsl:param name="percent" />
        
        <xsl:choose>
            <!--<xsl:when test="cbc:Percent and (string(number($percent)) != 'NaN')">-->
            <xsl:when test="string(number($percent)) != 'NaN'">
                
                <cbc:Percent><xsl:value-of select="number($percent)" /></cbc:Percent>
            </xsl:when>
            <xsl:otherwise>
                <cbc:Percent>0</cbc:Percent>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
    
    <xsl:template name="TaxCategoryID">
        <xsl:param name="taxCategoryID" />
        <cbc:ID>
            <xsl:choose>
                <xsl:when test="normalize-space($taxCategoryID) != ''"><xsl:value-of select="$taxCategoryID" /></xsl:when>
                <xsl:otherwise>S</xsl:otherwise>
            </xsl:choose>
            
        </cbc:ID>
    </xsl:template>
    
    
    
    
    
    
    
    
    
    <xsl:template match="cac:TaxCategory" mode="AllowanceCharge">
        <cac:TaxCategory>
            <xsl:choose>
                <xsl:when test="$isOutsideVAT = 'true'">
                    <cbc:ID>O</cbc:ID>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="cbc:ID" mode="TaxCategoryID" />
                    <xsl:apply-templates select="cbc:Percent" />
                    
                </xsl:otherwise>
            </xsl:choose>
            
            <cac:TaxScheme>
                <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
            
        </cac:TaxCategory>
        
    </xsl:template>
    <xsl:template match="cbc:ID" mode="TaxCategoryID">
        <cbc:ID>
            <xsl:choose>
                <xsl:when test="normalize-space(.) = 'O'">
                    <xsl:choose>
                        <xsl:when test="$isOutsideVAT = 'true'">O</xsl:when>
                        <xsl:otherwise>E</xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:when test="normalize-space(.) = 'B'">AE</xsl:when>
                <xsl:otherwise><xsl:value-of select="normalize-space(.)" /></xsl:otherwise>
            </xsl:choose>
            
        </cbc:ID>
        
    </xsl:template>
	
	
	<xsl:template match="cac:LegalMonetaryTotal">
		<cac:LegalMonetaryTotal>
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
			<xsl:apply-templates select="cbc:TaxExclusiveAmount" />
			<xsl:apply-templates select="cbc:TaxInclusiveAmount" />
			<!--<xsl:apply-templates select="cbc:AllowanceTotalAmount" />-->
			<xsl:call-template name="AllowanceTotalAmount" />
			<xsl:apply-templates select="cbc:ChargeTotalAmount" />
			<xsl:apply-templates select="cbc:PrepaidAmount" />
			<xsl:apply-templates select="cbc:PayableRoundingAmount" />
			<xsl:apply-templates select="cbc:PayableAmount" />
		
		</cac:LegalMonetaryTotal>
	
	</xsl:template>
	
	<xsl:template name="AllowanceTotalAmount" >
		<xsl:choose>
			<xsl:when test="cbc:AllowanceTotalAmount"><xsl:apply-templates select="cbc:AllowanceTotalAmount" /></xsl:when>
			<xsl:otherwise><cbc:AllowanceTotalAmount currencyID="HRK">0.00</cbc:AllowanceTotalAmount></xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	
	
	
	
	
	<xsl:template match="cac:PaymentTerms" >
		<cac:PaymentTerms>
			<cbc:ID><xsl:value-of select="concat('PT-', position())" /></cbc:ID>
			<cbc:PaymentMeansID>PM-001</cbc:PaymentMeansID>
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="//cac:LegalMonetaryTotal/cbc:PayableAmount" mode="PaymentTermsAmount" />
		</cac:PaymentTerms>
	
	</xsl:template>
	
	<xsl:template match="cbc:PayableAmount" mode="PaymentTermsAmount" >
		<cbc:Amount>
			<xsl:attribute name="currencyID" namespace="" >
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:value-of select="." />
		</cbc:Amount>
		
	</xsl:template>
	
	<xsl:template match="cac:PaymentMeans" >
	    <xsl:param name="InstructionID" select="cbc:InstructionID"/>
		<cac:PaymentMeans>
			<xsl:apply-templates select="cbc:PaymentMeansCode" />
			<xsl:apply-templates select="cbc:PaymentDueDate"/>
		    <xsl:apply-templates select="cbc:InstructionNote" />
			<xsl:apply-templates select="cbc:PaymentID">
			    <xsl:with-param name="InstructionID"/>
			</xsl:apply-templates>
			
			<xsl:apply-templates select="cac:PayeeFinancialAccount" mode="PaymentMeans" />
        
			
		
		</cac:PaymentMeans>
	
	</xsl:template>
	<xsl:template match="cbc:PaymentID">
	    <xsl:param name="InstructionID"/>
	    <cbc:PaymentID>
	        <xsl:choose>
	            <xsl:when test="substring($InstructionID,1,2) != 'HR'">
	                <xsl:value-of select="concat('HR',$InstructionID,' ',.)"/>
	            </xsl:when>
	            <xsl:otherwise>
	                <xsl:value-of select="concat($InstructionID,' ',.)"/>
	            </xsl:otherwise>
	        </xsl:choose>
	    </cbc:PaymentID>
	</xsl:template>

	
	<xsl:template match="cbc:DocumentCurrencyCode" mode="CurrencyCode" >
	
		<cbc:CurrencyCode>
			<xsl:value-of select="." />
		</cbc:CurrencyCode>
	</xsl:template>
	
	<xsl:template match="cac:PayeeFinancialAccount" mode="PaymentMeans">
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="/i:Invoice/cbc:DocumentCurrencyCode" mode="CurrencyCode" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:FinancialInstitutionBranch" />
		
		</cac:PayeeFinancialAccount>
	
	</xsl:template>
	

	
	<xsl:template match="cac:FinancialInstitutionBranch">
		<cac:FinancialInstitutionBranch>
			<cac:FinancialInstitution>
				<xsl:apply-templates select="cac:FinancialInstitution/cbc:ID" />
				<xsl:apply-templates select="cac:FinancialInstitution/cbc:Name" />
			</cac:FinancialInstitution>
		</cac:FinancialInstitutionBranch>
	
	</xsl:template>
	
	<xsl:template match="cac:CardAccount" >
		<cac:CardAccount>
			<xsl:apply-templates select="cbc:PrimaryAccountNumberID" />
			<xsl:apply-templates select="cbc:NetworkID" />
			<xsl:apply-templates select="cbc:HolderName" />
		
		</cac:CardAccount>
	
	</xsl:template>
	
	
	<xsl:template match="cbc:PaymentMeansCode" >
		<cbc:PaymentMeansCode >42</cbc:PaymentMeansCode>	
	</xsl:template>	
	
	
	<xsl:template match="cac:Delivery" >
		<cac:Delivery>
			<xsl:apply-templates select="cbc:ActualDeliveryDate" />
			<xsl:apply-templates select="cac:DeliveryLocation" />
			<xsl:apply-templates select="cac:DeliveryParty" />
			<xsl:apply-templates select="cac:Despatch" />
		
		</cac:Delivery>
	
	</xsl:template>
	
	<xsl:template match="cac:Despatch" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:Despatch>
				<xsl:apply-templates select="cbc:ActualDespatchDate" />
				<xsl:apply-templates select="cac:DespatchParty" />
			
			</cac:Despatch>
		</xsl:if>
	</xsl:template>

	<xsl:template match="cac:DespatchParty">
		<xsl:if test="normalize-space(.) != ''" >
			<cac:DespatchParty>
				<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
				
				<xsl:call-template name="DespatchPartyName"/>
				<xsl:apply-templates select="cac:PostalAddress" />
				<xsl:apply-templates select="cac:PhysicalLocation" />
				<!--<xsl:apply-templates select="PartyLegalEntity" mode="CompanyID" />-->
				<xsl:choose>
					<xsl:when test="(normalize-space(cac:PartyLegalEntity/cbc:RegistrationName) != '') and (normalize-space(cac:PartyLegalEntity/cbc:CompanyID) != '')">
						<xsl:apply-templates select="cac:PartyLegalEntity" mode="CompanyID" />
					</xsl:when>
					<xsl:otherwise>
							<xsl:call-template name="AccountingSupplierPartyLegalEntity" />
					</xsl:otherwise>
				</xsl:choose>

				<xsl:apply-templates select="cac:PartyIdentification" />
				
			</cac:DespatchParty>
		</xsl:if>
	
	</xsl:template>
    <xsl:template name="AccountingSupplierPartyLegalEntity" >
        <cac:PartyLegalEntity>
            <cbc:RegistrationName>
            	<xsl:value-of select="$supplierRegistrationName" />
            </cbc:RegistrationName>
            <cbc:CompanyID>
                <xsl:value-of select="$supplierCompanyId" />
            </cbc:CompanyID>
        </cac:PartyLegalEntity>
        
    </xsl:template>
	<xsl:template name="DespatchPartyName">
		<cac:PartyName>
			<cbc:Name><xsl:value-of select="//i:Invoice/cac:Delivery/cac:Despatch/cac:DespatchParty/cac:PhysicalLocation/cbc:ID"/></cbc:Name>
		</cac:PartyName>
	</xsl:template>
	
	
	
	<xsl:template match="cac:PhysicalLocation">
		<xsl:if test="normalize-space(.) != ''" >
			<cac:PhysicalLocation>
				<xsl:apply-templates select="cac:Address" />
			
			</cac:PhysicalLocation>
		</xsl:if>
	
	</xsl:template>
	
		
	<xsl:template match="cac:DeliveryLocation">
		<xsl:if test="normalize-space(.) != ''" >
			<cac:DeliveryLocation>
				<xsl:apply-templates select="cbc:ID" />
				<xsl:apply-templates select="cac:Address" />
				<xsl:apply-templates select="cac:DeliveryParty" />
			
			</cac:DeliveryLocation>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:DeliveryParty" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:DeliveryParty>
				<xsl:apply-templates select="cbc:EndpointID" mode="SchemeID"/>
				<xsl:call-template name="PartyName"/>
				<xsl:apply-templates select="cac:PostalAddress" />
				<xsl:choose>
					<xsl:when test="(normalize-space(cac:PartyLegalEntity/cbc:RegistrationName) != '') and (normalize-space(cac:PartyLegalEntity/cbc:CompanyID) != '')">
						<xsl:apply-templates select="cac:PartyLegalEntity"/>
					</xsl:when>
					<xsl:otherwise>
							<xsl:call-template name="AccountingCustomerPartyLegalEntity" />
					</xsl:otherwise>
				</xsl:choose>
				
				<xsl:apply-templates select="cac:PartyIdentification" />
	
			</cac:DeliveryParty>
		</xsl:if>
	
	</xsl:template>
    
    <xsl:template name="AccountingCustomerPartyLegalEntity" >
        <cac:PartyLegalEntity>
            
            <cbc:RegistrationName>
                
                <xsl:value-of select="$customerRegistrationName" />
            </cbc:RegistrationName>
            <cbc:CompanyID>
                <xsl:value-of select="$customerCompanyId" />
            </cbc:CompanyID>
        </cac:PartyLegalEntity>
        
    </xsl:template>
    <xsl:template name="PartyName" >
   
            <cac:PartyName>
                <cbc:Name>
                    <xsl:choose>
                        <xsl:when test="cac:PartyName/cbc:Name and (normalize-space(cac:PartyName/cbc:Name) != '')">
                            <xsl:value-of select="normalize-space(cac:PartyName/cbc:Name)" />
                        </xsl:when>
                        <xsl:when test="cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(cac:PartyLegalEntity/cbc:RegistrationName) != '')">
                            <xsl:value-of select="normalize-space(cac:PartyLegalEntity/cbc:RegistrationName)" />
                        </xsl:when>
                    </xsl:choose>
                </cbc:Name>
            </cac:PartyName>
            
        
        
        
    </xsl:template>
    
	<xsl:template match="cac:PartyLegalEntity">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>
			<xsl:apply-templates select="cbc:CompanyID"/>
		</cac:PartyLegalEntity>
		
	</xsl:template>



	<xsl:template match="cac:Address">
		<cac:Address>
			<xsl:apply-templates select="cbc:AddressFormatCode" />
			<xsl:apply-templates select="cbc:StreetName" />
			<xsl:apply-templates select="cbc:AdditionalStreetName" />
			<xsl:apply-templates select="cbc:CityName" />
			<xsl:apply-templates select="cbc:PostalZone" />
			<xsl:apply-templates select="cbc:CountrySubentity" />
			<xsl:apply-templates select="cac:AddressLine" />
			<xsl:apply-templates select="cac:Country" />
		
		</cac:Address>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:TaxRepresentativeParty" >
		<cac:TaxRepresentativeParty>
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PostalAddress" />
			<!--<xsl:apply-templates select="cac:PartyTaxScheme" mode="TaxRepresentativeParty" />-->
		
		
		</cac:TaxRepresentativeParty>
	
	
	</xsl:template>
	

	
	<xsl:template match="cac:PayeeParty" >
		<cac:PayeeParty>
			<xsl:apply-templates select="cac:PartyName[normalize-space(cbc:Name/text()) != '']" />
			<xsl:apply-templates select="cac:PartyLegalEntity" mode="CompanyID" />
			<xsl:apply-templates select="cac:PartyIdentification"  />
		
		</cac:PayeeParty>
	
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="CompanyID" >
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:CompanyID" mode="SchemeID" />
		
		</cac:PartyLegalEntity>
	
	</xsl:template>
	
	<xsl:template match="cac:PartyIdentification" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:PartyIdentification>
				<xsl:apply-templates select="cbc:ID" mode="SchemeID" />
			</cac:PartyIdentification>
		
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:AccountingCustomerParty">
		<cac:AccountingCustomerParty>
		
			<xsl:apply-templates select="cac:Party" mode="AccountingCustomerParty" />
		</cac:AccountingCustomerParty>
	
	</xsl:template>
	<xsl:template match="cac:Party" mode="AccountingCustomerParty" >
		<cac:Party>
			<xsl:choose>
				<xsl:when test="./cac:PartyLegalEntity/cbc:CompanyID = 'HU17781774'">
					<cbc:EndpointID>
						<xsl:choose>
							<xsl:when test="contains(./cac:PartyLegalEntity/cbc:RegistrationName,'PETROCHEMICALS')">6040</xsl:when>
							<xsl:otherwise>09</xsl:otherwise>
						</xsl:choose>
					</cbc:EndpointID>
				</xsl:when>
				<xsl:otherwise><xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="CustomerEndpointID"/></xsl:otherwise>
			</xsl:choose>
		    <xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="CustomerPartyIdentification" >
		        <xsl:with-param name="businessUnit" select="$customerBusinessUnit"/>
		    </xsl:apply-templates>
			<xsl:apply-templates select="cac:PartyName"/>
		    <xsl:apply-templates select="cac:PostalAddress" mode="customerAddress" />
		    <xsl:call-template name="PartyTaxScheme" />
			<xsl:apply-templates select="cac:PartyLegalEntity"/>
			<xsl:call-template name="CustomerContact"/>
			<xsl:apply-templates select="cac:Person" />
		
		</cac:Party>
	
	</xsl:template>
    <xsl:template name="CustomerContact">
        <cac:Contact>
            <cbc:ElectronicMail>adresar@moj-eracun.hr</cbc:ElectronicMail>
        </cac:Contact>
    </xsl:template>
    <xsl:template match="cac:PostalAddress" mode="customerAddress" >
        <cac:PostalAddress>
            <!--<xsl:apply-templates select="cbc:StreetName" />-->
            <xsl:call-template name="StreetName" />
            <xsl:apply-templates select="cbc:AdditionalStreetName" />
            <xsl:apply-templates select="cbc:CityName" />
            <xsl:apply-templates select="cbc:PostalZone" />
            <xsl:apply-templates select="cbc:CountrySubentity" />
            <xsl:apply-templates select="cac:AddressLine" />
            <!--<xsl:apply-templates select="cac:Country" />-->
            <xsl:call-template name="Country">
                
                <xsl:with-param name="countryCode" select="$customerCountryPrefix" />
            </xsl:call-template>
            
        </cac:PostalAddress>
        
    </xsl:template>
    <xsl:template match="cbc:CompanyID" mode="CustomerEndpointID" >
        <cbc:EndpointID>
            <xsl:attribute name="schemeID" namespace="">
                <xsl:value-of select="$customerCountrySchemeID"/>
            </xsl:attribute>
            <xsl:value-of select="." />
            
        </cbc:EndpointID>
        
    </xsl:template>
	
	<xsl:template name="PartyTaxScheme" >
		<cac:PartyTaxScheme>
		<xsl:if test="$isOutsideVAT = 'false'">
				<xsl:apply-templates select="cac:PartyLegalEntity/cbc:CompanyID" mode="PartyTaxID"/>
			</xsl:if>
		<cac:TaxScheme>
				<cbc:ID>VAT</cbc:ID>
			</cac:TaxScheme>
		
		</cac:PartyTaxScheme>
	
	</xsl:template>
    <xsl:template match="cbc:CompanyID" mode="PartyTaxID">
        <cbc:CompanyID>
            <xsl:choose>
                <xsl:when test="count($countryPrefixList//CountryPrefix[. = $customerCountryPrefix]) > 0">
                    <xsl:value-of select="."/>
                </xsl:when>
                <xsl:otherwise><xsl:value-of select="concat('HR', .)"/></xsl:otherwise>
            </xsl:choose>
            
        </cbc:CompanyID>
        
    </xsl:template>
	
	<xsl:template match="cac:PostalAddress" >
		<cac:PostalAddress>
			<xsl:apply-templates select="cbc:AddressFormatCode" />
			<xsl:apply-templates select="cbc:StreetName" />
			<xsl:apply-templates select="cbc:AdditionalStreetName" />
			<xsl:apply-templates select="cbc:CityName" />
			<xsl:apply-templates select="cbc:PostalZone" />
			<xsl:apply-templates select="cbc:CountrySubentity" />
			<xsl:apply-templates select="cac:AddressLine" />
		    <xsl:call-template name="Country">
		        
		        <xsl:with-param name="countryCode" select="cac:Country/cbc:IdentificationCode" />
		    </xsl:call-template>
			
		</cac:PostalAddress>
	
	</xsl:template>
	<xsl:template match="cac:Country" >
		<cac:Country>
			<xsl:apply-templates select="cbc:IdentificationCode" />
			<xsl:apply-templates select="cbc:Name" />
		
		</cac:Country>
	
	</xsl:template>
	
	<xsl:template match="cbc:IdentificationCode" >
		<cbc:IdentificationCode>
			<xsl:value-of select="." />
		</cbc:IdentificationCode>
	</xsl:template>
	
	<xsl:template match="cac:AddressLine">
		<xsl:if test="normalize-space(.) != ''">
			<cac:AddressLine>
				<xsl:apply-templates select="cbc:Line" />
			</cac:AddressLine>
		</xsl:if>
	</xsl:template>
	

		
	<xsl:template match="cac:PartyName" >
		<cac:PartyName>
			<xsl:choose>
				<xsl:when test="cbc:Name and (normalize-space(cbc:Name) != '')">
					<cbc:Name><xsl:value-of select="cbc:Name" /></cbc:Name>
				</xsl:when>
				<xsl:when test="../cac:PartyLegalEntity/cbc:RegistrationName and (normalize-space(../cac:PartyLegalEntity/cbc:RegistrationName) != '')">
					<cbc:Name><xsl:value-of select="../cac:PartyLegalEntity/cbc:RegistrationName" /></cbc:Name>
				
				</xsl:when>
			</xsl:choose>
		</cac:PartyName>
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:AdditionalDocumentReference">
			<cac:AdditionalDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="FiscalAdditionalDocumentReference" />
				<xsl:apply-templates select="cbc:IssueDate" />	
				<!--<xsl:apply-templates select="cbc:DocumentTypeCode" />-->
				<xsl:apply-templates select="cbc:DocumentType" />
			</cac:AdditionalDocumentReference>
		

	</xsl:template>
	
	<xsl:template match="cac:AdditionalDocumentReference" mode="SapTypeID">
		<cac:AdditionalDocumentReference>
			<cbc:ID><xsl:value-of select="$SAPTypeID"/></cbc:ID>
			<cbc:DocumentType>SAPTypeID</cbc:DocumentType>
		</cac:AdditionalDocumentReference>
		
		
	</xsl:template>
	
	
	<xsl:template match="cbc:ID" mode="FiscalAdditionalDocumentReference">
		<cbc:ID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '15538072333'">
					<xsl:choose>
						<xsl:when test="not(contains(.,'RIZ'))">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="not(contains(../../cbc:ID,'RIZ'))">
									<xsl:value-of select="../../cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID = '99172175603'">
					<xsl:choose>
						<xsl:when test="not(contains(.,'STSI'))">
							<xsl:value-of select="."/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:choose>
								<xsl:when test="not(contains(../../cbc:ID,'STSI'))">
									<xsl:value-of select="../../cbc:ID"/>
								</xsl:when>	
								<xsl:otherwise>
									<xsl:value-of select="."/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="."/>
				</xsl:otherwise>
			</xsl:choose>
		</cbc:ID>
	</xsl:template>
	
	<xsl:template match="cac:ContractDocumentReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:ContractDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />		
			</cac:ContractDocumentReference>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:OriginatorDocumentReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:OriginatorDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />		
			</cac:OriginatorDocumentReference>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:ReceiptDocumentReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:ReceiptDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />		
			</cac:ReceiptDocumentReference>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:DespatchDocumentReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:DespatchDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />		
				<xsl:choose>
					<xsl:when test="cbc:DocumentType and (normalize-space(cbc:DocumentType) != '')">
						<xsl:apply-templates select="cbc:DocumentType" />	
					</xsl:when>
					<xsl:otherwise>
						<cbc:DocumentType>351</cbc:DocumentType>
					</xsl:otherwise>
				</xsl:choose>
			</cac:DespatchDocumentReference>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:BillingReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:BillingReference>
				<xsl:apply-templates select="cac:InvoiceDocumentReference" />
			
			</cac:BillingReference>	
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:InvoiceDocumentReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />
			</cac:InvoiceDocumentReference>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cac:OrderReference" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:OrderReference>
				<xsl:apply-templates select="cbc:ID" mode="ID" />
				<xsl:apply-templates select="cbc:IssueDate" />		
			</cac:OrderReference>
		</xsl:if>
	
	</xsl:template>
	

	
	<xsl:template match="cac:InvoicePeriod" >
		<xsl:if test="normalize-space(.) != ''" >
			<cac:InvoicePeriod>
				<xsl:apply-templates select="cbc:StartDate" />
				<xsl:apply-templates select="cbc:EndDate" />
				<!--<xsl:apply-templates select="cbc:DescriptionCode" />-->
			
			</cac:InvoicePeriod>
		</xsl:if>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode">
		<cbc:DocumentCurrencyCode>
			<xsl:value-of select="." />
		</cbc:DocumentCurrencyCode>
	</xsl:template>

	
	<xsl:template match="cbc:Amount | cbc:BaseAmount | cbc:TaxAmount | cbc:TaxableAmount
		| cbc:LineExtensionAmount | cbc:TaxExclusiveAmount | cbc:TaxInclusiveAmount
		| cbc:AllowanceTotalAmount | cbc:ChargeTotalAmount | cbc:PrepaidAmount
		| cbc:PayableRoundingAmount | cbc:PayableAmount " >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:choose>
					<xsl:when test="normalize-space(@currencyID) = ''">HRK</xsl:when>
					<xsl:otherwise><xsl:value-of select="@currencyID" /></xsl:otherwise>
				</xsl:choose>
				
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise><xsl:value-of select="format-number(number(.), '#0.00')" /></xsl:otherwise>
			</xsl:choose>

			<!--<xsl:value-of select="format-number(number(.), '#.00')" />-->
		</xsl:element>
	
	</xsl:template>
	
	<xsl:template match="cbc:PriceAmount" >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:attribute name="currencyID" namespace="">
				<xsl:value-of select="@currencyID" />
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="number(.) = 0">0.00</xsl:when>
				<xsl:when test="string(number(.))='NaN'">0.00</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="priceAmount">
						<xsl:value-of select="format-number(number(.), '#0.00')" />
					</xsl:variable>
					<xsl:choose>
						<xsl:when test="string-length(string(.)) &gt; string-length(string($priceAmount))">
							<xsl:value-of select="." />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$priceAmount" />
						</xsl:otherwise>
					</xsl:choose>
				
				</xsl:otherwise>
			</xsl:choose>
			
		</xsl:element>
	
	</xsl:template>
	

	
	<xsl:template match="cbc:MultiplierFactorNumeric">
		<cbc:MultiplierFactorNumeric>
			<xsl:choose>
				<xsl:when test="string(number(.)) != 'NaN'">
					<xsl:value-of select="number(.)" />
				</xsl:when>
				<xsl:otherwise>0</xsl:otherwise>
			</xsl:choose>

		</cbc:MultiplierFactorNumeric>
	
	</xsl:template>

	<xsl:template match="cbc:Percent">
		<cbc:Percent>
			<xsl:value-of select="number(.)" />
		</cbc:Percent>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="CustomerPartyIdentification">
	    <xsl:param name="businessUnit"/>
	    <cac:PartyIdentification>
	        <cbc:ID>
	            <xsl:choose>
	                <xsl:when test="normalize-space($businessUnit) != ''">
	                    <xsl:value-of select="concat($customerCountrySchemeID,':', .,'::HR99:',$businessUnit)"/>
	                </xsl:when>
	                <xsl:otherwise><xsl:value-of select="concat($customerCountrySchemeID,':', .)"/></xsl:otherwise>
	            </xsl:choose>
	            
	            
	        </cbc:ID>
	        </cac:PartyIdentification>
	</xsl:template>
	
    <xsl:template match="cbc:CompanyID" mode="CustomerEndpointID" >
		<cbc:EndpointID>
		    <xsl:attribute name="schemeID" namespace=""><xsl:value-of select="$customerCountrySchemeID"/></xsl:attribute>
			<xsl:value-of select="." />
		
		</cbc:EndpointID>
	
	</xsl:template>
	
	<xsl:template match="cbc:EndpointID" mode="CompanyID">
	
		<cbc:CompanyID>
			<xsl:value-of select="." />
		</cbc:CompanyID>
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="PartyID2CompanyID">
		<xsl:variable name="id1"><xsl:value-of select="substring-after(., ':')" /></xsl:variable>
		<xsl:variable name="id2"><xsl:value-of select="substring-before($id1, '::HR99')" /></xsl:variable>
	
		<cbc:CompanyID>
			<xsl:choose>
				<xsl:when test="normalize-space($id2) != ''"><xsl:value-of select="$id2" /></xsl:when>
				<xsl:when test="normalize-space($id1) != ''"><xsl:value-of select="$id1" /></xsl:when>
				<xsl:otherwise><xsl:value-of select="." /></xsl:otherwise>
			</xsl:choose>
		</cbc:CompanyID>
	</xsl:template>

	<xsl:template match="cbc:CompanyID" mode="TaxID2CompanyID">
	
		<cbc:CompanyID>
			<xsl:value-of select="substring-after(., 'HR')" />
		</cbc:CompanyID>
	</xsl:template>

	
	<xsl:template match="cbc:EndpointID" mode="SchemeID">
		<cbc:EndpointID>
			<xsl:apply-templates select="@schemeID" />
			<xsl:value-of select="." />
		</cbc:EndpointID>
	</xsl:template>

	<xsl:template match="cbc:ID | cbc:CompanyID " mode="SchemeID" >
		<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:apply-templates select="@schemeID" />

			<xsl:value-of select="." />
		</xsl:element>
	</xsl:template>
	
	<xsl:template match="@schemeID" >
		<xsl:attribute name="schemeID" namespace="">
			<xsl:value-of select="." />
		</xsl:attribute>
	
	</xsl:template>
	
	<xsl:template match="cbc:ID" mode="ID">
		<cbc:ID><xsl:value-of select="." /></cbc:ID>
	</xsl:template>
	
	<xsl:template match="cbc:DocumentType" >
		<cbc:DocumentType><xsl:value-of select="." /></cbc:DocumentType>
	
	</xsl:template>


	<xsl:template match="cbc:*" >
<!--		<xsl:copy>
			<xsl:value-of select="." />
		</xsl:copy>-->
		<xsl:if test="normalize-space(.) != ''" >
			<xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
				<xsl:value-of select="." />
			</xsl:element>
		</xsl:if>
	</xsl:template>
	

</xsl:stylesheet>
