<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp "&#160;">
<!ENTITY lt "&#060;">
<!ENTITY gt "&#062;">
]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n="http://www.xemelios.org/namespaces#FEN0052A" xmlns:added="http://projets.admisource.gouv.fr/xemelios/namespaces#added" xmlns:ano="http://projets.admisource.gouv.fr/xemelios/namespaces#anomally" xmlns:txt2xml="http://www.xemelios.org/namespaces#txt2xml" version="2.0">

    <xsl:output encoding="ISO-8859-1" method="xml" indent="yes"/>

    <xsl:param name="anoId"/>
    <!-- id de l'anomalie a mettre en surbrillance -->

    <xsl:param name="collectivite"/>
    <xsl:param name="budget"/>

    <xsl:param name="presentation" select="'src'"/>
    <!-- valid values are 'normal' and 'src' -->

    <xsl:decimal-format name="decformat" decimal-separator="," grouping-separator=" " digit="#" pattern-separator=";" NaN="NaN" minus-sign="-"/> 

    <xsl:variable name="tags-ano">
        <xsl:for-each select="//ano:Anomalie[@ano:anoId = $anoId]/ano:node">
            <xsl:element name="node">
                <xsl:attribute name="id" select="./@ano:id"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:variable>

    <xsl:template match="/n:DocFacturesSansEngagementTiers">
        <xsl:choose>
            <!--xsl:when test="$presentation = 'normal'">
                <html>
                    <head>
                        <title>Demande d'achat</title>
                        <style type="text/css" media="print">
                            .invoiceDiv {
                                width: 100%;
                            }
                            tr {
                                page-break-inside: avoid;
                            }
                            table {
                                -fs-table-paginate: paginate;
                            }
                            .anomalie {
                                display: none;
                            }</style>
                        <style type="text/css" media="screen">
                            .invoiceDiv {
                                width: 75%;
                            }
                            th {
                                color: white;
                                background-color: #999999;
                            }
                            .highlighted {
                                background-color: #FFFF66;
                            }</style>
                        <style type="text/css" media="all">
                            body, p, th, td {
                                font-size: 12;
                                font-family: Verdana, Arial, Helvetica, sans-serif;
                            }
                            table {
                                border-collapse: collapse;
                            }
                            anomalie {
                                background-color: #FFFF66;
                                 
                            }
                            .titre0 {
                                font-weight: bold;
                                font-family: Verdana, Arial, Helvetica, sans-serif;
                                font-size: 24;
                            }
                            .center {
                                text-align: center;
                            }
                            .top {
                                vertical-align: top;
                            }
                            .gras {
                                font-weight: bold;
                            }
                            .right {
                                text-align: right;
                            }
                            .left {
                                text-align: left;
                            }
                            .bordered {
                                border-style: solid;
                                border-width: 1px;
                                border-color: black;
                                v
                                padding: 10px;
                            }
                            .collapsed {
                                border-collapse: collapse;
                                border-spacing: 0px;
                            }
                            .titre1 {
                                margin-top: 12px;
                                margin-bottom: 0px;
                                font-wieght: bold;
                                font-size: 16;
                            }
                            .nosign {
                                list-style-type: none;
                            }
                            td {
                                background-color: inherited;
                            }</style>
                    </head>
                    <body>
                        <center>
                            <xsl:apply-templates/>
                        </center>
                    </body>
                </html>
            </xsl:when-->
            <xsl:when test="$presentation = 'src' and count(//@txt2xml:*) eq 0">
                <html>
                    <head>
                        <title>Source XML de FEN0052A</title>
                        <style type="text/css">
                            span {
                                font-family: monospace;
                                font-size: 12px;
                                 
                            }
                            .expander-content {
                                padding-left: 1em;
                            }
                            .text {
                                color: black;
                                 
                            }
                            .expander {
                                text-align: center;
                                vertical-align: top;
                                width: 1em;
                                display: inline-block;
                                margin-left: -1em;
                            }
                            body {
                                font-family: monospace;
                                font-size: 12px;
                            }
                            #top > .expander-open, #top > .expander-closed {
                                margin-left: 1em;
                            }
                            .expander-closed > .expander-content {
                                display: none;
                            }
                            .comment {
                                font-family: monospace;
                                font-size: 12px;
                                white-space: pre;
                            }
                            #top > .expander-open {
                                font-family: monospace;
                                font-size: 12px;
                                white-space: pre;
                            }
                            *|*:root {
                                background-color: white;
                            }
                            #viewsource {
                                font-family: monospace;
                                font-size: 12px;
                                font-weight: normal;
                                color: black;
                                white-space: pre;
                            }
                            #viewsource.wrap {
                                white-space: pre-wrap;
                            }
                            pre {
                                font: inherit;
                                color: inherit;
                                white-space: inherit;
                                margin: 0;
                            }
                            .start-tag {
                                color: purple;
                                font-weight: bold;
                                font-size: 12px;
                            }
                            .end-tag {
                                color: purple;
                                font-weight: bold;
                                font-size: 12px;
                            }
                            .comment {
                                color: green;
                                font-style: italic;
                            }
                            .cdata {
                                color: #CC0066;
                            }
                            .doctype {
                                color: steelblue;
                                font-style: italic;
                            }
                            .pi {
                                color: orchid;
                                font-style: italic;
                            }
                            .entity {
                                color:#FF4500;
                                font-weight: normal;
                            }
                            .text {
                                font-weight: normal;
                            }
                            .attribute-name {
                                color: black;
                                font-weight: bold;
                            }
                            .attribute-value {
                                color: blue;
                                font-weight: normal;
                            }
                            .summary {
                                display: block;
                                background-color: #FFFFCC;
                                width: 90%;
                                border: solid;
                                border-width: 1pt;
                                font-family: sans-serif;
                            }
                            .popup {
                                font-weight: normal;
                            }
                            .markupdeclaration {
                                color: steelblue;
                                font-style: italic;
                            }
                            .error, .error > .start-tag, .error > .end-tag,
                            .error > .comment, .error > .cdata, .error > .doctype,
                            .error > .pi, .error > .entity, .error > .attribute-name,
                            .error > .attribute-value {
                                color: red;
                                font-weight: bold;
                            }</style>
                    </head>
                    <body>
                        <!--p>Paramètres<br/>
                            collectivite: <xsl:value-of select="$collectivite"/><br/>
                            collectivite-lib: <xsl:value-of select="$collectivite-lib"/><br/>
                            budget: <xsl:value-of select="$budg"/><br/>
                            budget-lib: <xsl:value-of select="$budget-lib"/>
                        </p-->
                        <xsl:for-each select="./n:FEN0052A">
                            <xsl:call-template name="displaySrc">
                                <xsl:with-param name="el" select="."/>
                            </xsl:call-template>
                        </xsl:for-each>
                        <xsl:if test="count(//ano:Anomalie) &gt; 0">
                            <hr/>
                            <xsl:for-each select="//ano:Anomalie">
                                <xsl:call-template name="anomalie">
                                    <xsl:with-param name="ano" select="."/>
                                    <xsl:with-param name="facturessansengagementtiersID" select="../@ano:unique-id"/>
                                </xsl:call-template>
                            </xsl:for-each>
                        </xsl:if>
                    </body>
                </html>
            </xsl:when>
            <xsl:when test="$presentation = 'src' and count(//@txt2xml:*) &gt; 0">
                <html>
                    <head>
                        <title>Source Texte de FEN0052A</title>
                        <style type="text/css">
                            span {
                                font-family: monospace;
                                font-size: 12px;
                                 
                            }
                            .expander-content {
                                padding-left: 1em;
                            }
                            .text {
                                color: black;
                                 
                            }
                            .expander {
                                text-align: center;
                                vertical-align: top;
                                width: 1em;
                                display: inline-block;
                                margin-left: -1em;
                            }
                            body {
                                font-family: monospace;
                                font-size: 12px;
                            }
                            #top > .expander-open, #top > .expander-closed {
                                margin-left: 1em;
                            }
                            .expander-closed > .expander-content {
                                display: none;
                            }
                            .comment {
                                font-family: monospace;
                                font-size: 12px;
                                white-space: pre;
                            }
                            #top > .expander-open {
                                font-family: monospace;
                                font-size: 12px;
                                white-space: pre;
                            }
                            *|*:root {
                                background-color: white;
                            }
                            #viewsource {
                                font-family: monospace;
                                font-size: 12px;
                                font-weight: normal;
                                color: black;
                                white-space: pre;
                            }
                            #viewsource.wrap {
                                white-space: pre-wrap;
                            }
                            pre {
                                font: inherit;
                                color: inherit;
                                white-space: inherit;
                                margin: 0;
                            }
                            .start-tag {
                                color: purple;
                                font-weight: bold;
                                font-size: 12px;
                            }
                            .end-tag {
                                color: purple;
                                font-weight: bold;
                                font-size: 12px;
                            }
                            .comment {
                                color: green;
                                font-style: italic;
                            }
                            .cdata {
                                color: #CC0066;
                            }
                            .doctype {
                                color: steelblue;
                                font-style: italic;
                            }
                            .pi {
                                color: orchid;
                                font-style: italic;
                            }
                            .entity {
                                color:#FF4500;
                                font-weight: normal;
                            }
                            .text {
                                font-weight: normal;
                            }
                            .attribute-name {
                                color: black;
                                font-weight: bold;
                            }
                            .attribute-value {
                                color: blue;
                                font-weight: normal;
                            }
                            .summary {
                                display: block;
                                background-color: #FFFFCC;
                                width: 90%;
                                border: solid;
                                border-width: 1pt;
                                font-family: sans-serif;
                            }
                            .popup {
                                font-weight: normal;
                            }
                            .markupdeclaration {
                                color: steelblue;
                                font-style: italic;
                            }
                            .error, .error > .start-tag, .error > .end-tag,
                            .error > .comment, .error > .cdata, .error > .doctype,
                            .error > .pi, .error > .entity, .error > .attribute-name,
                            .error > .attribute-value {
                                color: red;
                                font-weight: bold;
                            }</style>
                    </head>
                    <body>
                        <xsl:for-each select="n:FEN0052A">
                            <xsl:call-template name="displayText">
                                <xsl:with-param name="el" select="."/>
                            </xsl:call-template>
                        </xsl:for-each>
                        <xsl:if test="count(//ano:Anomalie) &gt; 0">
                            <hr/>
                            <xsl:variable name="allAnos" select="//ano:Anomalie"/>
                            <xsl:for-each select="$allAnos">
                                <xsl:call-template name="anomalie">
                                    <xsl:with-param name="ano" select="."/>
                                    <xsl:with-param name="facturessansengagementtiersID" select="../@ano:unique-id"/>
                                </xsl:call-template>
                            </xsl:for-each>
                        </xsl:if>
                    </body>
                </html>
            </xsl:when>
            <xsl:otherwise>
                <html>
                    <body>
                        <xsl:value-of select="$presentation"/>
                        <xsl:value-of select="$anoId"/>
                    </body>
                </html>
            </xsl:otherwise>       </xsl:choose>
    </xsl:template>

    <xsl:template name="anomalie">
        <xsl:param name="ano"/>
        <xsl:param name="facturessansengagementtiersID"/>
        <xsl:element name="div">
            <xsl:attribute name="class">anomalie left</xsl:attribute>
            <xsl:attribute name="width">75%</xsl:attribute>
            <xsl:if test="$anoId = ./@ano:anoId">
                <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
            </xsl:if>
            <h3 style="text-align: left;">
                <xsl:element name="a">
                    <xsl:attribute name="name">AnoId_<xsl:value-of select="$ano/@ano:anoId"/></xsl:attribute>
                    <xsl:attribute name="href">xemelios:/query?docId=DocFacturesSansEngagementTiers&amp;etatId=FEN0052A&amp;elementId=FACTURE&amp;collectivite=<xsl:value-of select="$collectivite"/>&amp;budget=<xsl:value-of select="$budget"/>&amp;path=[@ano:unique-id='<xsl:value-of select="$facturessansengagementtiersID"/>']&amp;xsl:param=(anoId,<xsl:value-of select="$ano/@ano:anoId"/>)&amp;xsl:param=(presentation,<xsl:value-of select="$presentation"/>)</xsl:attribute>
                    Anomalie
                </xsl:element>
            </h3>
            <span class="gras">Contr&#244;le :</span><xsl:value-of select="./@ano:ctrlLibelle"/>&#160;(<xsl:value-of select="./@ano:ctrlId"/>)<br/>
            <span class="gras">R&#232;gle fonctionnelle : </span><xsl:value-of select="./ano:ctrlRegleFonct/text()" disable-output-escaping="yes"/><br/>
            <span class="gras">Message : </span><xsl:value-of select="./ano:message/text()" disable-output-escaping="yes"/>
        </xsl:element>

    </xsl:template>

    <xsl:template name="displaySrc">
        <xsl:param name="el"/>
        <xsl:choose>
            <xsl:when test="namespace-uri(.) = 'http://projets.admisource.gouv.fr/xemelios/namespaces#anomally' and local-name(.)='Anomalie'"/>
            <xsl:when
                test="$el[* or processing-instruction() or comment() or string-length(.) &gt; 0]">
                <div class="expander-open">
                    <xsl:variable name="currentNode" select="$el/@ano:node-id"/>
                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                    </xsl:if>
                    <xsl:text>&lt;</xsl:text>
                    <span class="start-tag">
                        <xsl:value-of select="name(.)"/>
                    </span>
                    <xsl:apply-templates select="@*"/>
                    <xsl:text>&gt;</xsl:text>
                    <xsl:choose>
                        <xsl:when test="count(./*) &gt; 0">
                            <div class="expander-content">
                                <xsl:for-each select="./*">
                                    <xsl:call-template name="displaySrc">
                                        <xsl:with-param name="el" select="."/>
                                    </xsl:call-template>
                                    <!--xsl:if test="not(position() = last())"><br/></xsl:if-->
                                </xsl:for-each>
                                <span class="text">
                                    <xsl:value-of select="./text()"/>
                                </span>
                            </div>
                        </xsl:when>
                        <xsl:otherwise>
                            <span class="text">
                                <xsl:value-of select="./text()"/>
                            </span>
                        </xsl:otherwise>
                    </xsl:choose>
                    <xsl:text>&lt;/</xsl:text>
                    <span class="end-tag">
                        <xsl:value-of select="name(.)"/>
                    </span>
                    <xsl:text>&gt;</xsl:text>
                </div>
            </xsl:when>
            <xsl:otherwise>
                <xsl:text>&lt;</xsl:text>
                <span class="start-tag">
                    <xsl:variable name="currentNode" select="$el/@ano:node-id"/>
                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="name(.)"/>
                </span>
                <xsl:apply-templates select="@*"/>
                <xsl:text>/&gt;</xsl:text>
                <br/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template name="displayText">
        <xsl:param name="el"/>
        <xsl:choose>
            <xsl:when test="namespace-uri(.) = 'http://projets.admisource.gouv.fr/xemelios/namespaces#anomally' and local-name(.)='Anomalie'"/>
            <xsl:otherwise>
                <xsl:for-each select="$el/n:FACTURE">
                    <xsl:variable name="currentNode" select="n:ENTETE/@ano:node-id"/>
                    <span><xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0"><xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute></xsl:if>E
                        <xsl:for-each select="n:ENTETE/*/@txt2xml:TxtValue">
                            <span>
                                <xsl:variable name="currentNode" select="../@ano:node-id"/>
                                <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                                    <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                                </xsl:if>
                                <xsl:value-of select="."/>
                            </span>
                        </xsl:for-each>
                    </span>
                    <br/>
                    <xsl:for-each select="n:LIGNE_POSTE">
                     <xsl:variable name="currentNode" select="@ano:node-id"/>
                        <span><xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0"><xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute></xsl:if>P
                            <xsl:for-each select="child::node()/@txt2xml:TxtValue">
                                <span>
                                    <xsl:variable name="currentNode" select="../@ano:node-id"/>
                                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="."/>
                                </span>
                            </xsl:for-each>
                            </span>
                        <br/>
                    </xsl:for-each>
                    
                    <xsl:for-each select="n:RESPONSABLE">
                        <xsl:variable name="currentNode" select="@ano:node-id"/>
                        <span><xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0"><xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute></xsl:if>R
                            <xsl:for-each select="child::node()/@txt2xml:TxtValue">
                                <span>
                                    <xsl:variable name="currentNode" select="../@ano:node-id"/>
                                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="."/>
                                </span>
                            </xsl:for-each>
                        </span>
                        <br/>
                    </xsl:for-each>
                    
                    <xsl:for-each select="n:LIGNE_NOTE">
                        <xsl:variable name="currentNode" select="@ano:node-id"/>
                        <span><xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0"><xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute></xsl:if>T
                            <xsl:for-each select="child::node()/@txt2xml:TxtValue">
                                <span>
                                    <xsl:variable name="currentNode" select="../@ano:node-id"/>
                                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="."/>
                                </span>
                            </xsl:for-each>
                        </span>
                        <br/>
                    </xsl:for-each>
                    <xsl:for-each select="n:LIGNE_INCONNUE">
                        <xsl:variable name="currentNode" select="@ano:node-id"/>
                        <span><xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0"><xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute></xsl:if>
                            <xsl:for-each select="child::node()/@txt2xml:TxtValue">
                                <span>
                                    <xsl:variable name="currentNode" select="../@ano:node-id"/>
                                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                                    </xsl:if>
                                    <xsl:value-of select="."/>
                                </span>
                            </xsl:for-each>
                        </span>
                        <br/>
                    </xsl:for-each>  
                </xsl:for-each>
                <!--span class="start-tag">
                    <xsl:variable name="currentNode" select="$el/@ano:node-id"/>
                    <xsl:if test="count($tags-ano/node[@id = $currentNode]) &gt; 0">
                        <xsl:attribute name="style">background-color: #FFFF66;</xsl:attribute>
                    </xsl:if>
                    <xsl:value-of select="name(.)"/>
                </span>
                <xsl:apply-templates select="@*"/-->
                <br/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
    <xsl:template match="@*">
        <xsl:choose>
            <xsl:when test="namespace-uri(.) = 'http://projets.admisource.gouv.fr/xemelios/namespaces#anomally' and (local-name(.) = 'node-id' or local-name(.) = 'unique-id')"/>
            <xsl:when test="namespace-uri(.) = 'http://www.xemelios.org/namespaces#txt2xml' and (local-name(.) = 'LineTxt' or local-name(.) = 'TxtValue')"/>
            <xsl:when
                test="namespace-uri(.) = 'http://projets.admisource.gouv.fr/xemelios/namespaces#added'"/>
            <xsl:otherwise>&#160;<span class="attribute-name"><xsl:value-of select="name(.)"/></span><xsl:text>=</xsl:text><span class="attribute-value">"<xsl:value-of select="."/>"</span></xsl:otherwise>
        </xsl:choose>
    </xsl:template>


    <xsl:template name="format-date">
        <xsl:param name="datebrute"/>
        <xsl:param name="formataffichage" select="'date'"/>
        <!-- valeurs attendues : date / heure / dateheure -->
        <xsl:if test="($formataffichage = 'date') or ($formataffichage = 'dateheure')">
            <xsl:value-of select="substring($datebrute, 9, 2)"/>
            <xsl:text>/</xsl:text>
            <xsl:value-of select="substring($datebrute, 6, 2)"/>
            <xsl:text>/</xsl:text>
            <xsl:value-of select="substring($datebrute, 1, 4)"/>
        </xsl:if>
        <xsl:if test="($formataffichage = 'heure') or ($formataffichage = 'dateheure')">
            <xsl:text> </xsl:text>
            <xsl:value-of select="substring($datebrute, 12, 2)"/>
            <xsl:text>:</xsl:text>
            <xsl:value-of select="substring($datebrute, 15, 2)"/>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="number">
        <xsl:param name="num"/>
        <xsl:choose>
            <xsl:when test="string-length($num) = 0"/>
            <xsl:when test="number($num) = 0"/>
            <xsl:when test="string(number($num)) = 'NaN'"/>
            <xsl:otherwise>
                <xsl:value-of select="format-number($num,'# ###,00;-# ###,00','decformat')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
