title: Problem using xslt key() function
body: <p>(edited to include suggestions from Martin Honnen)</p>
  
  <p>Hello All,</p>
  
  <p>I've been trying to get the key function to work correctly in the stylesheet below.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet version="1.0" 
          xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
          xmlns="http://www.w3.org/1999/xhtml"
          xmlns:date="http://www.oracle.com/XSL/Transform/java/java.util.Date"
          xmlns:fo="http://www.w3.org/1999/XSL/Format"
          xmlns:m="http://mapping.tables" &gt;
  
  
  &lt;xsl:key name="preferences" match="preferences/preference" use="@code"/&gt;
  
  &lt;xsl:template match="Reservation/Detail" &gt;
      &lt;xsl:for-each select="Preferences/Preference"&gt;
          &lt;xsl:if test ="string-length(./PreferenceCode)&gt;0"&gt;
  
              &amp;#160;&amp;#160;&amp;#160;&amp;#160;* (&lt;xsl:value-of select="./PreferenceCode"/&gt;)
  
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="./PreferenceCode!='PETS'"&gt;
  
                      &lt;xsl:call-template name="prefmap"&gt;
                              &lt;xsl:with-param name="code" select="./PreferenceCode"/&gt;
                      &lt;/xsl:call-template&gt;
  
                      &lt;br/&gt;&lt;br/&gt;
                  &lt;/xsl:when&gt;
              &lt;/xsl:choose&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template
  
  
  &lt;xsl:template name="prefmap"&gt;
          &lt;xsl:param name="code"/&gt;
          You got here (called template) with code &lt;xsl:value-of select="$code"/&gt;
                &lt;xsl:for-each select="document('')"&gt;
          &lt;xsl:value-of select="key('preferences',$code)"/&gt;
      &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;
  
  &lt;m:Maps xmlns=""&gt;
  &lt;preferences&gt;
          &lt;preference code="ANT"&gt;
                  Hypoallergenic Bedding
          &lt;/preference&gt;
          &lt;preference code="NSK"&gt;
                  Non-smoking Room
          &lt;/preference&gt;
          &lt;preference code="SMK"&gt;
                  Smoking Room
          &lt;/preference&gt;
  &lt;/preferences&gt;
  &lt;/m:Maps&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>It takes a input (that I don't control) and produces this:</p>
  
  <pre><code>* (ANT) You got here (called template) with code ANT
  
  
  * (EARLY) You got here (called template) with code EARLY
  
  
  * (NSK) You got here (called template) with code NSK
  </code></pre>
  
  <p>When I was expecting:</p>
  
  <pre><code>* (ANT) You got here (called template) with code ANT
    Hypoallergenic Bedding
  
  * (EARLY) You got here (called template) with code EARLY
  
  * (NSK) You got here (called template) with code NSK
  Non-smoking Room
  </code></pre>
  
  <p>I tried to include this snippet in the main template to debug, but it produced no output:</p>
  
  <pre><code>            &lt;xsl:for-each select="key('preferences',./PreferenceCode)"&gt;
                  &lt;p&gt;
                      Code: &lt;xsl:value-of select="@code"/&gt;&lt;br /&gt;
                      Description: &lt;xsl:value-of select="."/&gt;
                  &lt;/p&gt;
              &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>Do I have a problem with my key definition or the way that I am trying to use it?</p>
  
  <p>Thanks all in advance.</p>