[xsl] Can xsl:key use a globally declared variable?

Subject: [xsl] Can xsl:key use a globally declared variable?
From: "Costello, Roger L. costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 16 Nov 2018 15:47:09 -0000
Hi Folks,

I have an airports document containing data for several airports:

<airports>
    <row>
        <icao>KBOS</icao>
        <name>Boston Logan Airport</name>
    </row>
    <row>
        <icao>KIAD</icao>
        <name>Washington Dulles Airport</name>
    </row>
</airports>

I want my XSLT program to output the <row> element for the airport with icao =
KBOS.

I declare a variable to hold the airports document:

<xsl:variable name="airport-file" select="doc('airports.xml')"/>

I declare an xsl:key to index the rows, using the <icao> element to identity
the desired <row>:

<xsl:key name="airports-with-icao" match="$airport-file/airports/row"
use="icao"/>

In a template I declare a variable to hold the icao of the desired airport:

<xsl:variable name="airport-icao" select="'KBOS'"/>

I use the key() function to obtain the desired <row>:

<xsl:sequence select="key('airports-with-icao', $airport-icao)" />

The output is empty. Why? What am I doing wrong? How to fix it?

Below is my complete XSLT program.  /Roger

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                           version="2.0">

    <xsl:output method="xml" />

    <xsl:variable name="airport-file" select="doc('airports.xml')"/>

    <xsl:key name="airports-with-icao" match="$airport-file/airports/row"
use="icao"/>

    <xsl:template match="/">
        <results>
            <xsl:variable name="airport-icao" select="'KBOS'"/>
            <xsl:variable name="Boston-airport-version1"
select="key('airports-with-icao', $airport-icao)"/>
            <version1-results>
                <xsl:sequence select="$Boston-airport-version1" />    <!--
This produces no output -->
            </version1-results>

            <xsl:variable name="Boston-airport-version2"
select="$airport-file/airports/row[icao eq $airport-icao]"/>
            <version2-results>
                <xsl:sequence select="$Boston-airport-version2" />    <!--
This produces the desired output -->
            </version2-results>
        </results>
    </xsl:template>

</xsl:stylesheet>

Current Thread