Skip to main content

Search 412 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, guides, blog and product.

XSLT guide

XSLT match vs select: know which expression runs when

Distinguish template patterns from XPath selections, trace modes and priority, and fix stylesheets that choose nodes but run the wrong rule.

8 min read · Updated 2026-08-21

The short answer

match is a pattern that declares which nodes can invoke a template rule; select is an XPath expression that chooses values or nodes for an instruction. xsl:apply-templates uses select to form a sequence and then chooses the winning matching template in the active mode. xsl:for-each also selects nodes, but processes its own body instead of dispatching template rules.

Follow selection and dispatch as two steps

When xsl:apply-templates runs, its select expression chooses candidate items relative to the current context. For each item, the processor finds template rules whose match patterns and mode apply, resolves precedence and priority, and invokes one rule.

Selection followed by template dispatch
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="orders">
    <xsl:apply-templates select="order[@status='open']" mode="summary"/>
  </xsl:template>
  <xsl:template match="order" mode="summary">
    <p><xsl:value-of select="@id"/></p>
  </xsl:template>
</xsl:stylesheet>

Check context, mode, and priority

A correct XPath can still appear to fail when it is evaluated from a different current node. A matching rule can also be invisible because apply-templates uses another mode or a more specific rule wins conflict resolution.

  • Log or inspect the current node before evaluating a relative select.
  • Match the mode on apply-templates and the template rule exactly.
  • Look for imported stylesheets and higher import precedence.
  • Use explicit priority sparingly when default priority is not obvious.

Choose templates for extensible transformations

Use apply-templates when behavior should be distributed by node kind, mode, or imported customization. Use for-each for a genuinely local operation whose body should not be overridden. Both can sort; the design difference is dispatch.

When output is missing, prove the select count, add one catch-all template in the active mode, then inspect which specific rule wins. This narrows selection bugs separately from template-conflict bugs.

Get started

Bring order to the XML your team can't afford to ignore.

Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.