<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:fmm="http://www.pnp-software.com/XFeature/fmm"
    xmlns:fm="http://www.pnp-software.com/XFeature/fmm"
    xmlns:dm="http://www.pnp-software.com/XFeature/displayModel" >
    <xsl:output method="xml"/>
    <!-- ========================================================================= -->
    <!-- This matches any node -->
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <!-- ========================================================================= -->    
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="fm:GroupNode">
        <xsl:variable name="gName" select="@fm:value"/>
        <xsl:variable name="num" select="count(preceding::fm:GroupNode[@fm:value=$gName] | ancestor::fm:GroupNode[@fm:value=$gName])"/>
        <xsl:copy>
            <xsl:copy-of select="@*[name() != 'fm:value']"/><!-- not(@fm:value) -->
            <xsl:choose>
                <xsl:when test="count(//fm:GroupNode[@fm:value=$gName])&gt;1">
                    <xsl:attribute name="fm:value">
                        <xsl:value-of select="concat(@fm:value,'__GN',$num)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="@fm:value"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="fm:GroupedFeatureNode">
        <xsl:variable name="fName" select="@fm:value"/>
        <xsl:variable name="num" select="count(preceding::fm:GroupedFeatureNode[@fm:value=$fName] | ancestor::fm:GroupedFeatureNode[@fm:value=$fName] | preceding::fm:SolitaryFeatureNode[@fm:value=$fName] | ancestor::fm:SolitaryFeatureNode[@fm:value=$fName])"/>
        <xsl:copy>
            <xsl:copy-of select="@*[name() != 'fm:value']"/>
            <xsl:choose>
                <xsl:when test="count(//fm:SolitaryFeatureNode[@fm:value=$fName]) + count(//fm:GroupedFeatureNode[@fm:value=$fName]) &gt; 1">
                    <xsl:attribute name="fm:value">
                        <xsl:value-of select="concat(@fm:value,'__GFN',$num)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="@fm:value"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="fm:SolitaryFeatureNode">
        <xsl:variable name="fName" select="@fm:value"/>
        <xsl:variable name="num" select="count(preceding::fm:SolitaryFeatureNode[@fm:value=$fName] | ancestor::fm:SolitaryFeatureNode[@fm:value=$fName] | preceding::fm:GroupedFeatureNode[@fm:value=$fName] | ancestor::fm:GroupedFeatureNode[@fm:value=$fName])"/>
        <xsl:copy>
            <xsl:copy-of select="@*[name() != 'fm:value']"/>
            <xsl:choose>
                <xsl:when test="count(//fm:GroupedFeatureNode[@fm:value=$fName]) + count(//fm:SolitaryFeatureNode[@fm:value=$fName]) &gt; 1">
                    <xsl:attribute name="fm:value">
                        <xsl:value-of select="concat(@fm:value,'__SFN',$num)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:copy-of select="@fm:value"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="fm:Annotation">
        <xsl:variable name="num" select="count(preceding::fm:Annotation | ancestor::fm:Annotation)"/>
        <xsl:element name="fm:Annotation__{$num}">
            <xsl:copy-of select="@*[name() != 'fm:value']"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="fm:DesignMapping">
        <xsl:variable name="num" select="count(preceding::fm:DesignMapping | ancestor::fm:DesignMapping)"/>
        <xsl:element name="fm:DesignMapping__{$num}">
            <xsl:copy-of select="@*[name() != 'fm:value']"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>

<!--    <xsl:template match="fm:Attribute">
        <xsl:variable name="num" select="count(preceding::fm:Attribute | ancestor::fm:Attribute)"/>
        <xsl:element name="fm:Attribute__{$num}">
            <xsl:copy-of select="@*[name() != 'fm:value']"/>
            <xsl:apply-templates/>
        </xsl:element>
    </xsl:template>-->
    
</xsl:stylesheet>
