How XSLT Priority Resolves Template Conflicts

When processing XML documents using XSLT, multiple template rules often match the same node, creating a conflict. The XSLT processor resolves these conflicts through a strict hierarchy based on import precedence, template priority, and document order. By explicitly defining the priority attribute on an xsl:template, developers can override default calculation rules and precisely control which template executes when multiple patterns match.

The Conflict Resolution Hierarchy

When an XSLT processor evaluates nodes against templates, it selects the winning template using the following order of evaluation:

  1. Import Precedence: Templates defined in the primary stylesheet take precedence over templates brought in via <xsl:import>.
  2. Explicit Priority: If two conflicting templates have the same import precedence, the processor checks for an explicit numeric priority attribute. The template with the higher numerical value is selected.
  3. Computed Default Priority: If no explicit priority is set, the processor automatically calculates a default priority based on the complexity and specificity of the template’s match pattern.
  4. Declaration Order: If two matching templates share the exact same precedence and priority, XSLT processors either report a static error or resolve the ambiguity by selecting the template that appears last in the stylesheet.

How the Priority Attribute Works

The priority attribute accepts any real number, including positive values, negative values, and decimals (e.g., priority="2", priority="0.5", priority="-1").

<!-- Higher priority: this template executes for <item type="special"> -->
<xsl:template match="item[@type='special']" priority="2">
    <div class="highlight"><xsl:apply-templates/></div>
</xsl:template>

<!-- Lower priority: this template will be skipped for special items -->
<xsl:template match="item" priority="1">
    <div class="standard"><xsl:apply-templates/></div>
</xsl:template>

A higher numeric value always takes precedence over a lower one, regardless of where the templates appear in the file or how specific their XPath match patterns are.

Computed Default Priorities

When the priority attribute is omitted, the XSLT specification assigns a default priority between -0.5 and +0.5 based on pattern specificity:

Best Practices for Managing Conflicts