I am aware that there are plenty of questions (and answers) here on the lookup from external xml files via xslt. However, I still haven't got my head around the logic of the key function so I'm having a hard time applying other solutions to my use case.
I have two xml files:
versA.xml
<TEI>
<div>
<l id="A001" corresp="B001">First line of VersA</l>
<l id="A002" corresp="B002">Second line of VersA</l>
<l id="A003" corresp="B003">Third line of VersA</l>
</div>
</TEI>
and
versB.xml
<TEI>
<div>
<l id="B001" corresp="A001">First line of VersB</l>
<l id="B002" corresp="A002">Second line of VersB</l>
<l id="B003" corresp="A003">Third line of VersB</l>
</div>
</TEI>
The files refer to each other via the corresp
-attribute.
I'm trying to figure out a xsl stylesheet (trans.xsl) that parses versA.xml, prints its own text node and then looks up the corresponding text node in versB.xml
trans.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:variable name="vB" select="document('versB.xml')/TEI/div/l"/>
<xsl:template match="/TEI/div/l">
I found ID <xsl:value-of select="@id"/> in versA.xml.
How can I get the corresponding node in versB.xml which has the ID <xsl:value-of select="@corresp"/>?
</xsl:template>
</xsl:stylesheet>
What I can do is output the ids of versA.xml and access versB.xml. I find it extremely difficult however, to set up an appropriate key function that takes the corresp
value from versA.xml to look up the corresponding id in versB.xml
I'd be happy if somebody could explain how this can be achieved.
For compatibility reasons xslt version 1.0 would be preferred.
I have updated my stylesheet according to the suggestions given in the comments. The following gives the desired output:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="ref" match="TEI/div/l" use="@id"/>
<xsl:template match="/TEI/div/l">
<xsl:variable name="corresp" select="@corresp"/>
<xsl:value-of select="."/> corresponds to
<xsl:for-each select="document('versB.xml')">
<xsl:value-of select="key('ref', $corresp)"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>