Re: xsl:key

Subject: Re: xsl:key
From: Michael French <mfrench@xxxxxxxxxxxxx>
Date: Wed, 3 May 2000 17:10:50 -0500
Michael French asks:
>Is there any way to key nodes from an external document() ?

David Marston wrote:
> I think the example given at the end of section 12.2 of the
> XSLT spec does what you want. This example is known to work
> with Xalan.

Scott Boag wrote:
> If you want to find key in another source tree, you have 
> to call the key() function inside a for-each which selects 
> a node using the document() function

I can now see how this would work, but the problem I have
(I think), is scalability and computational complexity. 

For example, I prefer:

   .. select="id('foo')" 


   .. select="//*[@id = 'foo']" 

because I assume id() is a hashtable lookup,
and //*[@id='foo'] is probably a loop over elements.

So I want to prefer:

  <xsl:key match="document('bar.xml')//bar" ...  >
  ..  select="key(...)"
over a solitary:

  .. select="document('bar.xml')//bar[..]"

because I assume the xsl:key is a hashtable (?).
Perhaps the extra generality of xsl:key mean it cannot
be a hashtable, and must always be a map or dynamic 
expression re-writer.

Maybe it's always dangerous to assume some underlying 
data structure and consequent performance characteristics 
without benchmarking a particular implementation !!?
But it is natural for developers to form some 
mental picture of the plausible implementations, 
and use that model to make decisions about the 
possible (in)efficiency of their stylesheets.

Looking at the XSLT spec end-of-12.2 example
(slightly modified to make name an attribute 
 for both <entry> and <bibref> elements):

What's the point of doing:

  <xsl:key name="bib" match="entry" use="@name"/>

  <xsl:template match="bibref">
    <xsl:variable name="name" select="@name"/>
    <xsl:for-each select="document('bib.xml')">
      <xsl:apply-templates select="key('bib',$name)"/>

rather than say:

  <xsl:template match="bibref">
    <xsl:variable name="name" select="@name"/>
     select="document('bib.xml')//entry[@name = $name]"/>

if the xsl:key is not a static hashtable ?

What I want to do is build a table and look up entries:

  <xsl:key name="bib" select="document('bib.xml')//entry" use="@name"/>
  <xsl:template match="bibref">
    <xsl:apply-templates select="key('bib',@name)"/>


 XSL-List info and archive:

Current Thread