XSLT - Templates

1 - About

A stylesheet contains a number of templates, defined with the <xsl:template> tag.

An XSLT template is a set of formatting instructions that apply to the nodes selected by an XPath expression.

3 - Syntax

In a stylesheet, an XSLT template would look something like this:

3.1 - Without parameter

<xsl:template match="/ARTICLE/SECT">
     <xsl:apply-templates select="text()|B|I|U|DEF|LINK"/> 
 <xsl:apply-templates select="SECT|PARA|LIST|NOTE"/> 


  • xsl:template defines a template
  • the match attribute select the (current|set of) node(s) by using an XPath addressing mechanisms. In this case, the (selected|current) node is the set of node /ARTICLE/SECT. It will select the set of nodes from the input stream.
  • The instruction <xsl:apply-templates>:
    • processes any templates that apply to children of the current node
    • causes the text of the current node to be processed.
  • The select attribute of the apply-template instruction select the tags that must be processed in a regular expression style (Note: text() is an Xpath function). Here a set of tag are processed first and the others afterwards. Using the select clause lets you apply all templates to a subset of the information available in the current context.

3.2 - With parameter

3.2.1 - The definition

The parameterized template gets executed only when you invoke it.

<xsl:template name="htmLink">
 <xsl:param name="dest" select="UNDEFINED"/> 
 <xsl:element name="a">
    <xsl:attribute name="href">
       <xsl:value-of select="$dest"/>


  • name is the name of the template (<note important>There is no match claus</note>)
  • the xsl:param tag defines a parameter with the default value (UNDEFINED)
  • the <xsl:value-of> tag extract the value of a node
  • xsl:apply-templates inserts the text from the text node

3.2.2 - The call

<xsl:call-template name="htmLink">
  <xsl:with-param name="dest" select="@target"/> 

The template definition must be below the call.

4 - Processing

A processor will process the stylesheet.

4.1 - Tag

XSLT works by transforming the source tree into the result tree. To visualize the result of XSLT operations, it is helpful to understand the structure of those trees, and their contents.

Within the template, tags that do not start with the xsl: namespace prefix are simply copied. The newlines and whitespace that follow them are also copied.

An XSLT stylesheet expects to process tags. So everything it sees needs to be either:

  • an <xsl:..> tag,
  • some other tag,
  • or whitespace.

In the example above case, the non-XSL tags are HTML tags. So when the root tag is matched, XSLT:

  • outputs the HTML start tags,
  • processes any templates that apply to children of the root,
  • and then outputs the HTML end tags.

4.2 - Order

Templates are independent of one another, so it does not generally matter where they occur in a file.

Order does make a difference when two templates can apply to the same node. In that case, the one that is defined last is the one that is found and processed.

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap