<?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 or Elektronički računi d.o.o., 
 -->


<!--UPDATE 2020-08-21 Filip Karmazen
->Ako postoji polje OriginatorDocumentReference, kreira se AdditionalDocumentReference sa poljima ID i IssueDate. Dodano polje DocumentType sa fiksnom vrijednošću OriginatorDocumentReference

->Ispravljeno polje PaymentId koje nije bilo ispravno zatvoreno.
-->
<!--UPDATE 2020-08-25 Filip Karmazen
->U DeliveryParty-ju, dodan je fiksni dio za PartyLegalEntity (CompanyId i RegistrationName) jer bez podataka pada na validatoru. Upisuju se podatci kupca fiksno.

->Dodatni DocumentType-ovi kod ostalih čvorova za referencu DespatchDocumentReference, ReiceptDocumentReference, ContractDocumentReference, BillingDocumentReference
-->

<xsl:stylesheet version="1.0" 

xmlns:c="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:xades="http://uri.etsi.org/01903/v1.3.2#" 
 xmlns:n2="http://uri.etsi.org/01903/v1.4.1#" 
 xmlns:ds="http://www.w3.org/2000/09/xmldsig#" 
 xmlns:xs="http://www.w3.org/2001/XMLSchema" 
 xmlns:exthr ="urn:invoice:hr:schema:xsd:CommonExtensionComponents-1"
 xmlns:ns0="http://fina.hr/eracun/erp/OutgoingInvoicesData/v3.2" 
 xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" 
 xmlns:a="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2" 
 xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
 xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
 xmlns:sig="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
 xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
 xmlns:qdt2="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" 
 xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
 xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
 xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
 xmlns:udt2="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2" 
 xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" 
 xmlns:clm5639="urn:un:unece:uncefact:codelist:specification:5639:1988" 
 xmlns:clm66411="urn:un:unece:uncefact:codelist:specification:66411:2001" 
 xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" 
 xmlns:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" 
 xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
 xmlns:extagr="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-1"
 exclude-result-prefixes="ccts-cct ds sig n2 qdt2 sac sbc udt2 xades xs xsi xsl i ns1 a c " >
	<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
	
	<xsl:variable name="noteSum">
		<xsl:variable name="sum" />
		<xsl:for-each select="/i:Invoice/cbc:Note">
			
			<xsl:choose>
				
				<xsl:when test="normalize-space(substring-before(., '#AAI#')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Vrijeme izdavanja')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Oznaka operatera')) != ''" />
				<xsl:when test="normalize-space(substring-before(., '#Odgovorna osoba')) != ''" />
				<xsl:when test="normalize-space(substring-after(., 'Vrijeme izdavanja:')) != ''" />
				<xsl:when test="normalize-space(substring-after(., 'Oznaka operatera:')) != ''" />
				<xsl:when test="normalize-space(substring-after(., 'Odgovorna osoba:')) != ''" />
				<xsl:when test="normalize-space(substring-after(., 'Operater:')) != ''" />	
				<xsl:otherwise><xsl:value-of select="concat($sum,., ';')"/></xsl:otherwise>
			</xsl:choose>
			
		</xsl:for-each>
	</xsl:variable>
	
	
	
	
	<xsl:template match="/">
		
		<OutgoingDocumentsData xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">
			<xsl:call-template name="HROutgoingDocumentsHeader" />
			<CdnDocument>
				<CreditNoteEnvelope>
					<xsl:apply-templates select="//i:Invoice" />
				</CreditNoteEnvelope>
			
			<AttachedDocumentEnvelope>
				<xsl:apply-templates select="/i:Invoice/cac:Signature"/>
				<xsl:apply-templates select="/i:Invoice/cac:AdditionalDocumentReference[cac:Attachment]" mode="Attachment" />
			</AttachedDocumentEnvelope>
			</CdnDocument>
			</OutgoingDocumentsData>
					
	</xsl:template>
	
	<xsl:template match="i:Invoice">
		
		<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
			 xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" 
			 xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" 
			 xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" 
			 xmlns:n0="urn:oasis:names:specification:ubl:schema:xsd:CommonSignatureComponents-2" 
			 xmlns:i="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" 
			 xmlns:qdt2="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDataTypes-2" 
			 xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" 
			 xmlns:sac="urn:oasis:names:specification:ubl:schema:xsd:SignatureAggregateComponents-2" 
			 xmlns:sbc="urn:oasis:names:specification:ubl:schema:xsd:SignatureBasicComponents-2" 
			 xmlns:udt2="urn:oasis:names:specification:ubl:schema:xsd:UnqualifiedDataTypes-2" 
			 xmlns:clm54217="urn:un:unece:uncefact:codelist:specification:54217:2001" 
			 xmlns:clm5639="urn:un:unece:uncefact:codelist:specification:5639:1988" 
			 xmlns:clm66411="urn:un:unece:uncefact:codelist:specification:66411:2001" 
			 xmlns:clmIANAMIMEMediaType="urn:un:unece:uncefact:codelist:specification:IANAMIMEMediaType:2003" 
			 xmlns:ccts-cct="urn:un:unece:uncefact:data:specification:CoreComponentTypeSchemaModule:2" 
			 xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" 
		>
			<xsl:apply-templates select="ext:UBLExtensions"/>
			<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
			<cbc:CustomizationID>
				<xsl:value-of select="'urn:invoice.hr:ubl-2.1-customizations:FinaInvoice'"/>
			</cbc:CustomizationID>
			<cbc:ProfileID>MojEracunInvoice</cbc:ProfileID>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<cbc:CopyIndicator>false</cbc:CopyIndicator>
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select ="cbc:IssueTime"/>
			<xsl:apply-templates select="cbc:InvoiceTypeCode"/>
			<xsl:apply-templates select="cbc:TaxPointdate"/>
			<xsl:if test="normalize-space($noteSum) != '' ">
				<cbc:Note><xsl:value-of select="$noteSum" /></cbc:Note>
			</xsl:if>
			<xsl:apply-templates select="cbc:DocumentCurrencyCode" />
			<xsl:apply-templates select ="cbc:TaxCurrencyCode"/>
			<xsl:apply-templates select="cbc:AccountingCost" />
			
			<xsl:apply-templates select="cac:InvoicePeriod" />
			
			<xsl:apply-templates select="cac:OrderReference" />
			<xsl:apply-templates select="cac:DespatchDocumentReference" />	
			<xsl:apply-templates select="cac:ReceiptDocumentReference" />
			<xsl:apply-templates select="cac:ContractDocumentReference[cbc:ID != '']" />
			<xsl:apply-templates select="cac:BillingReference" />
			<xsl:apply-templates select="cac:OriginatorDocumentReference" />
			<xsl:apply-templates select="cbc:BuyerReference" />
			<xsl:apply-templates select="cac:AccountingSupplierParty" />
			<xsl:apply-templates select="cac:AccountingCustomerParty" />
			
			<xsl:apply-templates select="cac:PayeeParty" />
			<xsl:apply-templates select="cac:BuyerCustomerParty" />
			<xsl:apply-templates select="cac:SellerSupplierParty" />
			<xsl:apply-templates select="cac:TaxRepresentativeParty" />
			
			<xsl:apply-templates select="cac:Delivery" />
			<xsl:apply-templates select="cac:DeliveryTerms" />
			<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" />
		
		</Invoice>
	
	</xsl:template>
	<xsl:template match="cac:Attachment" mode="Attachment" >
		<cac:Attachment>
			<xsl:apply-templates select="cbc:EmbeddedDocumentBinaryObject" />
		</cac:Attachment>
		
	</xsl:template>
	<xsl:template match="cac:AdditionalDocumentReference" mode="Attachment">
		<AttachedDocument xmlns="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2">
			<cbc:ID>
				<xsl:value-of select="cbc:ID" />
			</cbc:ID>
			<cbc:IssueDate>
				<xsl:value-of select="/i:Invoice/cbc:IssueDate"/>
			</cbc:IssueDate>
			<cbc:IssueTime>
				<xsl:value-of select="/i:Invoice/cbc:Time" />
			</cbc:IssueTime>
			<cbc:DocumentType>
				<xsl:value-of select="cbc:DocumentType"/>
			</cbc:DocumentType>
			<cbc:ParentDocumentID><xsl:value-of  select="/i:Invoice/cbc:ID" /></cbc:ParentDocumentID>
			<cac:SenderParty>
				<xsl:apply-templates select="/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity" />
			</cac:SenderParty>
			<cac:ReceiverParty>
				<xsl:apply-templates select="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity" />
			</cac:ReceiverParty>
			
			<xsl:apply-templates select="cac:Attachment" mode="Attachment" />
			
		</AttachedDocument>
	</xsl:template>
	<xsl:template match="cac:Signature">
		
		<AttachedDocument xmlns="urn:oasis:names:specification:ubl:schema:xsd:AttachedDocument-2">
			<cbc:ID>
				<xsl:value-of select="cbc:ID" />
			</cbc:ID>
			<cbc:IssueDate>
				<xsl:value-of select="/i:Invoice/cbc:IssueDate"/>
			</cbc:IssueDate>
			<cbc:IssueTime>
				<xsl:value-of select="/i:Invoice/cbc:IssueTime" />
			</cbc:IssueTime>
			<cbc:ParentDocumentID><xsl:value-of  select="/i:Invoice/cbc:ID" /></cbc:ParentDocumentID>
			<cac:SenderParty>
				<cac:PartyName>
					<cbc:Name><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name" /></cbc:Name>
				</cac:PartyName>
				<cac:PostalAddress>
					<cac:AddressLine>
						<cbc:Line><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line" /></cbc:Line>
					</cac:AddressLine>
					<cac:Country>
						<cbc:Name><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cac:Country/cbc:Name" /></cbc:Name>
					</cac:Country>
				</cac:PostalAddress>
				<cac:PartyLegalEntity>
					<cbc:RegistrationName><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cac:RegistrationName" /></cbc:RegistrationName>
					<cbc:CompanyID><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cac:CompanyID" /></cbc:CompanyID>
				</cac:PartyLegalEntity>
			</cac:SenderParty>
			<cac:ReceiverParty>
				<cac:PartyName>
					<cbc:Name><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyName/cbc:Name" /></cbc:Name>
				</cac:PartyName>
				<cac:PostalAddress>
					<cac:AddressLine>
						<cbc:Line><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:AddressLine/cbc:Line" /></cbc:Line>
					</cac:AddressLine>
					<cac:Country>
						<cbc:Name><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PostalAddress/cac:Country/cbc:Name" /></cbc:Name>
					</cac:Country>
				</cac:PostalAddress>
				<cac:PartyLegalEntity>
					<cbc:RegistrationName><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cac:RegistrationName" /></cbc:RegistrationName>
					<cbc:CompanyID><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cac:CompanyID" /></cbc:CompanyID>
				</cac:PartyLegalEntity>
			</cac:ReceiverParty>
			<cac:Attachment>
				<xsl:apply-templates select=".//cbc:EmbeddedDocumentBinaryObject"  />
				
			</cac:Attachment>
			
		</AttachedDocument>
		
	</xsl:template>
	
	<xsl:template match="cbc:EmbeddedDocumentBinaryObject">
		<cbc:EmbeddedDocumentBinaryObject>
			<xsl:attribute name="mimeCode" namespace="">
				<xsl:value-of select="@mimeCode"/>
			</xsl:attribute>
			
			<xsl:attribute name="filename" namespace="">
				<xsl:value-of select="translate(@filename, ':/\&amp;', '-__ ')"/>
			</xsl:attribute>
			
			<xsl:value-of select="."/>
			
		</cbc:EmbeddedDocumentBinaryObject>
		
	</xsl:template>
	<xsl:template name="HROutgoingDocumentsHeader" >
		<Header xmlns="http://ubl.moj-eracun.hr/v1.2/xsd">
			<Sender>
				<cbc:CompanyID>
					<xsl:value-of select="string(/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
				</cbc:CompanyID>
				<cbc:ElectronicMail>
					<xsl:value-of select="string(/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/>
				</cbc:ElectronicMail>
			</Sender>
			<Recipient>
				<cbc:CompanyID>
					<xsl:choose>
						<xsl:when test="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and  normalize-space(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID) != ''">
							<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID)"/>
						</xsl:when>
						<xsl:when test="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID and  normalize-space(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID) != ''">
							<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID)"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="string(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cbc:EndpointID)"/>
						</xsl:otherwise>
					</xsl:choose>
				</cbc:CompanyID>
				<cbc:ElectronicMail>
					<xsl:choose>
						<xsl:when test="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail and normalize-space(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail) != ''"><xsl:value-of select="string(/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/></xsl:when>						
						<xsl:otherwise>adresar@moj-eracun.hr</xsl:otherwise>
					</xsl:choose>
				</cbc:ElectronicMail>
			</Recipient>
			<DocumentWorkflow>
				<DocumentType>381</DocumentType>
				<SignDocument>2</SignDocument>
				<DocumentID>
					<xsl:value-of select="string(/i:Invoice/cbc:ID)"/>
				</DocumentID>
				<ResponseRequired>true</ResponseRequired>
			</DocumentWorkflow>
			
		</Header>
	</xsl:template>
	<xsl:template match="cbc:TaxCurrencyCode">
		<cbc:TaxCurrencyCode>
			<xsl:value-of select="."/>
		</cbc:TaxCurrencyCode>
	</xsl:template>
	
	
	<xsl:template match="cbc:TaxPointDate">
		<cbc:TaxPointDate>
			<xsl:value-of select="."/>
		</cbc:TaxPointDate>
	</xsl:template>
	
	
	<xsl:template match="cbc:BuyerReference">
		<cac:AdditionalDocumentReference>
			<cbc:ID><xsl:value-of select="."/></cbc:ID>
			<cbc:DocumentType>BuyerReference</cbc:DocumentType>
		</cac:AdditionalDocumentReference>
	</xsl:template>
	
	<xsl:template match="cbc:IssueTime">
		<cbc:IssueTime>
			<xsl:value-of select="."/>
		</cbc:IssueTime>
		
	</xsl:template>
	
	<xsl:template match="cbc:IssueDate">
		<cbc:IssueDate>
			<xsl:value-of select="."/>
		</cbc:IssueDate>
		
	</xsl:template>
	<xsl:template match="ext:UBLExtensions">
		<ext:UBLExtensions>
			<xsl:call-template name="UBLExtension"></xsl:call-template>
			</ext:UBLExtensions>
	</xsl:template>
	
	<xsl:template name="UBLExtension">
		<ext:UBLExtension>
			<cbc:ID>HRINVOICE1</cbc:ID>
			<cbc:Name>InvoiceIssuePlaceData</cbc:Name>
			<ext:ExtensionAgencyID>MINGORP</ext:ExtensionAgencyID>
			<ext:ExtensionAgencyName>MINGORP</ext:ExtensionAgencyName>
			<ext:ExtensionAgencyURI>MINGORP</ext:ExtensionAgencyURI>
			<ext:ExtensionURI>urn:invoice:hr:issueplace</ext:ExtensionURI>
			<ext:ExtensionReasonCode>MandatoryField</ext:ExtensionReasonCode>
			<ext:ExtensionReason>Mjesto izdavanja računa prema Pravilniku o PDV-u</ext:ExtensionReason>
			<ext:ExtensionContent>
				<ext:InvoiceIssuePlace>
					<xsl:choose>
						<xsl:when test="//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace and normalize-space(//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace) != ''">
							<xsl:value-of select="//ext:UBLExtensions/ext:UBLExtension/ext:ExtensionContent[not(extagr:AdditionalContent)]/exthr:InvoiceIssuePlace"/>
						</xsl:when>
						<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName and normalize-space(//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName)!= ''">
							<xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:CityName"/>
						</xsl:when>
						<xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PostalAddress/cbc:StreetName"/></xsl:otherwise>
					</xsl:choose>
				</ext:InvoiceIssuePlace>
			</ext:ExtensionContent>
		</ext:UBLExtension>
		<ext:UBLExtension>
			<cbc:ID>HRINVOICE1</cbc:ID>
			<cbc:Name>InvoiceIssuerData</cbc:Name>
			<ext:ExtensionAgencyID>FINA</ext:ExtensionAgencyID>
			<ext:ExtensionAgencyName>FINA</ext:ExtensionAgencyName>
			<ext:ExtensionAgencyURI>FINA</ext:ExtensionAgencyURI>
			<ext:ExtensionURI>urn:invoice:hr:issuer</ext:ExtensionURI>
			<ext:ExtensionReasonCode>MandatoryField</ext:ExtensionReasonCode>
			<ext:ExtensionReason>Podaci o izdavatelju prema Zakonu o trgovačkim društvima</ext:ExtensionReason>
			<ext:ExtensionContent>
				<ext:InvoiceIssuer>
					<xsl:choose>
						<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm"><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyLegalForm"/></xsl:when>
						<xsl:when test="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName"/></xsl:when>
						<xsl:otherwise><xsl:value-of select="//cac:AccountingSupplierParty/cac:Party/cac:PartyName/cbc:Name"/></xsl:otherwise>
					</xsl:choose>
				</ext:InvoiceIssuer>
			</ext:ExtensionContent>
		</ext:UBLExtension>
	</xsl:template>
	
	<xsl:template match="cac:InvoiceLine">
		<cac:InvoiceLine>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="cbc:InvoicedQuantity" />
			<xsl:apply-templates select="cbc:LineExtensionAmount" />
			<xsl:apply-templates select="cac:DespatchLineReference" />
			<xsl:apply-templates select="cac:ReceiptLineReference" />
			<xsl:apply-templates select="cac:DocumentReference" />
			<xsl:apply-templates select="cac:Delivery" />
			<xsl:apply-templates select="cac:AllowanceCharge" />
			<xsl:choose>
				<xsl:when test="cac:TaxTotal"><xsl:apply-templates select="cac:TaxTotal"/></xsl:when>
				<xsl:when test="cac:Item/cac:ClassifiedTaxCategory">
					<xsl:variable name="taxCategoryNode">
						<xsl:apply-templates select="cac:Item/cac:ClassifiedTaxCategory" mode="AllowanceChargeLine" />
					</xsl:variable>
					<xsl:variable name="currencyID"><xsl:value-of select="cbc:LineExtensionAmount/@currencyID" /></xsl:variable>
					<xsl:variable name="taxableAmount"><xsl:value-of select="number(cbc:LineExtensionAmount)" /></xsl:variable>
					<xsl:variable name="percent"><xsl:value-of select="number(cac:Item/cac:ClassifiedTaxCategory/cbc:Percent)" /></xsl:variable>
					<xsl:call-template name="HRTaxTotal">
						<xsl:with-param name="currencyID" select="$currencyID" />
						<xsl:with-param name="taxableAmount" select="$taxableAmount"/>
						<xsl:with-param name="percent" select="$percent"/>		
						<xsl:with-param name="taxCategoryNode" select="$taxCategoryNode" />
					</xsl:call-template>
					
				</xsl:when>
				<xsl:otherwise></xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="cac:Item" />
			<xsl:apply-templates select="cac:Price" />
		</cac:InvoiceLine>
	</xsl:template>	
	<xsl:template name="HRTaxTotal">
		<xsl:param name="currencyID" select="'HRK'" />
		<xsl:param name="taxableAmount" select="0"/>
		<xsl:param name="percent" select="0"/>
		<xsl:param name="taxCategoryNode" />
		<!--<xsl:value-of select="concat('cID', $currencyID , 'taAm', $taxableAmount, 'per', $percent)" />-->
		<cac:TaxTotal>
			<xsl:variable name="taxAmount">
				<xsl:choose>
					<xsl:when test="string(number(number($taxableAmount) * number($percent) div 100))='NaN'">0.00</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="format-number( number($taxableAmount) * number($percent) div 100, '0.00') " />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<cbc:TaxAmount>
				<xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
				<xsl:value-of select="format-number($taxAmount,'0.00')" />
			</cbc:TaxAmount>
			<cac:TaxSubtotal>
				<cbc:TaxableAmount>
					<xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
					<xsl:value-of select="$taxableAmount" />					 
				</cbc:TaxableAmount>
				<cbc:TaxAmount>
					<xsl:attribute name="currencyID"><xsl:value-of select="$currencyID" /></xsl:attribute>
					<xsl:value-of select="format-number($taxAmount,'0.00')" />
				</cbc:TaxAmount>
				<cbc:CalculationSequenceNumeric>1</cbc:CalculationSequenceNumeric>
				<!--<xsl:apply-templates select="cac:TaxCategory" mode="AllowanceChargeLine" />-->
				<xsl:copy-of select="$taxCategoryNode" />
			</cac:TaxSubtotal>
		</cac:TaxTotal>
		
	</xsl:template>
	
	<xsl:template match="cac:ClassifiedTaxCategory" mode="AllowanceChargeLine">
		<cac:TaxCategory>
			<xsl:variable name="taxCategoryID">
				<xsl:call-template name="HRTaxCategoyIDValue">
					<xsl:with-param name="taxCategoryID" select="cbc:ID" />
				</xsl:call-template>
			</xsl:variable>
			<xsl:call-template name="HRTaxCategoryID">
				<xsl:with-param name="taxCategoryID" select="$taxCategoryID" />
			</xsl:call-template>
			<!--<xsl:apply-templates select="cbc:Percent" />-->
			<xsl:choose>
				<xsl:when test="cbc:Percent and (string(number(cbc:Percent)) != 'NaN')">
					<xsl:apply-templates select="cbc:Percent" />
				</xsl:when>
				<xsl:otherwise>
					<cbc:Percent>0</cbc:Percent>
				</xsl:otherwise>
			</xsl:choose>
			
			<xsl:apply-templates select="/i:Invoice/cac:TaxTotal/cac:TaxSubtotal/cac:TaxCategory[cbc:ID/text() = $taxCategoryID]/cbc:TaxExemptionReason" />
			
			<xsl:call-template name="HRTaxScheme">
				<xsl:with-param name="percent" select="cbc:Percent" />			
			</xsl:call-template>
			
		</cac:TaxCategory>
		
	</xsl:template>
	
	<xsl:template name="HRTaxScheme">
		<xsl:param name="percent" select="0"/>
		<xsl:choose>
			<xsl:when test="number($percent) = 0">
				<cac:TaxScheme>
					<cbc:Name>FRE</cbc:Name>
					<cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">ZeroRated</cbc:TaxTypeCode>
				</cac:TaxScheme>		
			</xsl:when>
			<xsl:when test="string(number($percent)) = 'NaN'">
				<cac:TaxScheme>
					<cbc:Name>FRE</cbc:Name>
					<cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">ZeroRated</cbc:TaxTypeCode>
				</cac:TaxScheme>		
			</xsl:when>
			<xsl:otherwise>
				<cac:TaxScheme>
					<cbc:Name>VAT</cbc:Name>
					<cbc:TaxTypeCode listAgencyName="RH" listName="taxtypecode" listVersionID="1.1" listURI="http://cv.eracun.hr/ubl/os-ubl-2.0/cl/gc/taxtypecode" listSchemeURI="urn:hrubl:codelist:taxtypecode-1.1">StandardRated</cbc:TaxTypeCode>
				</cac:TaxScheme>
				
			</xsl:otherwise>
		</xsl:choose>
		
	</xsl:template>
	
	<xsl:template name="HRTaxCategoyIDValue">
		<xsl:param name="taxCategoryID" />
		<xsl:choose>
			<!--Podrška za mapping ako bude potreban -->
			<xsl:when test="normalize-space($taxCategoryID) = 'O'">O</xsl:when>
			<xsl:when test="normalize-space($taxCategoryID) = 'E'">E</xsl:when>
			<xsl:otherwise><xsl:value-of select="$taxCategoryID" /></xsl:otherwise>
		</xsl:choose>	
	</xsl:template>
	
	<xsl:template name="HRTaxCategoryID">
		<xsl:param name="taxCategoryID" />
		<cbc:ID schemeID="UN/ECE 5305" schemeAgencyID="6" schemeURI="http://www.unece.org/trade/untdid/d07a/tred/tred5305.htm">
			<xsl:value-of select="$taxCategoryID" />
		</cbc:ID>
		<cbc:Name>
			<xsl:choose>
				<xsl:when test="$taxCategoryID = 'S'">PDV</xsl:when>
				<xsl:when test="$taxCategoryID = 'O'">NEOPOREZIVO</xsl:when>
				<xsl:when test="$taxCategoryID = 'AE'">PPO</xsl:when>
				<xsl:when test="$taxCategoryID = 'E'">OSLOBOĐENO_POREZA</xsl:when>
				<xsl:when test="$taxCategoryID = 'Z'">OSLOBOĐENO_POREZA</xsl:when>
			</xsl:choose>
		</cbc:Name>
	</xsl:template>
	<xsl:template match="cac:Price" >
		<cac:Price>
			<xsl:apply-templates select="cbc:PriceAmount" />
			<xsl:apply-templates select="cbc:BaseQuantity" />
		</cac:Price>
	</xsl:template>
	<xsl:template match="cac:Item" >
		<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:AdditionalItemIdentification" />
			<xsl:apply-templates select="cac:OriginCountry" />
			<xsl:apply-templates select="cac:AdditionalItemProperty" />
			</cac:Item>
	</xsl:template>
	<xsl:template match="cac:AdditionalItemProperty" >
		<cac:AdditionalItemProperty>
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:Value" />
		</cac:AdditionalItemProperty>
	</xsl:template>
	
	<xsl:template match="cac:OriginCountry" >
		<cac:OriginCountry>
			<xsl:apply-templates select="cbc:IdentificationCode" />
			<xsl:apply-templates select="cbc:Name" />
		</cac:OriginCountry>
	</xsl:template>
	<xsl:template match="cac:BuyersItemIdentification" >
		<cac:BuyersItemIdentification>
			<xsl:apply-templates select="cbc:ID" />
		</cac:BuyersItemIdentification>
	</xsl:template>
	<xsl:template match="cac:ManufacturersItemIdentification" >
		<cac:ManufacturersItemIdentification>
			<xsl:apply-templates select="cbc:ID" />
		</cac:ManufacturersItemIdentification>
	</xsl:template>
	<xsl:template match="cac:SellersItemIdentification" >
		<cac:SellersItemIdentification>
			<xsl:apply-templates select="cbc:ID" />
		</cac:SellersItemIdentification>
	</xsl:template>
	<xsl:template match="cac:AdditionalItemIdentification" >
		<cac:AdditionalItemIdentification>
			<xsl:apply-templates select="cbc:ID" />
		</cac:AdditionalItemIdentification>
	</xsl:template>
	<xsl:template match="cbc:InvoicedQuantity" >
		<cbc:InvoicedQuantity>
			<xsl:attribute name="unitCode" namespace="" >
				<xsl:value-of select="@unitCode" />
			</xsl:attribute>
			<xsl:value-of select="."/>
		</cbc:InvoicedQuantity>
	</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="."/>
		</cbc:BaseQuantity>
	</xsl:template>
	<xsl:template match="cac:DocumentReference" >
		<cac:DocumentReference>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select="cbc:DocumentType" />
		</cac:DocumentReference>
	</xsl:template>
	
	<xsl:template match="cac:ReceiptLineReference" >
		<cac:ReceiptLineReference>
			<xsl:apply-templates select="cbc:LineID" />
		</cac:ReceiptLineReference>
	</xsl:template>
	<xsl:template match="cac:DespatchLineReference" >
		<cac:DespatchLineReference>
			<xsl:apply-templates select="cbc:LineID" />
		</cac:DespatchLineReference>
	</xsl:template>
	<xsl:template match="cac:OriginCountry" >
		<cac:OriginCountry>
			<xsl:apply-templates select="cbc:IdentificationCode" />
		</cac:OriginCountry>
	</xsl:template>
	
	<xsl:template match="cac:TaxTotal">
	
		<cac:TaxTotal>
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cac:TaxSubtotal" />
		</cac:TaxTotal>
	
	
	</xsl:template>
	
	
	
	<xsl:template match="cac:TaxSubtotal" >
		<cac:TaxSubtotal>
			<xsl:apply-templates select="cbc:TaxableAmount" />
			<xsl:apply-templates select="cbc:TaxAmount" />
			<xsl:apply-templates select="cbc:CalculationSequenceNumeric" />
			<xsl:apply-templates select="cac:TaxCategory" />
		
		</cac:TaxSubtotal>
	
	</xsl:template>
	
	<xsl:template match="cac:AllowanceCharge">
		<cac:AllowanceCharge>
			<xsl:apply-templates select="cbc:ID" />
			<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:SequenceNumber" />
			<xsl:apply-templates select="cbc:Amount" />
			<xsl:apply-templates select="cbc:BaseAmount" />
			<xsl:apply-templates select="cac:TaxCategory" />
			<xsl:apply-templates select="cac:TaxTotal" />
			</cac:AllowanceCharge>
	</xsl:template>
	
	<xsl:template match="cac:TaxCategory">
		<cac:TaxCategory>
			
			<xsl:apply-templates select="cbc:ID"  />
			<xsl:choose>
				<xsl:when test="cbc:Name">
					<xsl:apply-templates select="cbc:Name" mode="TaxCategory" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="taxcategoryid"><xsl:value-of select="./cbc:ID" /></xsl:variable>
					<xsl:call-template name="TaxCategoryNameFix">
						<xsl:with-param name="taxcategoryid" select="$taxcategoryid" />
					</xsl:call-template>
				
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates select="cbc:Percent" />
			<xsl:apply-templates select="cbc:BaseUnitMeasure" />
			<xsl:apply-templates select="cbc:PerUnitAmount" />
			<xsl:apply-templates select="cbc:TaxExemptionReasonCode" />
			<xsl:apply-templates select="cbc:TaxExemptionReason" />
			<xsl:apply-templates select="cbc:BaseAmount" />
			<xsl:apply-templates select="cac:TaxScheme" />
		</cac:TaxCategory>
	</xsl:template>
	
	<xsl:template match="cbc:Name" mode="TaxCategory">
		<cbc:Name>
			<xsl:choose>
				<xsl:when test="normalize-space(../cbc:ID) = 'S'">PDV</xsl:when>
				<xsl:when test="normalize-space(../cbc:ID) = 'Z'">NEOPOREZIVO</xsl:when>
				<xsl:when test="normalize-space(../cbc:ID) = 'E'">OSLOBOĐENO_POREZA</xsl:when>
				<xsl:otherwise>PDV</xsl:otherwise>
			</xsl:choose>
		</cbc:Name>
	</xsl:template>
	
	<xsl:template name="TaxCategoryNameFix">
		<xsl:param name="taxcategoryid"/>
		<cbc:Name>
			<xsl:choose>
				<xsl:when test="normalize-space($taxcategoryid) = 'S'">PDV</xsl:when>
				<xsl:when test="normalize-space($taxcategoryid) = 'Z'">NEOPOREZIVO</xsl:when>
				<xsl:when test="normalize-space($taxcategoryid) = 'E'">OSLOBOĐENO_POREZA</xsl:when>
				<xsl:otherwise>PDV</xsl:otherwise>
			</xsl:choose>
		</cbc:Name>
	</xsl:template>
	
	
	<xsl:template match="cac:TaxScheme">
		<cac:TaxScheme>
			<xsl:apply-templates select="cbc:ID" mode="TaxScheme"/>
			<xsl:choose>
				<xsl:when test="cbc:Name">
					<xsl:apply-templates select="cbc:Name" mode="TaxScheme" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="taxcategoryid"><xsl:value-of select="../cbc:ID" /></xsl:variable>
					<xsl:call-template name="TaxSchemeTaxTypeCodeFix">
						<xsl:with-param name="taxcategoryid" select="$taxcategoryid" />
					</xsl:call-template>
					
				</xsl:otherwise>
			</xsl:choose>
		</cac:TaxScheme>
	</xsl:template>
	
	<xsl:template name="TaxSchemeTaxTypeCodeFix">
		<xsl:param name="taxcategoryid"/>
		<cbc:TaxTypeCode>
			<xsl:choose>
				<xsl:when test="normalize-space($taxcategoryid) = 'S'">StandardRated</xsl:when>
				<xsl:when test="normalize-space($taxcategoryid) = 'Z'">ZeroRated</xsl:when>
				<xsl:when test="normalize-space($taxcategoryid) = 'E'">ZeroRated</xsl:when>
				<xsl:otherwise>StandardRated</xsl:otherwise>
			</xsl:choose>
		</cbc:TaxTypeCode>
	</xsl:template>
	
	
	
	
	
	<xsl:template match="cbc:ID" mode="TaxScheme">
		<cbc:Name>
			<xsl:choose>
				<xsl:when test="normalize-space(../../cbc:ID) = 'S'">VAT</xsl:when>
				<xsl:when test="normalize-space(../../cbc:ID) = 'Z'">FRE</xsl:when>
				<xsl:when test="normalize-space(../../cbc:ID) = 'E'">FRE</xsl:when>
				<xsl:otherwise>VAT</xsl:otherwise>
			</xsl:choose>
		</cbc:Name>
	</xsl:template>
	<xsl:template match="cbc:Name" mode="TaxScheme">
		<cbc:TaxTypeCode>
			<xsl:choose>
				<xsl:when test="normalize-space(../../cbc:ID) = 'S'">StandardRated</xsl:when>
				<xsl:when test="normalize-space(../../cbc:ID) = 'Z'">ZeroRated</xsl:when>
				<xsl:when test="normalize-space(../../cbc:ID) = 'E'">ZeroRated</xsl:when>
				<xsl:otherwise>StandardRated</xsl:otherwise>
			</xsl:choose>
		</cbc:TaxTypeCode>
	</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: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 match="cac:PaymentTerms" >
		<cac:PaymentTerms>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:PaymentMeansID" />
			<xsl:apply-templates select="cbc:PrepaidPaymentReferenceID" />
			<xsl:apply-templates select="cbc:Note" />
			<xsl:apply-templates select="cbc:SettlementDiscountPercent" />
			<xsl:apply-templates select="cbc:PenaltySurchargePercent" />
			<xsl:apply-templates select="cbc:Amount" />
		</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="cbc:DueDate" >
		<cbc:PaymentDueDate><xsl:value-of select="."/></cbc:PaymentDueDate>
		
	</xsl:template>
	<xsl:template match="cac:PaymentMeans" >
		<cac:PaymentMeans>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:PaymentMeansCode" />
			<xsl:choose>
				<xsl:when test="../cbc:DueDate and ../cbc:DueDate != ''">
					<xsl:apply-templates select="../cbc:DueDate"/>
				</xsl:when>
				<xsl:otherwise><xsl:apply-templates select="cbc:PaymentDueDate"/></xsl:otherwise>
			</xsl:choose>
			<xsl:call-template name="PaymentChannelCode" />
			<xsl:apply-templates select="cbc:PaymentID" mode="InstructionID"/>
			<xsl:apply-templates select="cbc:InstructionNote" />
			<xsl:apply-templates select="cbc:InstructionID" mode="PaymentID"/>
			<xsl:apply-templates select="cac:PayerFinancialAccount"/>
			<xsl:apply-templates select="cac:PayeeFinancialAccount" />
			
		
		</cac:PaymentMeans>
	
	</xsl:template>
	
	<xsl:template name="PaymentChannelCode">
		<cbc:PaymentChannelCode>
			<xsl:choose>
				<xsl:when test="/i:invoice/cac:PaymentMeans/cbc:PaymentChannelCode and normalize-space(/i:invoice/cac:PaymentMeans/cbc:PaymentChannelCode)">
					<xsl:value-of select="/i:invoice/cac:PaymentMeans/cbc:PaymentChannelCode"/>
				</xsl:when>
				<xsl:otherwise>IBAN</xsl:otherwise>
			</xsl:choose>
			
		</cbc:PaymentChannelCode>
	</xsl:template>
	
	<xsl:template match="cbc:InstructionID" mode="PaymentID" >
		<cbc:PaymentID><xsl:value-of select="concat('HR',.)"/></cbc:PaymentID>
		
	</xsl:template>
	<xsl:template match="cbc:PaymentID" mode="InstructionID" >
		<cbc:InstructionID><xsl:value-of select="."/></cbc:InstructionID>
		
	</xsl:template>
	
	
	
	<xsl:template match="cac:PayeeFinancialAccount" >
		<cac:PayeeFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:CurrencyCode" />
			<xsl:apply-templates select="cbc:PaymentNote" />
			<xsl:apply-templates select="cac:FinancialInstitutionBranch" />
			<xsl:apply-templates select="cac:Country" />
		</cac:PayeeFinancialAccount>
		
	</xsl:template>
	<xsl:template match="cac:PayerFinancialAccount" >
		<cac:PayerFinancialAccount>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:CurrencyCode" />
			<xsl:apply-templates select="cbc:PaymentNote" />
			<xsl:apply-templates select="cac:FinancialInstitutionBranch" />
			<xsl:apply-templates select="cac:Country" />
		</cac:PayerFinancialAccount>
		
	</xsl:template>
	<xsl:template match="cac:FinancialInstitutionBranch" >
		<cac:FinancialInstitutionBranch>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cac:FinancialInstitution" />
			<xsl:apply-templates select="cac:Address" />
			
			
		</cac:FinancialInstitutionBranch>	
		
	</xsl:template>
	<xsl:template match="cac:FinancialInstitution" >
		<cac:FinancialInstitution>
			<xsl:apply-templates select="cbc:ID" />
			<xsl:apply-templates select="cbc:Name" />
			<xsl:apply-templates select="cbc:Address" />
		</cac:FinancialInstitution>	
		
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode" mode="CurrencyCode" >		<cbc:CurrencyCode listID="ISO 4217 Alpha" listAgencyID="5" listURI="http://docs.oasis-open.org/ubl/os-UBL-2.1/cl/gc/default/CurrencyCode-2.1.gc">
			<xsl:value-of select="." />
		</cbc:CurrencyCode>
	</xsl:template>
	
	<xsl:template match="cac:DeliveryTerms" >
		<cac:DeliveryTerms>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cbc:SpecialTerms"/>
			<xsl:apply-templates select="cac:DeliveryLocation"/>
			<xsl:apply-templates select="cac:AllowanceCharge"/>
			
		</cac:DeliveryTerms>
		
	</xsl:template>
	<xsl:template match="cac:DeliveryLocation">
		<cac:DeliveryLocation>
			<xsl:apply-templates select="cac:Address"/>
		</cac:DeliveryLocation>
	</xsl:template>
	<xsl:template match="cac:Address">
		<cac:Address>
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:BuildingNumber"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cbc:CountrySubentityCode"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<xsl:apply-templates select="cac:Country"/>
		</cac:Address>
	</xsl:template>
	
	<xsl:template match="cac:Delivery" >
		<cac:Delivery>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cbc:Quantity"/>
			<xsl:apply-templates select="cbc:MinimumQuantity"/>
			<xsl:apply-templates select="cbc:MaximumQuantity"/>
			<xsl:apply-templates select="cbc:ActualDeliveryDate"/>
			<xsl:apply-templates select="cbc:ActualDeliveryTime"/>
			<xsl:apply-templates select="cac:DeliveryAddress"/>
			<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">
		<cac:Despatch>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cbc:ActualDespatchDate"/>
			<xsl:apply-templates select="cac:ActualDespatchTime"/>
			<xsl:apply-templates select="cac:DespatchAddress"/>
			<xsl:apply-templates select="cac:DespatchParty"/>
		</cac:Despatch>
	</xsl:template>
	<xsl:template match="cac:DespatchParty">
		<cac:DespatchParty>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyLegalEntity"/>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		</cac:DespatchParty>
	</xsl:template>
	<xsl:template match="cac:DeliveryParty">
		<cac:DeliveryParty>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<cac:PartyLegalEntity>
				<cbc:RegistrationName><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:RegistrationName" /></cbc:RegistrationName>
				<cbc:CompanyID><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID" /></cbc:CompanyID>
			</cac:PartyLegalEntity>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		</cac:DeliveryParty>
	</xsl:template>
	<xsl:template match="cac:DeliveryAddress">
		<cac:DeliveryAddress>
		<xsl:apply-templates select="cbc:StreetName"/>
		<xsl:apply-templates select="cbc:BuildingNumber"/>
		<xsl:apply-templates select="cbc:CityName"/>
		<xsl:apply-templates select="cbc:PostalZone"/>
		<xsl:apply-templates select="cbc:CountrySubentity"/>
		<xsl:apply-templates select="cbc:CountrySubentityCode"/>
		<xsl:apply-templates select="cac:AddressLine"/>
		<xsl:apply-templates select="cac:Country"/>
		</cac:DeliveryAddress>
	</xsl:template>
	<xsl:template match="cac:DespatchAddress">
		<cac:DespatchAddress>
			<xsl:apply-templates select="cbc:StreetName"/>
			<xsl:apply-templates select="cbc:BuildingNumber"/>
			<xsl:apply-templates select="cbc:CityName"/>
			<xsl:apply-templates select="cbc:PostalZone"/>
			<xsl:apply-templates select="cbc:CountrySubentity"/>
			<xsl:apply-templates select="cbc:CountrySubentityCode"/>
			<xsl:apply-templates select="cac:AddressLine"/>
			<xsl:apply-templates select="cac:Country"/>
		</cac:DespatchAddress>
	</xsl:template>
	<xsl:template match="cac:TaxRepresentativeParty" >
		<cac:TaxRepresentativeParty>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyLegalEntity"/>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		
		
		</cac:TaxRepresentativeParty>
	
	
	</xsl:template>
	<xsl:template match="cac:SellerSupplierParty" >
		<cac:SellerSupplierParty>
			<xsl:apply-templates select="cbc:CustomerAssignedAccountID"/>
			<xsl:apply-templates select ="cac:Party"/>
			<xsl:apply-templates select ="cac:DespatchContact"/>
			<xsl:apply-templates select ="cac:AccountingContact"/>
			<xsl:apply-templates select ="cac:SellerContact"/>
			
		</cac:SellerSupplierParty>
		
	</xsl:template>
	<xsl:template match="cac:BuyerCustomerParty" >
		<cac:BuyerCustomerParty>
			<xsl:apply-templates select="cbc:CustomerAssignedAccountID"/>
			<xsl:apply-templates select="cbc:SupplierAssignedAccountID"/>
			<xsl:apply-templates select ="cac:Party"/>
			<xsl:apply-templates select ="cac:DeliveryContact"/>
			<xsl:apply-templates select ="cac:AccountingContact"/>
			<xsl:apply-templates select ="cac:BuyerContact"/>
			
		</cac:BuyerCustomerParty>
		
	</xsl:template>
	
	
	<xsl:template match="cac:PayeeParty" >
		<cac:PayeeParty>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyLegalEntity"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		
		</cac:PayeeParty>
	
	</xsl:template>
	
	<xsl:template match="cac:AccountingCustomerParty">
		<cac:AccountingCustomerParty>
			<xsl:apply-templates select="cbc:CustomerAssignedAccountID"/>
			<xsl:apply-templates select ="cac:Party" mode="CustomerParty"/>
			<xsl:apply-templates select ="cac:DespatchContact"/>
			<xsl:call-template name="CustomerAccountingContact"/>
			<xsl:apply-templates select="cac:SellerContact"/>
		</cac:AccountingCustomerParty>
	
	</xsl:template>
	
	<xsl:template name="CustomerAccountingContact" >
		<cac:AccountingContact>
			<xsl:apply-templates select="cbc:Name"/>
			<xsl:apply-templates select="cbc:Telephone"/>
			<xsl:apply-templates select="cbc:Telefax"/>
			<xsl:call-template name="ElectronicMail"/>
			<xsl:apply-templates select="cbc:Note"/>
		</cac:AccountingContact>
	</xsl:template>
	
	<xsl:template name="ElectronicMail">
		<cbc:ElectronicMail>
			<xsl:choose>
				<xsl:when test="/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail and normalize-space(/i:Invoice/cac:AccountingCustomerParty/cac:Party/cac:Contact/cbc:ElectronicMail) != ''"><xsl:value-of select="string(/i:Invoice/cac:AccountingSupplierParty/cac:Party/cac:Contact/cbc:ElectronicMail)"/></xsl:when>						
				<xsl:otherwise>adresar@moj-eracun.hr</xsl:otherwise>
			</xsl:choose>
		</cbc:ElectronicMail>				
	</xsl:template>
		
	<xsl:template match="cac:AccountingSupplierParty" >
		<cac:AccountingSupplierParty>
			<xsl:apply-templates select="cbc:CustomerAssignedAccountID"/>
			<xsl:apply-templates select ="cac:Party"/>
			<xsl:apply-templates select ="cac:DespatchContact"/>
			<xsl:apply-templates select ="cac:AccountingContact"/>
			<xsl:apply-templates select="cac:SellerContact"/>
		</cac:AccountingSupplierParty>
	
	</xsl:template>
	<xsl:template match="cac:AccountingContact" >
		<cac:AccountingContact>
			<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:AccountingContact>
	</xsl:template>
	
	<xsl:template match="cac:DespatchContact" >
		<cac:DespatchContact>
			<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:DespatchContact>
	</xsl:template>
	
	<xsl:template match="cac:BuyerContact" >
		<cac:BuyerContact>
			<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:BuyerContact>
	</xsl:template>
	
	<xsl:template match="cbc:CustomerAssignedAccountID" >
		<cbc:CustomerAssignedAccountID>
			<xsl:value-of select="."/>
		</cbc:CustomerAssignedAccountID>
	</xsl:template>
	
	<xsl:template match="cac:Party" >
		<cac:Party>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyLegalEntity"/>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		</cac:Party>
	</xsl:template>
	<xsl:template match="cac:Party" mode="CustomerParty" >
		<cac:Party>
			<xsl:apply-templates select="cbc:WebsiteURI"/>
			<xsl:apply-templates select="cbc:EndpointID"/>
			<xsl:apply-templates select ="cac:PartyName"/>
			<xsl:apply-templates select="cac:PostalAddress"/>
			<xsl:apply-templates select="cac:PartyLegalEntity" mode="CustomerParty"/>
			<xsl:apply-templates select="cac:PartyIdentification"/>
			<xsl:apply-templates select="cac:Contact"/>
			<xsl:apply-templates select="cac:Person"/>
			<xsl:apply-templates select="cac:ServiceProviderParty"/>
		</cac:Party>
	</xsl:template>
	<xsl:template match="cac:ServiceProviderParty" >
		<cac:ServiceProviderParty>
			<xsl:apply-templates select="cbc:ID"/>
			<xsl:apply-templates select="cbc:ServiceTypeCode"/>
			<xsl:apply-templates select="cbc:ServiceType"/>
			<xsl:apply-templates select="cac:Party"/>
			<xsl:apply-templates select="cac:SellerContact"/>
		</cac:ServiceProviderParty>
	</xsl:template>
	<xsl:template match="cac:SellerContact" >
		<cac:SellerContact>
			<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:SellerContact>
	</xsl:template>
	<xsl:template match="cac:DeliveryContact" >
		<cac:DeliveryContact>
			<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:DeliveryContact>
	</xsl:template>
	<xsl:template match="cac:Person" >
		<cac:Person>
			<xsl:apply-templates select="cbc:FirstName"/>
			<xsl:apply-templates select="cbc:FamilyName"/>
			<xsl:apply-templates select="cbc:JobTitle"/>
			<xsl:apply-templates select="cbc:OrganizationDepartment"/>
		</cac:Person>
	</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:PartyIdentification" >
		<cac:PartyIdentification>
			<xsl:apply-templates select ="cbc:ID" mode="ID"/>
		</cac:PartyIdentification>
	</xsl:template>

	<xsl:template match="cac:PartyName" >
		<cac:PartyName>
			<xsl:apply-templates select="cbc:Name"/>
		</cac:PartyName>
	</xsl:template>
	<xsl:template match="cbc:Name">
		<cbc:Name><xsl:value-of select="."/></cbc:Name>
	</xsl:template>
	<xsl:template match="cac:PostalAddress">
		<cac:PostalAddress>
		<xsl:apply-templates select="cbc:StreetName"/>
		<xsl:apply-templates select="cbc:BuildingNumber"/>
		<xsl:apply-templates select="cbc:CityName"/>
		<xsl:apply-templates select="cbc:PostalZone"/>
		<xsl:apply-templates select="cbc:CountrySubentity"/>
		<xsl:apply-templates select="cbc:CountrySubentityCode"/>
			<xsl:choose>
				<xsl:when test="cac:AddressLine/cbc:Line and normalize-space(cac:AddressLine/cbc:Line) != ''">
					<xsl:apply-templates select="cac:AddressLine"></xsl:apply-templates>
				</xsl:when>
				<xsl:otherwise>
					<cac:AddressLine>
						<cbc:Line><xsl:value-of select="concat(cbc:StreetName, cbc:BuildingNumber, cbc:PostalZone, cbc:CityName) "/></cbc:Line>
						
					</cac:AddressLine>
					
				</xsl:otherwise>
			</xsl:choose>
		<xsl:apply-templates select="cac:Country"/>
		</cac:PostalAddress>
	</xsl:template>
	<xsl:template match="cac:AddressLine">
		<cac:AddressLine>
			<xsl:apply-templates select="cbc:Line"/>
		</cac:AddressLine>
	</xsl:template>
	<xsl:template match="cbc:Line">
		<cbc:Line><xsl:value-of select="."/></cbc:Line>
	</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="cac:PartyLegalEntity">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>	
			<xsl:apply-templates select="cbc:CompanyID"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	
	<xsl:template match="cac:PartyLegalEntity" mode="CustomerParty">
		<cac:PartyLegalEntity>
			<xsl:apply-templates select="cbc:RegistrationName"/>
			<xsl:call-template name="CustomerCompanyID"/>
		</cac:PartyLegalEntity>
	</xsl:template>
	
	<xsl:template name="CustomerCompanyID" >
		<cbc:CompanyID>
			<xsl:choose>
				<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID) != ''">
					<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyLegalEntity/cbc:CompanyID"/>
				</xsl:when>
				<xsl:when test="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID and normalize-space(//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID) != ''">
					<xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cac:PartyIdentification/cbc:ID"/>
				</xsl:when>
				<xsl:otherwise><xsl:value-of select="//cac:AccountingCustomerParty/cac:Party/cbc:EndpointID"/></xsl:otherwise>
			</xsl:choose>
		</cbc:CompanyID>
		
	</xsl:template>
	
	
	<xsl:template match="cac:ContractDocumentReference" >
		<cac:ContractDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<cbc:DocumentType>ContractDocumentReference</cbc:DocumentType>
		</cac:ContractDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:OriginatorDocumentReference" >
		<cac:AdditionalDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<cbc:DocumentType>OriginatorDocumentReference</cbc:DocumentType>
		</cac:AdditionalDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:ReceiptDocumentReference" >
		<cac:ReceiptDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<cbc:DocumentType>ReceiptDocumentReference</cbc:DocumentType>
		</cac:ReceiptDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:DespatchDocumentReference" >
		<cac:DespatchDocumentReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<cbc:DocumentType>DespatchDocumentReference</cbc:DocumentType>
		</cac:DespatchDocumentReference>
	
	</xsl:template>
	
	<xsl:template match="cac:BillingReference" >
		<cac:AdditionalDocumentReference>
			<cbc:ID><xsl:value-of select="." /></cbc:ID>
			<cbc:DocumentType>BillingReference</cbc:DocumentType>
		</cac:AdditionalDocumentReference>	
	
	</xsl:template>
	
	<xsl:template match="cac:InvoiceDocumentReference" >
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
	</xsl:template>
	
	<xsl:template match="cac:OrderReference" >
		<cac:OrderReference>
			<xsl:apply-templates select="cbc:ID" mode="ID" />
			<xsl:apply-templates select="cbc:IssueDate" />
			<xsl:apply-templates select="cbc:SalesOrderReference" />		
		</cac:OrderReference>
	
	</xsl:template>
		
	
	<xsl:template match="cac:InvoicePeriod" >
		<cac:InvoicePeriod>
			<xsl:apply-templates select="cbc:StartDate" />
			<xsl:apply-templates select="cbc:EndDate" />
			<!--<xsl:apply-templates select="cbc:DescriptionCode" />-->
		
		</cac:InvoicePeriod>
	
	</xsl:template>
	
	<xsl:template match="cbc:DocumentCurrencyCode">
		<cbc:DocumentCurrencyCode listID="ISO 4217 Alpha" listAgencyID="5" listSchemeURI="http://docs.oasis-open.org/ubl/os-UBL-2.1/cl/gc/default/CurrencyCode-2.1.gc">
			<xsl:value-of select="." />
		</cbc:DocumentCurrencyCode>
	</xsl:template>
	
	
	
	<!-- GENERIC ELEMENTS-->
	
	<xsl:template match="cbc:Note">
		<cbc:Note>
			<xsl:value-of select="."/>
		</cbc:Note>

	</xsl:template>
	
	<xsl:template name ="AllowanceTotalAmount">
		<cbc:AllowanceTotalAmount currencyID="HRK">
			<xsl:choose>
				<xsl:when test="//cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount"><xsl:value-of select="//cac:LegalMonetaryTotal/cbc:AllowanceTotalAmount"/></xsl:when>
				<xsl:otherwise>0.00</xsl:otherwise>
			</xsl:choose>
		</cbc:AllowanceTotalAmount>
		
	</xsl:template>
	
	<xsl:template match="cbc:Amount | cbc:BaseAmount | cbc:TaxAmount | cbc:TaxableAmount
		| cbc:LineExtensionAmount | cbc:TaxExclusiveAmount | cbc:TaxInclusiveAmount
		 | cbc:ChargeTotalAmount | cbc:PrepaidAmount
		| cbc:PayableRoundingAmount | cbc:PayableAmount " >
		<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:value-of select="format-number(number(.), '#.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(.), '#.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:value-of select="number(.)" />
		</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="PartyIdentification">
		<cac:PartyIdentification>
			<cbc:ID>
				<xsl:value-of select="concat('9934:', .)"/>
			</cbc:ID>
		
		</cac:PartyIdentification>
	
	</xsl:template>
	
	<xsl:template match="cbc:CompanyID" mode="PartyTaxID">
		<cbc:CompanyID>
			<xsl:value-of select="concat('HR', .)"/>
		</cbc:CompanyID>
	
	</xsl:template>

	
	<xsl:template match="cbc:CompanyID" mode="EndpointID" >
		<cbc:EndpointID>
			<xsl:attribute name="schemeID" namespace="">9934</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">
		<xsl:if test="@schemeID and (normalize-space(@schemeID) = '0088')">
			<cbc:EndpointID schemeID="GLN">
				<xsl:value-of select="." />
			</cbc:EndpointID>
		
		</xsl:if>
<!--		<cbc:EndpointID>
			<xsl:choose>
				<xsl:when test="@schemeID and (normalize-space(@schemeID) != '9934')">
					<xsl:attribute name="schemeID" namespace="">
						<xsl:value-of select="@schemeID" />
					</xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="schemeID" namespace="">9934</xsl:attribute>			
				</xsl:otherwise>
			</xsl:choose>
			<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:element name="{name(.)}" namespace="{namespace-uri(.)}">
			<xsl:value-of select="." />
		</xsl:element>
		

	</xsl:template>
</xsl:stylesheet>
