あなたができることは、最初のXMLドキュメントのデータ要素の子要素を最初に一致させることです
<xsl:template match="datas/*">
次に、要素の「タイプ」と「位置」を変数に抽出します
<xsl:variable name="type" select="substring(local-name(), 1, 4)" />
<xsl:variable name="position" select="number(substring(local-name(), 5))" />
最後に、次のように 2 番目のドキュメント (この例では「test2.xml」と呼んでいます) で関連する名前を検索できます。
<xsl:apply-templates select="document('test2.xml')/results/result[type=$type][position()=$position]/value/text()" />
ここに完全な XSLT があります
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="datas/*">
<xsl:variable name="type" select="substring(local-name(), 1, 4)" />
<xsl:variable name="position" select="number(substring(local-name(), 5))" />
<xsl:copy>
<xsl:apply-templates select="document('test2.xml')/results/result[type=$type][position()=$position]/value/text()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
入力 XML に適用すると、次のように出力されます。
<datas>
<data1>john</data1>
<data2>tom</data2>
<data3>john</data3>
<name1>marc</name1>
<name2>marc</name2>
</datas>
編集: Martin Honnen がコメントで正しく指摘したように (Martin に感謝します!)、これはキーを使用する方が適切です。最初に次のようにキーを定義します。
<xsl:key name="lookup" match="result" use="type" />
次に、次のように 2 番目のドキュメントからテキストを検索できます。
<xsl:apply-templates
select="key('lookup', $type, document('test2.xml'))[$position]/value/text()" />
この XSLT も動作するはずです (XSLT 2.0 の場合)。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="lookup" match="result" use="type" />
<xsl:template match="datas/*">
<xsl:variable name="type" select="substring(local-name(), 1, 4)" />
<xsl:variable name="position" select="number(substring(local-name(), 5))" />
<xsl:copy>
<xsl:apply-templates
select="key('lookup', $type, document('test2.xml'))[$position]/value/text()" />
</xsl:copy>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>