Q:

Record Count functoid returns aggregate count for non-flattend target message

I tried to use the Record Count functoid to map the number of sub-records of an record that itself occurs 0 to unbounded to a message with each record containing a field holding the number of sub-records:
root+                           +root
    |                           |
    +foo+                   +foo+
        |                   |
        +bar+ -RecordCount- barcount
            |
            +xyz

However my current map aggregates the count of all bar records and returns it in every foo\barcount.
Sample source message
<root>
    <foo>
        <Id>1</Id>
        <bar>
            <xyz />
        </bar>
        <bar>
            <xyz />
        </bar>
    </foo>
    <foo>
        <Id>2</Id>
        <bar>
            <xyz />
        </bar>
        <bar>
            <xyz />
        </bar>
    </foo>
</root>

... and the result is
<root>
    <foo>
        <Id>1</Id>
        <barcount>4</barcount>
    </foo>
    <foo>
        <Id>2</Id>
        <barcount>4</barcount>
    </foo>
</root>

... whereas I expected
<root>
    <foo>
        <Id>1</Id>
        <barcount>2</barcount>
    </foo>
    <foo>
        <Id>2</Id>
        <barcount>2</barcount>
    </foo>
</root>

A:

I solved this issue by replacing the Record Count functoid with a Call XSLT Template Scripting functoid.
The XSLT template looks like this:
<xsl:template name="CountMyBar">
    <xsl:param name="fooId" />
    <xsl:element name="barcount">
        <xsl:value-of select="count(//foo[Id=$fooId]/bar)" />
    </xsl:element>
</xsl:template>

and the input to the scripting functoid is the Id field from foo.

