How Does XSLT Compute Default Template Priority?

When an XSLT template rule does not include an explicit priority attribute, the XSLT processor automatically assigns a default priority based on the structural specificity of its match pattern. This built-in resolution mechanism assigns numeric scores—typically ranging from -0.5 to +0.5—to ensure more specific patterns (such as exact qualified names or expressions with predicates) override general wildcard or node-type selectors. Understanding these exact calculation rules prevents unexpected template collisions during document transformations.

The Priority Calculation Rules

The W3C XSLT specification defines the default priority computation based on the syntax of the pattern. When a pattern consists of multiple alternatives separated by the union operator (|), each branch is evaluated separately as if each had its own distinct template rule with its own computed priority.

For single-pattern steps, the processor applies four standard numeric tiers:

Conflict Resolution and Overrides

If multiple templates match a given node with the same default priority, the processor follows strict conflict resolution rules:

  1. Explicit Priority: An author can override the default priority by adding the priority attribute (e.g., <xsl:template match="para" priority="2">). Any explicit numerical priority takes precedence over default calculations.
  2. Import Precedence: Templates in importing stylesheets always take precedence over templates in imported stylesheets, regardless of the calculated default priority.
  3. Declaration Order: If two matching templates share the exact same import precedence and calculated priority, the processor either raises an error or chooses the template that appears last in the stylesheet declaration order.