Re: [xsl] How to create xsl:key that has a composite value in its "use" attribute?

Subject: Re: [xsl] How to create xsl:key that has a composite value in its "use" attribute?
From: "Roger L Costello costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sat, 15 Mar 2025 16:18:38 -0000
Thank you, Martin for pushing me to create a simple sample input XML document
along with the XSLT program. I have done so but cannot replicate the problem
that I described earlier. However, I uncovered another problem that I hope you
can help me with.

Previously I described a join between the <row> elements in <ANAV> with the
<row> elements in <BDRY>. Actually, there is a 3-way join: the <row> elements
in <ARPT> join with the <row> elements in <ANAV> which join with the <row>
elements in <BDRY>.

ARPT --> ANAV --> BDRY

I have found that this template:

<xsl:template match="ARPT/ARPT/row">
    <result>
        <xsl:for-each select="key('ARPT-to-ANAV', ARPT_IDENT)">
            <xsl:sequence
select="key('ANAV-to-BDRY',(NAV_IDENT,NAV_CTRY,NAV_TYPE))[TYPE eq
'08']/BDRY_IDENT"/>
      </xsl:for-each>
    </result>
</xsl:template>

Produces the correct output:

<Document>
   <result>
      <BDRY_IDENT>Boundary 1</BDRY_IDENT>
      <BDRY_IDENT>Boundary 2</BDRY_IDENT>
   </result>
   <result>
      <BDRY_IDENT>Boundary 3</BDRY_IDENT>
      <BDRY_IDENT>Boundary 4</BDRY_IDENT>
   </result>
</Document>

But when I do-it-all-in-one-step, using a key within a key:

<xsl:template match="ARPT/ARPT/row">
    <result>
         <xsl:sequence select="key('ANAV-to-BDRY',key('ARPT-to-ANAV',
ARPT_IDENT)/(NAV_IDENT,NAV_CTRY,NAV_TYPE))[TYPE eq '08']/BDRY_IDENT"/>
    </result>
</xsl:template>

Then I get incorrect output:

<Document>
   <result>
      <BDRY_IDENT>Boundary 1</BDRY_IDENT>
      <BDRY_IDENT>Boundary 2</BDRY_IDENT>
   </result>
   <result/>
</Document>

Why does the key-within-a-key version not work correctly?

Below is my XSLT program, along with the XML input document.

-------------------------------------------------------
              XSLT Program
-------------------------------------------------------
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:key name="ARPT-to-ANAV" match="ANAV/row" use="ARPT_IDENT"/>

  <xsl:key name="ANAV-to-BDRY" match="BDRY/row"
            use="NAV_IDENT,NAV_CTRY,NAV_TYPE "
            composite="yes"/>

  <xsl:template match="/*">
    <Document>
         <xsl:apply-templates select="ARPT/ARPT/row"/>
    </Document>
  </xsl:template>

  <xsl:template match="ARPT/ARPT/row">
    <result>
      <xsl:sequence select="key('ANAV-to-BDRY',key('ARPT-to-ANAV',
ARPT_IDENT)/(NAV_IDENT,NAV_CTRY,NAV_TYPE))[TYPE eq '08']/BDRY_IDENT"/>
      <!--<xsl:for-each select="key('ARPT-to-ANAV', ARPT_IDENT)">
        <xsl:sequence
select="key('ANAV-to-BDRY',(NAV_IDENT,NAV_CTRY,NAV_TYPE))[TYPE eq
'08']/BDRY_IDENT"/>
      </xsl:for-each>-->
    </result>
  </xsl:template>
</xsl:stylesheet>
-------------------------------------------------------
      Input XML Document
-------------------------------------------------------
<Document>
   <ARPT>
      <ARPT>
         <row>
            <ARPT_IDENT>ABC</ARPT_IDENT>
            <NAME>Airport 1</NAME>
         </row>
         <row>
            <ARPT_IDENT>DEF</ARPT_IDENT>
            <NAME>Airport 2</NAME>
         </row>
      </ARPT>
      <ANAV>
         <row>
            <ARPT_IDENT>ABC</ARPT_IDENT>
            <NAV_IDENT>HUM</NAV_IDENT>
            <NAV_CTRY>AR</NAV_CTRY>
            <NAV_TYPE>1</NAV_TYPE>
         </row>
         <row>
            <ARPT_IDENT>DEF</ARPT_IDENT>
            <NAV_IDENT>B</NAV_IDENT>
            <NAV_CTRY>US</NAV_CTRY>
            <NAV_TYPE>5</NAV_TYPE>
         </row>
         <row>
            <ARPT_IDENT>DEF</ARPT_IDENT>
            <NAV_IDENT>BNO</NAV_IDENT>
            <NAV_CTRY>US</NAV_CTRY>
            <NAV_TYPE>4</NAV_TYPE>
         </row>
      </ANAV>
   </ARPT>
   <BDRY>
      <BDRY>
         <row>
            <BDRY_IDENT>Boundary 1</BDRY_IDENT>
            <NAV_IDENT>HUM</NAV_IDENT>
            <NAV_CTRY>AR</NAV_CTRY>
            <NAV_TYPE>1</NAV_TYPE>
            <TYPE>08</TYPE>
         </row>
         <row>
            <BDRY_IDENT>Boundary 2</BDRY_IDENT>
            <NAV_IDENT>HUM</NAV_IDENT>
            <NAV_CTRY>AR</NAV_CTRY>
            <NAV_TYPE>1</NAV_TYPE>
            <TYPE>08</TYPE>
         </row>
         <row>
            <BDRY_IDENT>Boundary 3</BDRY_IDENT>
            <NAV_IDENT>BNO</NAV_IDENT>
            <NAV_CTRY>US</NAV_CTRY>
            <NAV_TYPE>4</NAV_TYPE>
            <TYPE>08</TYPE>
         </row>
         <row>
            <BDRY_IDENT>Boundary 4</BDRY_IDENT>
            <NAV_IDENT>BNO</NAV_IDENT>
            <NAV_CTRY>US</NAV_CTRY>
            <NAV_TYPE>4</NAV_TYPE>
            <TYPE>08</TYPE>
         </row>
      </BDRY>
   </BDRY>
</Document>

Current Thread