You want something like this:
<xsl:template match="/">
<xsl:apply-templates select="/NODES/NODE[parent='Root']"/>
</xsl:template>
<xsl:template match="NODE">
<NODE>
<xsl:copy-of select="*"/>
<xsl:apply-templates select="/NODES/NODE[parent=current()/id]"/>
</NODE>
</xsl:template>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference