Try something like this (you seem to be using XSLT 2.0):
<xsl:variable name="rulesdoc" select="/"/>
<xsl:variable name="testdoc" select="document('test.xml')"/>
<xsl:key name="mapping" match="xpath" use="@attribute1"/>
<xsl:template match="/">
<xsl:apply-templates select="$testdoc/*"/>
</xsl:template>
<xsl:template match="test">
<xsl:copy>
<xsl:for-each select="*">
<xsl:element name="{key('mapping', name(), $rulesdoc)/@attribute2}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference