XSLT elements

<xsl:apply-imports>

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 />
  </div>
</xsl:template>

<xsl:apply-templates>

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.

<xsl:attribute>

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.

<xsl:attribute-set>

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.

<xsl:call-template>

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" />
</xsl:call-template>

<xsl:choose>

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).

<xsl:comment>

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>

<xsl:decimal-format>

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

<xsl:element>

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">).

<xsl:fallback>

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

<xsl:for-each>

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.

<xsl:if>

<xsl:import>

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

<xsl:include>

<xsl:key>

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.

<xsl:message>

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

<xsl:namespace-alias>

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).

<xsl:number>

<xsl:otherwise>

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

<xsl:output>

<xsl:param>

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.

<xsl:preserve-space>

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" ("*").

<xsl:processing-instruction>

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" />
</xsl:processing-instruction>

<xsl:sort>

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

Multiple sort elements are allowed.

<xsl:strip-space>

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.

<xsl:stylesheet>

<xsl:template>

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.

<xsl:text>

<xsl:transform>

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

<xsl:value-of>

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.

<xsl:variable>

<xsl:when>

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().

<xsl:with-param>

XPath functions

boolean(condition)

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().

count(source)

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">

current()

Returns the current node

document(URL, [base])

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

false()

Returns the boolean value false

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

function-available(function)

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')

generate-id([node])

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

id(ids)

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.

local-name([source])

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

name([source])

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

normalize-space([source])

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

number(source)

Convert a nodeset, boolean value or string to a number

position()

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

processing-instruction([name])

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.

string(value)

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.

string-length(string)

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

true()

Returns the boolean value true

Fork me on GitHub