<!--20/11/2018 --><axsl:stylesheet xmlns:xx="http://xml.sandre.eaufrance.fr/scenario/bilan_epr/3" xmlns:fn="http://www.w3.org/2006/xpath-functions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:sa_ref="http://xml.sandre.eaufrance.fr/scenario/referentiel/2" xmlns:axsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <axsl:output method="text" omit-xml-declaration="no" standalone="yes" indent="yes"/>
    <axsl:variable name="cheminPAR">http://xml.sandre.eaufrance.fr/exist/sandre/Referentiels/PAR.xml</axsl:variable>
    <axsl:variable name="cheminSUP">http://xml.sandre.eaufrance.fr/exist/sandre/Referentiels/SUP.xml</axsl:variable>
    <axsl:variable name="cheminFAN">http://xml.sandre.eaufrance.fr/exist/sandre/Referentiels/FAN.xml</axsl:variable>
    <axsl:variable name="testFanSup">
        <axsl:for-each select="document($cheminFAN)//sa_ref:Referentiel/sa_ref:FractionAnalysee">
            <axsl:text>'</axsl:text>
            <axsl:value-of select="current()/sa_ref:CdFractionAnalysee/text()"/>
            <axsl:text>-</axsl:text>
            <axsl:value-of select="current()/sa_ref:Support/sa_ref:CdSupport/text()"/>
            <axsl:text>'</axsl:text>
            <axsl:if test="position()!=last()">
                <axsl:text>, </axsl:text>
            </axsl:if>
        </axsl:for-each>
    </axsl:variable>
    <axsl:template match="*|@*" mode="schematron-get-full-path">
        <axsl:apply-templates select="parent::*" mode="schematron-get-full-path"/>
        <axsl:text>/</axsl:text>
        <axsl:if test="count(. | ../@*) = count(../@*)">@</axsl:if>
        <axsl:value-of select="name()"/>
        <axsl:text>[</axsl:text>
        <axsl:value-of select="1+count(preceding-sibling::*[name()=name(current())])"/>
        <axsl:text>]</axsl:text>
    </axsl:template>
    <axsl:template match="/">
        <axsl:apply-templates select="/" mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="./xx:FctionIntervenantCampagne/xx:RoleIntCampEpandage/text()='13'"/>
            <axsl:otherwise>E4.10/Les fonctions ayant pour code 2 (épandeur du produit d'épandage), 7 (service instructeur du plan d'épandage), 8 (Prestataire de suivi du plan d'épandage) et 13 (pétitionnaire du plan d'épandage) sont obligatoires.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:choose>
            <axsl:when test="./xx:FctionIntervenantCampagne/xx:RoleIntCampEpandage/text()='7'"/>
            <axsl:otherwise>E4.10/Les fonctions ayant pour code 2 (épandeur du produit d'épandage), 7 (service instructeur du plan d'épandage), 8 (Prestataire de suivi du plan d'épandage) et 13 (pétitionnaire du plan d'épandage) sont obligatoires.#
</axsl:otherwise>
        </axsl:choose>
                <axsl:choose>
            <axsl:when test="./xx:FctionIntervenantCampagne/xx:RoleIntCampEpandage/text()='2'"/>
            <axsl:otherwise>E4.10/Les fonctions ayant pour code 2 (épandeur du produit d'épandage), 7 (service instructeur du plan d'épandage), 8 (Prestataire de suivi du plan d'épandage) et 13 (pétitionnaire du plan d'épandage) sont obligatoires.#
</axsl:otherwise>
        </axsl:choose>
                <axsl:choose>
            <axsl:when test="./xx:FctionIntervenantCampagne/xx:RoleIntCampEpandage/text()='8'"/>
            <axsl:otherwise>E4.10/Les fonctions ayant pour code 2 (épandeur du produit d'épandage), 7 (service instructeur du plan d'épandage), 8 (Prestataire de suivi du plan d'épandage) et 13 (pétitionnaire du plan d'épandage) sont obligatoires.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PointSuivi/xx:PrlvtTerre" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="./xx:Support/xx:CdSupport!='25'">
            E4.25/Le support associé aux prélèvements de terre DOIT être le sol (code Sandre 25).#
</axsl:when>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:FctionIntervenantCampagne" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="./xx:Intervenant/xx:CdIntervenant/text()=/xx:BILAN_EPR/xx:Intervenant/xx:CdIntervenant"/>
            <axsl:otherwise>E4.1/Tous les intervenants mis en jeu au sein d'un fichier DOIVENT être déclarés en amont du fichier. L'intervenant dont le code est '<axsl:value-of select="./xx:Intervenant/xx:CdIntervenant/text()"/>' n'est pas déclaré.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:choose>
            <axsl:when test="(./xx:RoleIntCampEpandage='13') and ./xx:Intervenant/xx:CdIntervenant!=/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PerimetreEpandage/xx:Petitionnaire/xx:CdIntervenant">
E4.6/Incohérence sur la fonction de l'intervenant <axsl:value-of select="./xx:Intervenant/xx:CdIntervenant/text()"/>' mentionné comme pétitionnaire.#
                        </axsl:when>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="//xx:PointSuivi" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(not(./xx:CoordXPointSuivi) and not(./xx:CoordYPointSuivi) and not(./xx:TypeProjCoordPointSuivi))or ((./xx:CoordXPointSuivi) and (./xx:CoordYPointSuivi) and (./xx:TypeProjCoordPointSuivi)) "/>
            <axsl:otherwise>E4.2/Toute coordonnée géographique mentionnée pour un point de suivi DOIT être accompagnée du type de projection géographique. Les coordonnées géographiques du point '<axsl:value-of select="./xx:CdPointSuivi"/>' sont incomplètes.#
</axsl:otherwise>
        </axsl:choose>
    </axsl:template>
    <axsl:template match="//xx:ExpAgri" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(not(./xx:CoordXExpAgri) and not(./xx:CoordYExpAgri) and not(./xx:TypeProjCoordExpAgri))or ((./xx:CoordXExpAgri) and (./xx:CoordYExpAgri) and (./xx:TypeProjCoordExpAgri)) "/>
            <axsl:otherwise>E4.2/Toute coordonnée géographique mentionnée pour une exploitation agricole DOIT être accompagnée du type de projection géographique. Les coordonnées géographiques de l'exploitation agricole '<axsl:value-of select="./xx:Exploitant/xx:CdIntervenant"/>' sont incomplètes.#
</axsl:otherwise>
        </axsl:choose>
    </axsl:template>
        <!--<axsl:template match="/xx:DESC_EPR/xx:UniteProduction" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(./xx:TypeUniteProduction='1.2' and ./xx:OuvrageDepollution) or (./xx:TypeUniteProduction='2' and ./xx:ExpAgri) or (./xx:TypeUniteProduction='5' and ./xx:SiteIndustriel)"/>
            <axsl:otherwise>E4.3/Le type d'unité de production est incohérent par rapport à l'unité de production déclarée.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
        </axsl:template>-->

    <!--<axsl:template match="/xx:DESC_EPR/xx:PerimetreEpandage/xx:PdtEpandage/xx:PrlvtEffluent" priority="4000" mode="M1">
        <axsl:variable name="DatePrel" select="number(translate(./xx:DatePrlvtEffluent/text(),'-',''))"/>
        <axsl:for-each select="./xx:AnalyseEffluent">
            <axsl:variable name="Par" select="./xx:Parametre/xx:CdParametre/text()"/>
            <axsl:variable name="CdPdt" select="../../xx:CdPdtEpandage"/>
            <axsl:variable name="ParUnit"
                select="concat(./xx:Parametre/xx:CdParametre,'-',./xx:UniteMesure/xx:CdUniteMesure)"/>
            <axsl:variable name="ParSitu"
                select="concat(./xx:Parametre/xx:CdParametre,'-',./xx:InSituAnalyse/text())"/>
            <axsl:if test="./xx:DateAnalyse!=''">
                <axsl:variable name="DateAna"
                    select="number(translate(./xx:DateAnalyse/text(),'-',''))"/>
                <xsl:choose>
                    <xsl:when test="$DateAna<=$DatePrel"></xsl:when>
                    <axsl:otherwise>E4.9/Toute date d’analyse DOIT être supérieure ou égale à la date de ce même prélèvement. L'analyse réalisée le '<axsl:value-of select="$DateAna"/>' ayant porté sur le paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>' a été réalisée à une date antérieure au prélèvement '<axsl:value-of select="../xx:DatePrlvtEffluent"/>', sur le produit '<axsl:value-of select="$CdPdt"/>'.#
</axsl:otherwise>
                </xsl:choose>
                <axsl:choose>
            <axsl:when test="(string-length(./xx:RsAnalyse/text())=0 and (./xx:CdRemAnalyse/text()!='5') and (./xx:CdRemAnalyse/text()!='0'))">E4.13/Le résultat d'analyse du paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>' ne peut pas etre nul, à condition d'être accompagné d'un code remarque égal à
0 (pour analyse non réalisée) ou 5 (pour incomptable).#
            </axsl:when>
<axsl:when test="(string-length(./xx:RsAnalyse/text())!=0 and (./xx:CdRemAnalyse/text()='0'))">E4.14/Erreur au niveau de l'analyse portant sur le paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>'. Celle-ci n'a pas été réalisée (code remarque associé égal à 0), et le résultat d'analyse est pourtant différent d'une valeur vide.#
</axsl:when>
                    <axsl:when test="(string-length(./xx:RsAnalyse/text())!=0 and (./xx:CdRemAnalyse/text()='5'))">E4.15/Erreur au niveau de l'analyse portant sur le paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>'. Le résultat est incomptable (code remarque associé égal à 5), et celui-ci est pourtant différent d'une valeur nulle.#
</axsl:when>
                </axsl:choose>


            </axsl:if>
 <axsl:apply-templates mode="M1"/>
        </axsl:for-each>
        </axsl:template>-->
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PrlvtEffluent/xx:AnalyseEffluent" priority="4000" mode="M1">
        <axsl:variable name="Support">
            <axsl:value-of select="ancestor::xx:PrlvtEffluent/xx:Support/xx:CdSupport"/>
        </axsl:variable>
        <axsl:variable name="valueTestFanSup">
            <axsl:text>'</axsl:text>
            <axsl:value-of select="current()/xx:FractionAnalysee/xx:CdFractionAnalysee/text()"/>
            <axsl:text>-</axsl:text>
            <axsl:value-of select="ancestor::xx:PrlvtEffluent/xx:Support/xx:CdSupport"/>
            <axsl:text>'</axsl:text>
        </axsl:variable>
        <xsl:choose>
            <xsl:when test="contains($testFanSup, $valueTestFanSup)"/>
            <axsl:otherwise>A4.22/La fraction analysée '<axsl:value-of select="./xx:FractionAnalysee/xx:CdFractionAnalysee"/>' (<axsl:value-of select="document($cheminFAN)/sa_ref:REFERENTIELS/sa_ref:Referentiel/sa_ref:FractionAnalysee[./sa_ref:CdFractionAnalysee/text()=current()/xx:FractionAnalysee/xx:CdFractionAnalysee/text()]/sa_ref:LbFractionAnalysee"/>) associée à une analyse est incohérente par rapport au support prélevé
                        '<axsl:value-of select="ancestor::xx:PrlvtEffluent/xx:Support/xx:CdSupport"/>' (<axsl:value-of select="document($cheminSUP)/sa_ref:REFERENTIELS/sa_ref:Referentiel/sa_ref:Support[./sa_ref:CdSupport/text()=$Support]/sa_ref:LbSupport"/>) .#</axsl:otherwise>
        </xsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PrlvtEffluent/xx:AnalyseEffluent[./xx:CdRemAnalyse/text()='4']" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(document($cheminPAR)//sa_ref:Referentiel/sa_ref:Parametre[./sa_ref:CdParametre/text()=current()/xx:Parametre/xx:CdParametre/text()]/sa_ref:ParametreMicrobiologique) and (current()/xx:UniteMesure/xx:CdUniteReference='X') and((current()/xx:RsAnalyse/text()='1') or (current()/xx:RsAnalyse/text()='2'))"/>
            <axsl:otherwise>E4.16/Erreur au niveau du résultat du paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>' ('<axsl:value-of select="document($cheminPAR)//sa_ref:Referentiel/sa_ref:Parametre[./sa_ref:CdParametre/text()=current()/xx:Parametre/xx:CdParametre/text()]/sa_ref:NomParametre"/>'). Le code remarque 4 est réservé aux résultats de paramètre microbiologique qualitatif de type Présence ou Absence.
 Le résultat prend alors deux valeurs possibles '1' (Absence) ou '2' (présence). Le code de l'unité de mesure associé au résultat du paramètre DOIT prendre pour valeur 'X' (sans objet) 
.#</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PerimetreEpandage/xx:ParcellePerimetre/xx:AptParcelle" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(./xx:ClasseApt/xx:CdClasseApt!='2' or ./xx:ClasseApt/xx:CdClasseApt!='3') or (./xx:ClasseApt/xx:CdClasseApt='2' and ./xx:ContrainteApt!='') or (./xx:ClasseApt/xx:CdClasseApt='3' and ./xx:ContrainteApt!='')  "/>
            <axsl:otherwise>E4.29/Lorsque la classe d’aptitude d’une parcelle du périmètre prend pour valeur 2 (sous contrainte) ou 3 (interdit), il est nécessaire de spécifier la raison de la contrainte ou de l’interdiction d’épandre sur la parcelle. La parcelle du périmètre erronée est celle dont le code est '<axsl:value-of select="ancestor::xx:ParcellePerimetre/xx:CdParcellePerimetre"/>'.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>      
    
    
    
    
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PrlvtEffluent/xx:AnalyseEffluent[string-length(./xx:RsAnalyse/text())=0]" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="(./xx:CdRemAnalyse/text()='5') or (./xx:CdRemAnalyse/text()='0')"/>
            <axsl:otherwise>E4.13/Le résultat d'analyse du paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>' ne peut pas etre nul, à condition d'être accompagné d'un code remarque égal à 0 (pour analyse non réalisée) ou 5 (pour incomptable).#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PrlvtEffluent/xx:AnalyseEffluent[./xx:CdRemAnalyse/text()='0']" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="string-length(./xx:RsAnalyse/text())=0"/>
            <axsl:otherwise>E4.14/Erreur au niveau de l'analyse portant sur le paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>'. Celle-ci n'a pas été réalisée (code remarque associé égal à 0), et le résultat d'analyse est pourtant différent d'une valeur nulle.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="/xx:BILAN_EPR/xx:CampEpandage/xx:PdtEpandage/xx:PrlvtEffluent/xx:AnalyseEffluent[./xx:CdRemAnalyse/text()='2' or ./xx:CdRemAnalyse/text()='3']" priority="4000" mode="M1">
        <axsl:choose>
            <axsl:when test="document($cheminPAR)//sa_ref:Referentiel/sa_ref:Parametre[./sa_ref:CdParametre/text()=current()/xx:Parametre/xx:CdParametre/text()][(./sa_ref:ParametreChimique) or (./sa_ref:ParametrePhysique) ]"/>
            <axsl:otherwise>E4.21/Erreur au niveau du résultat du paramètre '<axsl:value-of select="./xx:Parametre/xx:CdParametre"/>' ('<axsl:value-of select="document($cheminPAR)//sa_ref:Referentiel/sa_ref:Parametre[./sa_ref:CdParametre/text()=current()/xx:Parametre/xx:CdParametre/text()]/sa_ref:NomParametre"/>'). Les codes remarque '2' (&lt;seuil de détection), '3' (&lt;seuil de saturation), '7' (traces, LD&lt;resultat&lt;LQ) et '10' (&lt; seuil de quantification) sont réservés aux résultats de paramètre chimique.#
</axsl:otherwise>
        </axsl:choose>
        <axsl:apply-templates mode="M1"/>
    </axsl:template>
    <axsl:template match="text()" priority="-1" mode="M1"/>
    <axsl:template match="text()" priority="-1"/>
</axsl:stylesheet>