It's a bit hard to see what's going on because the XML you posted isn't well-formed.
I would expect your "root" template not to process all the child projects, but only the one that you know to be a top-level project. And your code for processing a project by finding its logical children looks quite wrong:
<xsl:apply-templates select="/root/project[$pid=current()]">
I would have expected to see
<xsl:apply-templates select="/root/project[parent_id=$pid]">
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference