I’m using xsl for a XML transformation and I have to use multiple lookups for that. Lookup xml’s have following format.
<lookups>
<lookup attr1="val1a" attr2="val1b">
<lookup attr1="val2a" attr2="val2b">
<lookups>
All lookup xmls use above format. for example lookup file for element A is there and another lookup file for element B is also there with same element structure but different values which are relevant to element B. Then I’m doing my lookups in main xsl file as follows.
<!-- here come the first lookup for element A -->
<xsl:key name="A-lookup" match="lookup" use="@attr1"/>
<xsl:variable name="ALookupDoc" select="document('ALookup.xml')"/>
<xsl:template match="A">
<Anew>
<xsl:apply-templates select="$ALookupDoc/*">
<xsl:with-param name="curr-code" select="string(.)"/>
</xsl:apply-templates>
</Anew>
</xsl:template>
<xsl:template match="lookups">
<xsl:param name="curr-code"/>
<xsl:value-of select="key('A-lookup', $curr-code)/@attr2"/>
</xsl:template>
<!-- And the second one for element B -->
<xsl:key name="B-lookup" match="lookup" use="@attr1"/>
<xsl:variable name="BLookupDoc" select="document('BLookup.xml')"/>
<xsl:template match="B">
<Anew>
<xsl:apply-templates select="$BLookupDoc/*">
<xsl:with-param name="curr-code" select="string(.)"/>
</xsl:apply-templates>
</Anew>
</xsl:template>
<xsl:template match="lookups">
<xsl:param name="curr-code"/>
<xsl:value-of select="key('B-lookup', $curr-code)/@attr2"/>
</xsl:template>
OK now. the problem is first lookup which is for the element A works fine. But not the second one. As I see there are two problematic parts.
- Both lookups have same element names(lookups and lookup). Once I change second lookup XML to differnt elements(lookupsX and lookupX) and templates accordingly they work fine.
- There are two templates for same element(lookups for element A and B)
Still I’m not sure exactly where the problem is.
I think the problem is that you have two templates with exactly the same
matchvalue, so only one of them is actually being used. You could resolve this by giving them different modes, but how about something like this:Then again, you might not need two keys, since they both have the same definition. Please see if this works: