RE: [xsl] Testing if something is not there

Subject: RE: [xsl] Testing if something is not there
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Tue, 6 Nov 2001 15:33:41 -0000
> Some XSLT processors
> detect when the same expression is used several times and caches the
> result of the expression (e.g. Saxon caches it if you use it three
> times), but using a variable forces the expression to only be
> evaluated once.

Not strictly true; the processor has a choice whether to evaluate the
variable at the time it is declared, when it is first referenced, or each
time it is referenced.

Saxon currently evaluates all variables except node-set variables at the
time they are declared. For node-set variables, it evaluates the select
expression the first three times the variable is referred to; the third
time, it saves the value of the variable in memory. This is a time/space
trade-off: for many simple node-set expressions such as
"following-sibling::*", allocating space to hold the value is more expensive
than re-evaluating it on each reference.

Saxon never (currently) saves the results of an expression unless the value
is assigned to a variable: except for the special case of "//element-name".
It would be possible to save results of other expressions, of course, but
it's quite tricky to work out exactly under what circumstances it's safe
(because the context can change from one evaluation to another), and it's
also tricky to work out under what circumstances it's likely to be

What Saxon does try to do is to avoid repeated evaluation of expressions
used e.g. in a predicate or sort key. For example if you write
"//item[@name=//param/x]", Saxon will evaluate "//param/x" once only,
because it knows that the context document will be the same for each node in

There's scope here for a lot more intelligence, but the limiting factor is
that optimization is done with no knowledge of the source document structure
or volumetrics. The other limiting factor is that I've got to the point
where I don't want to do any more optimization, because the software is fast
enough for most purposes, and new optimizations always introduce new bugs.

Mike Kay

 XSL-List info and archive:

Current Thread