[xsl] Using XSLT's key() function while context in secondary document?

Subject: [xsl] Using XSLT's key() function while context in secondary document?
From: Michael Ludwig <mlu@xxxxxxxxxxxxx>
Date: Mon, 18 Feb 2008 20:03:57 +0100
I have a key I use to look up values. It works fine.

Now I process another document using XSLT's document() function. My key
stops working. What a pity.

Why does it not work? Can I work around this situation?

xsltproc a.xsl x.xml

#### x.xml - principal document
<XPG>
  <Channels>
    <Chnl ID="ard">ARD</Chnl>
    <Chnl ID="zdf">ZDF</Chnl>
  </Channels>
</XPG>

#### s.xml - document pulled in via XSLT's document() function
<BcStat>
  <Channel>zdf</Channel>
  <Channel>ard</Channel>
  <Channel>zdf</Channel>
</BcStat>

#### a.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
 <xsl:output method="xml" indent="yes"/>
 <xsl:variable name="pntr" select="/XPG/Channels/Chnl"/>
 <xsl:key name="chnam" match="Chnl" use="@ID"/>
 <xsl:template match="/">
  <Urmel>
   <p><xsl:value-of select="key('chnam', 'ard')"/> key works</p>
   <xsl:for-each select="document('s.xml')//Channel">
    <div>
     <p>
      <xsl:value-of select="$pntr[ @ID = current() ]"/>
      <xsl:text> variable works</xsl:text>
     </p>
     <p><xsl:value-of select="key('chnam', current())"/> key doesn't</p>
     <p><xsl:value-of select="key('chnam', .)"/> NO</p>
     <p><xsl:value-of select="key('chnam', string())"/> NO</p>
     <p><xsl:value-of select="key('chnam', 'ard')"/> NO</p>
    </div>
   </xsl:for-each>
  </Urmel>
 </xsl:template>
</xsl:stylesheet>

Michael

Current Thread