[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: RE: [xsl] Using XSLT's key() function while context in secondary document?
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Mon, 18 Feb 2008 13:07:50 -0600

If you look at s.xml, and look at your 'chnam' key, you'll see that
chnam matches <Chnl> elements, of which there are none in s.xml.
Remember that key() returns key-matches from the document containing the
context node only. Does that make sense?

~ Scott


-----Original Message-----
From: Michael Ludwig [mailto:mlu@xxxxxxxxxxxxx]
Sent: Monday, February 18, 2008 1:04 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] Using XSLT's key() function while context in secondary
document?

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