XSLT - Default Template Rules in XSLT

1. Default Template Rules in XSLT

When you don’t write a template for certain XML nodes, XSLT doesn’t stop — instead, it applies built-in default templates defined by the XSLT specification. These act like a safety net.


The Default Templates

(a) For the root (/) and element nodes

<xsl:template match="*|/">
  <xsl:apply-templates/>
</xsl:template>
  • Meaning:
    If there’s no explicit template for an element (or the root), XSLT recursively applies templates to its children.


(b) For text nodes and attribute nodes

<xsl:template match="text()|@*">
  <xsl:value-of select="."/>
</xsl:template>
  • Meaning:
    If you didn’t write a template for text or attributes, XSLT will output their values.


(c) For comments, processing instructions, etc.

  • These are ignored by default (no output).


2. Effect of Default Rules

  • They make sure your XML always produces some output, even without custom templates.

  • By default, XSLT just prints all text content from the XML, stripping the tags.


3. Example

XML:

<library>
  <book>1984</book>
  <book>Brave New World</book>
</library>

XSLT with no templates:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
</xsl:stylesheet>

Output (because of default templates):

1984Brave New World
  • It walked through <library> and <book> elements (default match="*|/" rule),

  • then printed the text inside them (match="text()" rule).


4. Paraphrased Explanation (Simplified)

  • In XSLT, you normally write <xsl:template> rules to decide how elements should be transformed.

  • But if you don’t, XSLT doesn’t just quit — it uses built-in default templates.

  • These defaults:

    • For elements: keep processing their children.

    • For text and attributes: print out their values.

    • For comments and processing instructions: ignore them.

  • The result is that, by default, XSLT will strip tags and just output the text content of the XML.

  • This system ensures transformations don’t break even if you don’t handle every possible node explicitly.

In short:
Default template rules are the fallback behavior in XSLT. They make sure text gets output and children keep being processed, even if you don’t write any templates yourself.