<?xml version="1.1" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
]>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ef="urn:DGFIP:EtatsFrais"
    xmlns:added="http://projets.admisource.gouv.fr/xemelios/namespaces#added"
    xmlns:inv="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
    xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
    xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
    xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
    xmlns:dgfip="urn:DGFIP:EtatFrais:OrdreMission"
    xmlns:ano="http://projets.admisource.gouv.fr/xemelios/namespaces#anomally"
    exclude-result-prefixes="xsl" version="2.0">
    
    <xsl:output standalone="yes" method="xml" indent="yes"/>
    
    <!--xsl:output encoding="ISO-8859-1"/-->
    <xsl:output version="1.0"/>
    
    <xsl:variable name="natures" select="document('./resources/naturesOM.xml')"/>
    <xsl:variable name="pays" select="document('./resources/iso_3166-1_list_fr.xml')"/>
    <xsl:variable name="typeFrais" select="document('./resources/typesFrais.xml')"/> 
    
    <xsl:param name="language" select="'fr'"/>
    
    <xsl:template match="/*[position()=1]">
        <xsl:call-template name="writeElement">
            <xsl:with-param name="el" select="."/>
        </xsl:call-template>
    </xsl:template>
    
    <!--xsl:template match="dgfip:NatureOM">
        <xsl:element name="NatureOM" namespace="urn:DGFIP:EtatFrais:OrdreMission"><xsl:attribute name="libelle" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added"><xsl:value-of select="$natures//Nature[@code=./text()]/text()"/></xsl:attribute><xsl:value-of select="./text()"/></xsl:element>
    </xsl:template-->
    
    <!--xsl:template match="dgfip:LieuDeplacement|dgfip:LieuDepart|dgfip:LieuArrivee">
        <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
            <xsl:for-each select="./@*">
                <xsl:attribute name="{name(.)}" namespace="{namespace-uri(.)}"><xsl:value-of select="."/></xsl:attribute>
            </xsl:for-each>
            <xsl:attribute name="libPays" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added"><xsl:value-of select="$pays//ISO_3166-1_Entry[ISO_3166-1_Alpha-2_code/text()=./@CodPays]/ISO_3166-1_Country_name/text()"/></xsl:attribute>
        </xsl:element>
    </xsl:template-->
    
    <!--xsl:template match="cac:BuyersItemIdentification">
        <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
            <xsl:attribute name="libelle" select="$typeFrais/TypesFrais/Frais[@code=cbc:ID/text()]/text()"/>
            <xsl:copy-of select="cbc:ID"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="cac:AdditionalItemProperty">
        <xsl:element name="{name(.)}" namespace="{namespace-uri(.)}">
            <xsl:attribute name="generated-id" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added"><xsl:value-of select="generate-id()"/></xsl:attribute>
        </xsl:element>
        <xsl:copy-of select="cbc:Name"/>
        <xsl:copy-of select="cbc:Value"/>
    </xsl:template-->
    
    <xsl:template name="writeElement">
        <xsl:param name="el"/>
        <xsl:element name="{name($el)}" namespace="{namespace-uri($el)}">
            <xsl:for-each select="$el/@*">
                <xsl:attribute name="{name(.)}" namespace="{namespace-uri(.)}">
                    <xsl:value-of select="."/>
                </xsl:attribute>
            </xsl:for-each>
            <xsl:choose>
                <xsl:when test="namespace-uri($el) = 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2'">
                    <xsl:choose>
                        <xsl:when test="contains('|AdditionalItemProperty|InvoiceLine|',concat('|',local-name($el),'|'))"><xsl:attribute name="generated-id" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added" select="generate-id()"/></xsl:when>
                        <xsl:when test="contains('|PaymentMeans|',local-name($el))"><xsl:attribute name="RIB" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added">
                            <xsl:choose>
                                <xsl:when test="($el/cbc:PaymentChannelCode/text() = 'IBAN') and (substring($el/cac:PayeeFinancialAccount/cbc:ID/text(), 1, 2) = 'FR')">
                                    <xsl:value-of select="substring($el/cac:PayeeFinancialAccount/cbc:ID,5,5)"/>-<xsl:value-of select="substring($el/cac:PayeeFinancialAccount/cbc:ID,10,5)"/>-<xsl:value-of select="substring($el/cac:PayeeFinancialAccount/cbc:ID,15,11)"/>-<xsl:value-of select="substring($el/cac:PayeeFinancialAccount/cbc:ID,26,2)"/>
                                </xsl:when>
                                <xsl:when test="($el/cbc:PaymentChannelCode/text() = 'IBAN') and (substring($el/cac:PayeeFinancialAccount/cbc:ID/text(), 1, 2) != 'FR')">
                                    <xsl:value-of select="$el/cac:PayeeFinancialAccount/cbc:ID"/>
                                </xsl:when>
                                <xsl:when test="$el/cbc:PaymentChannelCode/text() = 'BBAN'">
                                    <xsl:value-of select="$el/cac:PayeeFinancialAccount/cac:Country/cbc:IdentificationCode"/>-<xsl:value-of select="$el/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:ID"/>-<xsl:value-of select="$el/cac:PayeeFinancialAccount/cac:FinancialInstitutionBranch/cbc:Name"/>-<xsl:value-of select="$el/cac:PayeeFinancialAccount/cbc:ID"/>-<xsl:value-of select="$el/cac:PayeeFinancialAccount/cbc:PaymentNote"/>
                                </xsl:when>
                                <xsl:otherwise>Définition du compte bancaire non reconnue</xsl:otherwise>
                            </xsl:choose>
                        </xsl:attribute></xsl:when>
                    </xsl:choose>
                </xsl:when>
                <xsl:when test="namespace-uri($el) = 'urn:DGFIP:EtatsFrais'">
                    <xsl:choose>
                        <xsl:when test="contains('|Invoice|',concat('|',local-name($el),'|'))"><xsl:attribute name="generated-id" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added" select="generate-id()"/></xsl:when>
                    </xsl:choose>
                </xsl:when>
                <xsl:when test="namespace-uri($el) = 'urn:DGFIP:EtatFrais:OrdreMission'">
                    <xsl:choose>
                        <xsl:when test="local-name($el) = 'NatureOM'"><xsl:attribute name="libelle" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added" select="$natures//Nature[@code=$el/text()]/text()"/></xsl:when>
                        <xsl:when test="contains('|LieuDeplacement|LieuDepart|LieuArrivee|',concat('|',local-name($el),'|'))"><xsl:attribute name="libPays" namespace="http://projets.admisource.gouv.fr/xemelios/namespaces#added" select="$pays//ISO_3166-1_Entry[ISO_3166-1_Alpha-2_code/text()=$el/@CodPays]/ISO_3166-1_Country_name/text()"></xsl:attribute></xsl:when>
                    </xsl:choose>
                </xsl:when>
            </xsl:choose>
            <xsl:for-each select="$el/*">
                <xsl:call-template name="writeElement">
                    <xsl:with-param name="el" select="."/>
                </xsl:call-template>
            </xsl:for-each>
            <xsl:value-of select="$el/text()"/>
        </xsl:element>
    </xsl:template>
    
    
</xsl:stylesheet>
