xsl-list
[Top] [All Lists]

Re: xxRe: efficient use of key element and function

2005-09-07 08:31:48
Hi David,

in my original post I wrote (sorry for the typo "xxRe: [xsl] ..."):
Given a rather long list of unique names I want to know for a
certain name if it is on that list. The stylesheet below does
this, but is this the best (most efficient) solution? In the most
common case around 95% of the names will be found.

In the real stylesheet the names represent files and are used
to avoid a file-not-found for doc(). They are retrieved by means
of collection().

The stylesheet below is the original one, except for the
key element. Instead of
<xsl:key name="find" match="*" use="."/>     
it is now 
<xsl:key name="find" match="name" use="text()"/>
Is this the best (most efficient) solution?

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
           xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:key name="find" match="name" use="text()"/>      
<xsl:template match="/">
<xsl:variable name="Index-alt">
   <doc>
       <name>
           <xsl:text>schmid_hans</xsl:text>
       </name>
       <name>
           <xsl:text>prunauer_katharina</xsl:text>
       </name>
       <name>
           <xsl:text>senkenwald_wolfgang</xsl:text>
       </name>
   </doc>
</xsl:variable>
<xsl:value-of select="empty(key('find', 'prunauer_regina', $Index-alt))"/>
<xsl:value-of select="empty(key('find', 'strada_jacopo', $Index-alt))"/>
</xsl:template>
</xsl:stylesheet>


On 9/7/05, David Carlisle <davidc(_at_)nag(_dot_)co(_dot_)uk> wrote:

I was thinking the use attribute only comes into play _after_ a match
was found and in some way can be viewed as a return value (per node)

no you have that backwards, the use attribute specifies the allowe
_arguments_ to the key function, the match attribute specifies teh
returned value.

key('x',...) (in xslt1) is a function from strings to node sets

key('x','"foo") returns a set of nodes with key "foo".

The way this is function is defined is that the set of nodes returned is
the set of nodes that match the match attribute, and have a use
attribute that evaluates to "foo".

If might be better to match on text() than on *
For my test-case it should be the same.

They will only be the same if your input document only has one element
(and no comments).

If you have

<xsl:key match="*" use="."/>

Then the key for the top level element will be all the character data in
the document, concatenated. The system will then have to ake a hash
table entry using that large string as label.

key('x',string(/*)) would then return this element, but you would be a
lot more efficient just to use
/*
without using keys at all.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--