XSLT Templates

Estimated study time: 12 minutes.

A template is the core building block of an XSLT stylesheet — a rule pairing an XPath pattern with the output to produce whenever a matching node is found.

Basic Template Structure

<xsl:template match="module">
  <li><xsl:value-of select="."/></li>
</xsl:template>

The match attribute is an XPath pattern; whenever the processor encounters a node matching it, this template's content is output.

Applying Templates to Children

<xsl:template match="modules">
  <ul>
    <xsl:apply-templates select="module"/>
  </ul>
</xsl:template>

apply-templates tells the processor to find the matching template for each selected child node and run it — this is how templates chain together to process a whole document.

Named Templates

<xsl:template name="courseHeader">
  <h1>Course Catalog</h1>
</xsl:template>

<xsl:call-template name="courseHeader"/>

Named templates work like reusable functions, called explicitly by name rather than triggered by a pattern match.

Template Priority

When multiple templates could match the same node, XSLT uses specificity rules to decide which one wins — a more specific match (e.g. course/title) takes priority over a general one (e.g. *).

💡 Tip: Break your stylesheet into small, focused templates rather than one giant template — it mirrors how the document's structure is nested and is far easier to maintain.

Ready to go beyond the basics?

Get hands-on training, live mentorship, and placement support with the Data Analytics Course at Uncodemy.

Explore XML Course →