I have two lists of consecutive elements that relate to each other. I want to combine them, but my solution is both slow and not elegant. I am using XSLT 2.0, Saxon.
List1.xml:
<data>
<w tag="a">asda</w>
<w tag="c">sdsd</w>
<w tag="a">value2</w>
<w tag="f">fdxcc</w>
<w tag="c">no</w>
</data>
List2.xml:
<data>
<w class="2">asda</w>
<w class="5">sdsd</w>
<w class="6">value2</w>
<w class="1">fdxcc</w>
<w class="2">no</w>
</data>
Note that the values of neither @class, @tag, or content of the elements are unique; what links them is identical contents and identical sequence. (And note that the actual problem is more complicated, since I need to evaluate the elements of the first list using those of the second.)
Intended result (same order:)
<w tag="a" class="2">asda</w>
<w tag="c" class="5">sdsd</w>
<w tag="a" class="6">value2</w>
<w tag="f" class="1">fdxcc</w>
<w tag="c" class="2">no</w>
Now the obvious way to acchieve this is just to walk through one list and pick up the values from the second. I do this like this:
<xsl:template match="/">
<xsl:variable name="list1" select="doc('list1.xml')">
<xsl:variable name="list2" select="doc(*list2.xml')">
<xsl:for-each select="$list1//w">
<xsl:copy>
<xsl:copy-of select="@tag"/>
<xsl:variable name="thispos" select="position()"/>
<xsl:copy-of select="$list2//w[position()=$thispos]/@id"/>
<xsl:copy-of select="@text()"/>
</xsl:copy>
</xsl:for-each>
I have two questions: (a) is there really no better way to refer to the position in $list1 than to save it in a variable? (b) related to this question: this solution is MUCH too slow when dealing with hundreds of thousands of items. What would be a better solution?