<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="" version="2.0">
    <!-- warning: do not change output encoding ! -->
    <xsl:output method="xhtml" encoding="ISO-8859-1" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" />
    <xsl:character-map name="accents">
        <xsl:output-character character="à" string="&amp;agrave;"/>
        <xsl:output-character character="é" string="&amp;eacute;"/>
        <xsl:output-character character="è" string="&amp;egrave;"/>
        <xsl:output-character character="ê" string="&amp;ecirc;"/>
        <xsl:output-character character="ë" string="&amp;euml;"/>
        <xsl:output-character character="î" string="&amp;icirc;"/>
        <xsl:output-character character="ï" string="&amp;iuml;"/>
        <xsl:output-character character="ô" string="&amp;ocirc;"/>
        <xsl:output-character character="ù" string="&amp;ugrave;"/>
        <xsl:output-character character="ü" string="&amp;uuml;"/>
    </xsl:character-map>
    
    <xsl:param name="server"/>
    <xsl:param name="context"/>
    <xsl:param name="port"/>

    <xsl:template match="@*|node()">
        <xsl:choose>
            <xsl:when test="contains('|href|src|action|',concat('|',name(.),'|'))">
                <xsl:variable name="href" select="."/>
                <!-- depends on protocol : http, ftp, https remain unchanged ; xemelios is modified -->
                <xsl:variable name="protocol" select="substring-before($href,':')"/>
                <xsl:variable name="newHref">
                    <xsl:choose>
                        <xsl:when test="$protocol = 'xemelios'">
                            <!-- form is : xemelios:/query?docId=PES_Aller&etatId=PES_RecetteAller&elementId=BordereauRecette&collectivite=26620926100012&budget=00&path=[@added:primary-key='2008-02-100015']&xsl:param=(mandatId,100018) -->
                            <xsl:variable name="uri" select="substring-after($href,'/')"/>
                            <xsl:variable name="params">
                                <xsl:choose>
                                    <xsl:when test="contains($uri,'?')"><xsl:value-of select="substring-after($uri,'?')"/></xsl:when>
                                    <xsl:otherwise></xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>
                            <xsl:variable name="servlet">
                                <xsl:choose>
                                    <xsl:when test="contains($uri,'?')"><xsl:value-of select="substring-before($uri,'?')"/></xsl:when>
                                    <xsl:otherwise><xsl:value-of select="$uri"/></xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>
                            <xsl:choose>
                                <xsl:when test="$servlet = 'query'">http://<xsl:value-of select="$server"/>:<xsl:value-of select="$port"/><xsl:value-of select="$context"/>/navigation.do?<xsl:value-of select="$params"/></xsl:when>
                                <xsl:when test="$servlet = 'attachment'">http://<xsl:value-of select="$server"/>:<xsl:value-of select="$port"/><xsl:value-of select="$context"/>/attachment.do?<xsl:value-of select="$params"/></xsl:when>
                                <xsl:when test="$servlet = 'resource'">http://<xsl:value-of select="$server"/>:<xsl:value-of select="$port"/><xsl:value-of select="$context"/>/resource.do?<xsl:value-of select="$params"/></xsl:when>
                                <xsl:when test="$servlet = 'customLink'">http://<xsl:value-of select="$server"/>:<xsl:value-of select="$port"/><xsl:value-of select="$context"/>/customLink.do?<xsl:value-of select="$params"/></xsl:when>
                                <xsl:otherwise>http://<xsl:value-of select="$server"/>:<xsl:value-of select="$port"/>/<xsl:value-of select="$servlet"/>?<xsl:value-of select="$params"/></xsl:otherwise>
                            </xsl:choose>
                        </xsl:when>
                        <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
                    </xsl:choose>
                </xsl:variable>
                <xsl:attribute name="{name(.)}"><xsl:value-of select="$newHref"/></xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
        
    </xsl:template>
</xsl:stylesheet>
