<?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" 
    xmlns:xd="http://www.pnp-software.com/XSLTdoc"
    xmlns:xf="http://www.pnp-software.com/xf"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output method="xml"/>
    
    <xd:doc>
        This matches any node.
    </xd:doc>
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xd:doc>
        Expands the part to the feature in ordet to make it the same is it will appear in the application meta-model.
    </xd:doc>
    <xsl:function name="xf:famToApp">
        <xsl:param name="node"/>
        <xsl:choose>
            <xsl:when test="(local-name($node) = 'FeatureModel') or (local-name($node/..) = 'FeatureModel')">
                <xsl:value-of select="concat('/fm:FeatureModel/fm:',$node/@fm:value)"/>
            </xsl:when>
            <xsl:when test="local-name($node/../..) = 'MacroFeatureNode'">
                <!--<xsl:value-of select="'foo'"/>-->
                <xsl:variable name="apos" select='"&apos;"'/>
                <xsl:variable name="part1" select="'//*[starts-with(local-name(.),'"/>
                <xsl:variable name="part2" select="$node/@fm:value"/>
                <xsl:variable name="part3" select="')]'"/>
                
                <xsl:value-of select="concat($part1, $apos, $part2, $apos, $part3)"/>
                <!--<xsl:value-of select="concat(xf:famToApp($node/..),'/')"/>-->
            </xsl:when>
            <xsl:when test="local-name($node) = 'FeatureModel'
                or local-name($node) = 'GroupedFeatureNode'
                or local-name($node) = 'SolitaryFeatureNode'">
                <xsl:value-of select="concat(xf:famToApp($node/..),'/fm:',$node/@fm:value)"/>
            </xsl:when>
            <xsl:when test="local-name($node) = 'AttributePropertySet'">
                <xsl:variable name="set" select="concat('/fm:',$node/fm:NameProperty/@fm:value, 'PropertySet/fm:',$node/fm:NameProperty/@fm:value, 'Property/@fm:value')"/>
                <xsl:value-of select="concat(xf:famToApp($node/..),$set)"/>
            </xsl:when>   
            <xsl:otherwise>
                <!-- do nothing, just recurse -->
                <xsl:value-of select="concat(xf:famToApp($node/..),'')"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:function>
    
    <xd:doc>
        Copy everything except, well, elements with fm:value attribute.
    </xd:doc>
    <xsl:template match="*[not (@fm:value)]" priority="-5">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
    <xd:doc>
        Handle the elements with fm:value attribute.
    </xd:doc>
    <xsl:template match="*[@fm:value]" priority="0">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:attribute name="xf:gcValue" select="xf:famToApp(.)"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

