[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Can xsl:key use a globally declared variable?

Subject: 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
 Can xsl:key use a globally declared variable?
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.