XSLT elements


This is used to allow the processor to process any imported templates that matches the context node

<!-- We need to wrap every movie element -->
<xsl:template match="movie">
  <div class="movie">
    <!-- Process the imported template for movie elements -->
    <xsl:apply-imports />


Collects the nodes specified by the select attribute and starts processing them using matching templates.

Using the mode attribute restricts the applied templates to only those with an identical mode attribute.

If no select attribute is present, it defaults to "*", i.e. the children of the current node.


Generates an attribute on the element being output. Must precede any content generated for an element (i.e.: text, child elements, comments etc.)

Can be used to override an attribute you've already set, as duplicate attribute names are not allowed in XML.


Used to build a set of attributes for reuse on multiple output elements. Use the special xsl:use-attribute-sets attribute on any literal output element, to tell the processor to generate these attributes on the element.


Explicitly call a named template (a template with a name attribute). The template will execute with the context at the point of this instruction.

If the template accepts arguments (parameters), use the with-param element to supply values to those.

<!-- Simple -->
<xsl:call-template name="headers" />

<!-- With argument -->
<xsl:call-template name="fibonacci">
	<xsl:with-param name="from" select="13" />


Similar to switch and if/elseif statements in other languages, this is used to choose between one or more outcomes. It's a bit verbose, and for simple A/B scenarios it's often possible to handle the logic in a single XPath statement instead.

Only the first when where the test returns true() will be executed (or, if no when-statements succeed, the contents of otherwise, if present).


Use this to generate a comment in the output.

This is necessary because a regular comment is in fact just a comment in the stylesheet, which is used to comment the code, and is in fact stripped by the XML parser before the XSLT processor gets the stylesheet.

<xsl:comment>This file is auto-generated</xsl:comment>


Defines a named custom format for use with the number instruction and the format-number() function.


Creates an element with a specific name.

Only necessary if you don't know the name of the element (e.g. if it's computed, like <xsl:element name="h{$level}">), or if you need to create the element in a specific namespace. Otherwise it's much better to just use the element tag directly (i.e., just use <div> instead of <xsl:element name="div">).


The contents of the fallback instruction is only instantiated if its parent element is not supported by the XSLT processor. Rarely used.


Almost an alias for apply-templates in that it also collects a set of nodes for processing, but all nodes will be processed using the same (embedded) template.



The import statement(s) must appear before any other child node of the main stylesheet.



Creates a named index of elements matching the expression in the match attribute. The value retrieved from the use attribute is stored as the index value.

To retrieve nodes from the index, use the XPath key() function.


Used to output debugging/logging info to stdout during transformation. If the terminate attribute is set to yes, the transformation will terminate.


Assigns an alias to an existing prefix so it's possible to generate literal result elements that would otherwise be interpreted as instructions (e.g. when generating an XSLT stylesheet).



Part of a choose construct - executed if none of the preceding when statements returned true().



Defines a value that can be used in many places, which should be easily changeable from e.g. the top of the file. Once defined, a parameter can be used by prefixing the name with a dollar-sign, e.g. <xsl:value-of select="$primary-color"> or <div class="{$selected-class}">.

A parameter is very similar to a variable, and none of them can be changed within the same transformation.

The difference between a parameter and a variable is that a parameter can be set from the XSLT Processor when invoking the transform.


Instructs the processor to preserve white-space nodes from specific elements in the source document before they're handled by XSLT.

The default behavior is to preserve white-space, so this element is provided so you can preserve specific elements' white-space, in case strip-space has been set to "all others" ("*").


Generates a Processing Instruction in the output; usually used for adding some kind of machine-info to the file.

A processing instruction can be selected in an XPath expression using the processing-instruction() function.

Although the name attribute isn't required, no output will be generated if it's missing.

<!-- Generates something like this: <?doc-version v0.9.3.4 ?> -->
<xsl:processing-instruction name="doc-version">
  <xsl:value-of select="$version" />


If present, this must be the first element inside a for-each or an apply-templates instruction.

Multiple sort elements are allowed.


Instructs the processor to remove all white-space nodes from specific elements in the source document before they're handled by XSLT.

You can use the value "*" to strip space from all elements.



A template defines a chunk of output. When using the match attribute, it can only be invoked by an apply-templates instruction (which can process multiple elements and their templates in a single operation), and is guaranteed to have the context of an element matching the pattern in the match attribute.

When using the name attribute, the template can be manually invoked by a call-template instruction, thus resembling a sub-routine in other languages.

The mode attribute facilitates multiple outputs for the same element, e.g. if an <article> needs a short version for the Table of Contents at the top of the output, and a longer version for the actual article content.

It's perfectly valid to have both a match and a name attribute on a template, if it makes sense. But you're required to use at least one of them.



This is an alias for the stylesheet element - almost never used.


This is used to generate a string value in the output by selecting something from the input document. The select attribute is an XPath expression - if it selects a node set, only the string value of the first node in the set will be output.

Though the disable-output-escaping attribute can be used to output text as HTML or XML, it is thus also possible to generate invalid XML - so be sure to know what the source contains before blindly transferring it to the output document.



Used in the choose construct to test a condition and if true(), execute the contents.

The condition in the test attribute is implicitly wrapped in a boolean() function to always return true() or false().


XPath functions


Convert the condition expression to either true() or false().

An empty string, an empty nodeset or the number 0 returns false(), anything else returns true().

Watch out for these pitfalls: ALL of these return true() when used in an expression that expects a boolean result, e.g., in the test attribute of a when or an if element: "1", "0", "true", "false"

concat(string1, string2, [stringN…])

Returns a string by concatenating the string value of all arguments

contains(source, find)

Returns true() if the source string contains the find string. Otherwise, it returns false().


Returns the number of nodes in the source nodeset

Often mistakenly used to check if a nodeset has any nodes, e.g.: <xsl:if test="count($children) &gt; 0"> which is not necessary at all, simply testing the nodeset itself will return true() if it has any nodes, and false() if it's empty - e.g.: <xsl:if test="$children">


Returns the current node

document(URL, [base])

Returns a nodeset pointing at the root node ("/") of the document found at the specified URL.


Returns the boolean value false

format-number(number, pattern, [decimal-format])


Returns true() if a function with the specified name exists.

You should include the prefix if the function is defined in another namespace, e.g.: function-available('msxsl:node-set')


Returns a unique string ID for the selected node (or the current node if none specified).


If the nodes in your document has a unique ID attribute (defined in the DOCTYPE), you can use the id function to select them with. The ids argument takes a whitespace separated string of IDs or a nodeset to take values from, and returns a nodeset containing all of the nodes referenced by their IDs.

<!-- Grab three specific nodes by their IDs -->
<xsl:variable name="static" select="id('4815 1623 4200')" />

<!-- Grab any node referenced in a `<nodeId>` child -->
<xsl:variable name="dynamic" select="id(nodeId)" />

key(name, value)

Returns a node set of all the nodes in the name index, with the value value stored. Used in conjunction with the key element to facilitate fast and easy lookups in the source XML.


Returns the name of the current node or the first node in the specified node set - without the namespace prefix


Returns the name of the current node or the first node in the specified node set


Returns a copy of the source string with leading and trailing space removed, as well as compressing runs of whitespace characters into a single space.

normalize-space(' a little tiny space ') returns: a little tiny space


Convert a nodeset, boolean value or string to a number


Returns the current node's index (1-based) within the current nodelist.


Returns a nodeset of the current context node's Processing Instruction children - if a name is specified, the list is filtered to only include those matching that.


Convert a value (nodeset, number, boolean or another string) to a string value

Converting a nodeset to a string will only return the string value of the first node in the set.


Returns the number of characters in the string.

substring(string, start, [length])

Returns part of (a) string starting at start, spanning at most length characters. If length isn’t specified, the rest of the string from index start will be returned.

Indexes in XPath/XSLT are 1-based, so the first character in a string can be found with: substring($string, 1, 1)

substring-after(source, find)

Returns the rest of the source string, following the find string.

substring-before(source, find)

Returns everything that comes before the find string inside the source string.

translate(source, find, replace)

Performs character-replacements in the source string, by replacing every character in the find argument found in source, with the character in replace at the same index


Returns the boolean value true

Fork me on GitHub