The example is a little ambiguous. Assuming your input can have multiple parent
nodes, each linked to multiple child
nodes, I would suggest you use a key to resolve the cross-references:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="child" match="child" use="parentId" />
<xsl:template match="/main">
<xsl:copy>
<xsl:apply-templates select="parent"/>
</xsl:copy>
</xsl:template>
<xsl:template match="parent">
<xsl:copy>
<xsl:copy-of select="*"/>
<xsl:apply-templates select="key('child', parentId)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="child">
<xsl:copy>
<xsl:copy-of select="*[not(self::parentId)]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>