What Does exclude-result-prefixes Do in XSLT?
The exclude-result-prefixes attribute in XSLT prevents
unnecessary namespace declarations from polluting the final transformed
XML or HTML output. When you declare namespaces in a stylesheet for
extension functions, schema references, or intermediate processing, XSLT
processors automatically copy those namespace declarations to literal
result elements by default. Specifying these prefixes in
exclude-result-prefixes strips out the redundant
declarations while keeping the output clean and valid.
Why Unwanted Namespaces Occur
When an XSLT stylesheet creates literal result elements—such as standard HTML tags or custom output XML nodes—the processor copies all in-scope namespace declarations to the output document. This copying ensures that any elements or attributes requiring those namespaces remain valid.
However, many stylesheets declare namespaces solely for stylesheet-internal purposes. Common examples include:
- Extension namespaces (e.g., EXSLT or processor-specific extensions)
- Utility namespaces used for custom stylesheet functions or variables
- Input-specific namespaces that have no role in the target output structure
Without intervention, every root or container element in the output
may inherit cluttered declarations like
xmlns:ext="[http://exslt.org/common](http://exslt.org/common)"
or
xmlns:custom="[http://example.com/logic](http://example.com/logic)".
Syntax and Usage
You typically define exclude-result-prefixes directly on
the root <xsl:stylesheet> or
<xsl:transform> element as a whitespace-separated
list of prefix names.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:my="http://example.com/my-functions"
exclude-result-prefixes="xs fn my">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<item>Clean output without unwanted namespaces.</item>
</root>
</xsl:template>
</xsl:stylesheet>In this example, the output <root> element will
only contain the required structure without emitting
xmlns:xs, xmlns:fn, or
xmlns:my.
Special Keywords and Scoping
Modern XSLT versions provide additional flexibility for excluding namespaces:
#default: Excludes the default namespace (where an element is declared without a prefix, such asxmlns="[http://example.com/default](http://example.com/default)").#all(XSLT 2.0+): Excludes all in-scope namespaces that are not explicitly required by elements or attributes in the result document.- Element-level exclusion: The attribute can also be placed on individual literal result elements to scope the namespace exclusion to specific branches of the output tree.
Important Limitations
The exclude-result-prefixes attribute acts as a
suppression directive, not an absolute override of XML validity rules.
If an element or attribute in the generated output actually uses a
prefix listed in exclude-result-prefixes, the XSLT
processor will still emit the namespace declaration. This behavior
ensures that the processor never generates malformed XML with undefined
prefixes.