xsl-list
[Top] [All Lists]

Re: A general <xsl:key> question.

2002-12-05 16:24:13
Jeni Tennison wrote:

Hi Edward,

Do keys have to be used within a context in which the associated
nodes exist?

Yes. The context node at the point where you use the key (with the
key() function) has to be a node in the document that you're
searching.

Usually this means that you should create a variable to hold the key
value that you want to search for, then switch into the context of the
document that you want to search in (using <xsl:for-each>) before
using the key.

I can't tell precisely what's going to work with your code, but
something like:

<xsl:key name="PageKey" match="/gnsl:Results/gnsl:Table/gnsl:Row"
         use="((count(preceding-sibling::*) div $RowsPerPage) + 1)" />

<xsl:variable name="results" select="/" />

<xsl:template name="ProcessPageRows">
   <xsl:variable name="PageNum" select="@PageNum" />
   <xsl:variable name="StartRow" select="@StartRow" />
   <xsl:variable name="EndRow" select="@EndRow" />

   <xsl:if test="$NumberOfRows">
      <xsl:for-each select="$results">
        <xsl:apply-templates select="key('PageKey',$PageNum)" />
      </xsl:for-each>
   </xsl:if>
</xsl:template>

You need to set the $results variable to being a node set containing a
single node in the document in which the gnsl:Row elements that you're
interested in are held.

Jeni Tennison
http://www.jenitennison.com/

Worked, have to do the performance test to see what I gained.  Had to
adjust the syntax a bit; (forcing the key to be an integer helped a
bunch):

<xsl:key name="PageKey" match="/gnsl:Results/gnsl:Table/gnsl:Row"
         use="floor(count(preceding-sibling::gnsl:Row) div $RowsPerPage)
+ 1" />
<xsl:variable name="Results" select="/" />

<xsl:template name="ProcessPageRows">
   <xsl:variable name="PageNum" select="@PageNum" />
   <xsl:if test="$NumberOfRows">
      <xsl:for-each select="$Results">
      <xsl:for-each select="key('PageKey',$PageNum)">

         <xsl:apply-templates select="." />

      </xsl:for-each>
      </xsl:for-each>
   </xsl:if>
</xsl:template>

Thanks for the help,
Ed

-- 
Edward L. Knoll   Phone (work)     : (719)484-2717
                  e-mail (work)    : 
ed(_dot_)knoll(_at_)cosd(_dot_)fedex(_dot_)com
                  e-mail (business): eknoll(_at_)sf-inc(_dot_)com
                  e-mail (personal): edward(_at_)elknoll(_dot_)com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list