The <precondition> element

The patternOptimization element may contain one or more precondition children defining preconditions for template rules. The precondition element has the following attributes:

Attribute

Applies to

Values

Effect

mode

EE

xsl:modes

Modes for which this directive is relevant. Default #all.

pattern

EE

a pattern string

A pattern to match against a template match pattern!

to

EE

a pattern string

An expression to generate a precondition expression for the matched template rule. Variable values bound during the match of @pattern will be in scope for evaluation of this expression.

when

EE

expression

Qualification expression on expression variables from the pattern. Default true().

For details of the @pattern, @when and @mode properties, see Configurable rule optimization.

Preconditions are designed to be used when there is a boolean condition, common to many rules, that can be evaluated just once for a node, but the value of that condition can be reused across several rules. The intention is mainly to eliminate rules from any further detailed checking based on conditions shared with other rules.

A rule may have several preconditions - the rule will only be subjected to a full match if all its preconditions yield true. For example rules with the following patterns:

chapter/title[condition1], chapter/title[condition2], chapter/subtitle[condition1] chapter/para, chapter/section ...

All share a common requirement: exists(parent::chapter). Hence each can refer to this precondition whose truth need only be determined once for a given node, but referenced from each of these rules. Equally well, two of them share a second condition ( condition1 ) which adds to the set.

In some cases existence of a precondition (parent::a) can reduce the pattern - Saxon will attempt such reduction, such as a/*[expr] is reduced to *[expr] when the precondition is guaranteed.