>Basically, I want to move all the <A> nodes that precede each <MainNode>, inside the <Mainnode> as children nodes of it.
That sort of hints that there might be <A> nodes that don't precede the MainNode, though your example doesn't show any.
Anyway, there are basically two rules here:
(a) if you find an A node before a MainNode, drop it
Code:
<xsl:template match="A[following-sibling::MainNode]"/>
(b) if you find a MainNode, copy its contents followed by all the preceding A nodes
[code]
<xsl:template match="MainNode">
<MainNode>
<xsl:copy-of select="child::node()"/>
<xsl:copy-of select="preceding-sibling::A"/>
</MainNode>
</xsl:template>
[code]
>I know I am going to be using xsl:For-each,
No: try to avoid xsl:for-each. Try to structure your code in terms of rules: "why I hit an XXX element, what should I do with it?"