Wojtek
Wojtek

Reputation: 1530

How do I use a variable in XSL 2.0 xsl:key match attribute?

<xsl:variable name="dictFile" select="document('dictionary.xml')" />
<xsl:key name="lookupTable" match="$dictFile/dictionary/item" use="@name" />
<xsl:variable name="lookup1" select="key('lookupTable','lookupA')" />
<xsl:variable name="lookup2" select="key('lookupTable','lookupB')" />

This does not compile with Saxon 9.5 EE returning an error:

XTSE0340 XSLT Pattern syntax error at char 0 on line 55 in {$dictFile}:
A variable reference is not allowed in an XSLT pattern (except in a predicate)

Unfortunately, I need to keep those variables lookup1 and lookup2 defined as they are. I would like to change as little code here as possible. It is a very old and large codebase running on a custom enterprise XSL engine that supports these kinds of constructs .

Upvotes: 1

Views: 779

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

This exact scenario is discussed in the XSLT 2.0 specification and two solutions are offered (scroll down to the Example: Using Keys to Reference other Documents part).

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167401

The right way in standard XSLT 2.0 would be

<xsl:variable name="dictFile" select="document('dictionary.xml')" />

<xsl:key name="lookupTable" match="dictionary/item" use="@name" />

<xsl:variable name="lookup1" select="key('lookupTable','lookupA', $dictFile)" />
<xsl:variable name="lookup2" select="key('lookupTable','lookupB', $dictFile)" />

Upvotes: 3

Related Questions