RE: [xsl] How to efficiently remove "a" nodes with no "b" descend ants

Subject: RE: [xsl] How to efficiently remove "a" nodes with no "b" descend ants
From: "Michael Kay" <mhkay@xxxxxxxxxxxx>
Date: Fri, 9 Mar 2001 10:32:54 -0000
> If I say
>
> > <xsl:template match="a[not(.//b)]"/>
>
> then all the descendants of "a" are checked for "b" elements. Let's
> have a closer look at the input structure.

> This presupposes that the XSLT processor you are using does
> no optimisation, which may or may not be true.
>

Optimizers look for "quick wins", constructs that are used sufficiently
often to be worth treating specially, and I suspect this isn't one of them.

Generally, it's bad news to put a complex test like this into a match
pattern, because there is little alternative to testing each node selected
by <apply-templates> against this pattern; there is more scope to optimise a
node-set expression than a pattern.

You might find that a more efficient approach is

<xsl:variable name="ancestors-of-b" select="//b/ancestor::*"/>
<xsl:variable name="anc-of-b-count" select="count($ancestors-of-b)"
<xsl:template match="a">
<xsl:if test="count(.|ancestors-of-b) != $anc-of-b-count">
 ...

Mike Kay


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


Current Thread