Here is a small example how what you want can be done without using an <xsl:for-each>
instruction:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vDoc">
<root>
<elem id="1"/>
<elem id="2"/>
<elem id="3"/>
</root>
</xsl:variable>
<xsl:template match="somenode">
<xsl:apply-templates select="$vDoc/*/elem[@id eq current()/@id]">
<xsl:with-param name="porigElem" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="elem">
<xsl:param name="porigElem" as="element()"/>
Match:
Original: <xsl:sequence select="$porigElem"/>
Matching: <xsl:sequence select="."/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided source XML document (reordered, to match the description of the problem):
<node>
<somenode id="3"/>
<somenode id="1"/>
<somenode id="2"/>
</node>
The wanted, correct result is produced:
Match:
Original: <somenode id="3"/>
Matching: <elem id="3"/>
Match:
Original: <somenode id="1"/>
Matching: <elem id="1"/>
Match:
Original: <somenode id="2"/>
Matching: <elem id="2"/>
Of course, if one wants to use keys, as recommended in the answer of Dr. Kay, the key must be defined, and then the applying of templates will look like this:
<xsl:apply-templates select="key('kelemById', @id, $vDoc)">
<xsl:with-param name="porigElem" select="."/>
</xsl:apply-templates>
The complete transformation now is:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kelemById" match="elem" use="@id"/>
<xsl:variable name="vDoc">
<root>
<elem id="1"/>
<elem id="2"/>
<elem id="3"/>
</root>
</xsl:variable>
<xsl:template match="somenode">
<xsl:apply-templates select="key('kelemById', @id, $vDoc)">
<xsl:with-param name="porigElem" select="."/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="elem">
<xsl:param name="porigElem" as="element()"/>
Match:
Original: <xsl:sequence select="$porigElem"/>
Matching: <xsl:sequence select="."/>
</xsl:template>
</xsl:stylesheet>
and when this transformation is applied on the same source XML document, the same wanted and correct result is produced.