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.
<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.
Prove the fix
- Test the selectProve the candidate node sequence independently of template dispatch.
- Fix namespace matchingMake match patterns target the source vocabulary.
- Review XSLT structureConnect templates, modes, variables, instructions, and output declarations.
Related guides
- Make XSLT match a default namespaceFix XSLT templates that never fire when source elements use a default namespace, with patterns that work across XSLT versions.
- Debug XSLT output, escaping, and serializationSeparate the XSLT result tree from its serialized bytes and fix wrong output methods, encodings, escaped markup, and indentation assumptions.
- XPath predicates, position(), and parenthesesPredict which node set an XPath predicate filters, distinguish numeric from boolean predicates, and select the first match at the intended scope.
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.