<?xml version="1.0"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="chart"/>
    <xsl:template match="record[@id]">
        <xsl:choose>
            <xsl:when test="@chart=$chart or not(@chart)">
                <xsl:copy>
                    <xsl:attribute name="id">
                        <xsl:value-of select="attribute::id"/>_<xsl:value-of select="$chart"/>
                    </xsl:attribute>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:choose>
            <xsl:when test="name()='id' or name()='chart'"></xsl:when>
            <xsl:when test="name()='ref' and not(starts-with(., 'tax_group'))" >
                <xsl:attribute name="ref">
                    <xsl:value-of select="."/>_<xsl:value-of select="$chart"/>
                </xsl:attribute>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    <xsl:template match="node()">
        <xsl:choose>
            <xsl:when test="@chart=$chart or not(@chart)">
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:when>
        </xsl:choose>
    </xsl:template>
</xsl:transform>
