Re: [xsl] Are xsl:key's best going into the future?

Subject: Re: [xsl] Are xsl:key's best going into the future?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Fri, 15 Mar 2002 15:32:49 +0000
Hi Robert,

> In fact, if you want to get the incredible performance gains from
> XSLTC, they: [ Xalan's XSLTC team -
> http://xml.apache.org/xalan-j/xsltc/xsltc_performance.html ] warn
> that you should not use keys.
>
> Should keys be avoided?

Actually, what that document says is that keys shouldn't be used *in
patterns*. The example they give is:

<xsl:template match="key('key-name', 'some-value')">
  ...
</xsl:template>

Here, the template matches nodes that have the value 'some-value'
according to the key 'key-name'.

Assuming that the key is simply using the id attribute of elements:

<xsl:key name="key-name" match="*" use="@id" />

The equivalent key-lesss way to express such a pattern is something
like:

<xsl:template match="node()[@id = 'some-value']">
  ...
</xsl:template>

As you can see from the preceding performance tip on the same page,
using predicates within patterns is a bad idea for good performance
with XSLTC as well.

The reason that both of these patterns are discouraged is because they
are type-less matches. From their description, with both keys and the
predicates, XSLTC can't quickly work out what kind of nodes could be
matched by the pattern, so it has to look through the whole document
to work it out.

This use of keys to match nodes with patterns is very different from
using keys to *select* nodes with an expression. There's nothing on
the XSLTC performance page that indicates that keys shouldn't be used
to select nodes, so I don't see any reason not to use them in that
way. For example:

  <xsl:apply-templates select="key('some-key', 'some-value')" />

Cheers,

Jeni

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


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


Current Thread