Here is a sample stylesheet using xsl:for-each-group to group and merge the Contact elements, taking the first child element for all child elements besides the AddressIds child element, where the string-join() of all such elements in a group is taken:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="Contact_CSV">
<xsl:copy>
<xsl:copy-of select="header"/>
<xsl:for-each-group select="Contact" group-by="contactid">
<xsl:copy>
<xsl:copy-of select="AddressIds/preceding-sibling::*"/>
<AddressIds>
<xsl:value-of select="string-join(current-group()/AddressIds, '|')"/>
</AddressIds>
<xsl:copy-of select="AddressIds/following-sibling::*"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>