Skip to content
Instructions

XSLT Instructions

xsl:template

Defines a template rule (match pattern) or named template.

<xsl:template match="book" priority="1">
  ...
</xsl:template>

<xsl:template name="header">
  <h1><xsl:value-of select="$title"/></h1>
</xsl:template>

Attributes: match, name, priority, mode, as

xsl:apply-templates

Selects nodes and applies matching template rules.

<xsl:apply-templates select="chapter">
  <xsl:sort select="@number" data-type="number"/>
  <xsl:with-param name="level" select="2"/>
</xsl:apply-templates>

Attributes: select (default: child::node()), mode

Supports xsl:sort and xsl:with-param as children.

xsl:call-template

Calls a named template.

<xsl:call-template name="header">
  <xsl:with-param name="title" select="'Hello'"/>
</xsl:call-template>

xsl:value-of

Evaluates an XPath expression and writes the string value to the output.

<xsl:value-of select="@name"/>
<xsl:value-of select="items" separator=", "/>

Attributes: select, separator

xsl:for-each

Iterates over a node sequence.

<xsl:for-each select="item">
  <xsl:sort select="@price" data-type="number" order="descending"/>
  <li><xsl:value-of select="."/></li>
</xsl:for-each>

Attributes: select

xsl:for-each-group

Groups items and iterates over groups.

<xsl:for-each-group select="employee" group-by="@department">
  <div>
    <h2><xsl:value-of select="current-grouping-key()"/></h2>
    <xsl:for-each select="current-group()">
      <p><xsl:value-of select="@name"/></p>
    </xsl:for-each>
  </div>
</xsl:for-each-group>

Attributes: select, group-by, group-starting-with

Available functions inside: current-group(), current-grouping-key().

xsl:if

Conditional execution.

<xsl:if test="@status = 'active'">
  <span class="active">Active</span>
</xsl:if>

xsl:choose / xsl:when / xsl:otherwise

Multi-branch conditional.

<xsl:choose>
  <xsl:when test="@type = 'book'">Book</xsl:when>
  <xsl:when test="@type = 'article'">Article</xsl:when>
  <xsl:otherwise>Unknown</xsl:otherwise>
</xsl:choose>

xsl:variable

Binds a variable in the current scope.

<xsl:variable name="count" select="count(item)"/>
<xsl:variable name="header"><h1>Title</h1></xsl:variable>
<xsl:variable name="nums" as="xs:integer*" select="1 to 10"/>

Attributes: name, select, as

xsl:param

Declares a parameter (on templates, functions, or at the stylesheet level).

<xsl:param name="page-size" select="'A4'"/>
<xsl:param name="items" as="element()*"/>

xsl:copy

Shallow copy of the current node.

<xsl:template match="*">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

xsl:copy-of

Deep copy of selected nodes.

<xsl:copy-of select="$fragment"/>

xsl:element

Creates an element with a computed name.

<xsl:element name="{$tag-name}" namespace="{$ns}">
  ...
</xsl:element>

xsl:attribute

Creates an attribute on the nearest enclosing element.

<xsl:attribute name="class" select="'highlight'"/>

xsl:text

Writes literal text to the output. Useful for controlling whitespace.

<xsl:text>Hello, World!</xsl:text>

With Text Value Templates (when expand-text="yes"):

<xsl:text>Hello, {$name}!</xsl:text>

xsl:sequence

Adds items to the current sequence constructor.

<xsl:sequence select="1 to 10"/>

xsl:comment

Creates an XML comment.

<xsl:comment>Generated on <xsl:value-of select="current-date()"/></xsl:comment>

xsl:processing-instruction

Creates a processing instruction.

<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="style.xsl"</xsl:processing-instruction>

xsl:number

Generates formatted numbers.

<xsl:number level="single" count="chapter" format="1. "/>
<xsl:number level="multiple" count="chapter|section" format="1.1 "/>
<xsl:number level="any" count="figure" format="I"/>

Attributes: level (single, multiple, any), count, from, format, value

xsl:message

Outputs a diagnostic message.

<xsl:message>Processing item <xsl:value-of select="@id"/></xsl:message>
<xsl:message terminate="yes">Fatal error</xsl:message>

xsl:result-document

Writes output to a secondary destination.

<xsl:result-document href="chapter-{@id}.html">
  <html>...</html>
</xsl:result-document>

xsl:analyze-string

Splits a string by a regular expression.

<xsl:analyze-string select="$input" regex="\d+">
  <xsl:matching-substring>
    <num><xsl:value-of select="."/></num>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
    <text><xsl:value-of select="."/></text>
  </xsl:non-matching-substring>
</xsl:analyze-string>

Available function inside xsl:matching-substring: regex-group($n).

xsl:map / xsl:map-entry

Constructs XPath 3.1 maps.

<xsl:variable name="config" as="map(*)">
  <xsl:map>
    <xsl:map-entry key="'color'" select="'red'"/>
    <xsl:map-entry key="'size'" select="12"/>
  </xsl:map>
</xsl:variable>

xsl:function

Defines a custom XPath function callable from expressions.

<xsl:function name="my:double" as="xs:integer"
    xmlns:my="http://example.com/my">
  <xsl:param name="n" as="xs:integer"/>
  <xsl:sequence select="$n * 2"/>
</xsl:function>

<!-- Usage: -->
<xsl:value-of select="my:double(21)"/>  <!-- 42 -->

xsl:key

Declares an index for efficient lookup via the key() function.

<xsl:key name="emp-by-dept" match="employee" use="@department"/>

<!-- Usage: -->
<xsl:for-each select="key('emp-by-dept', 'Sales')">
  ...
</xsl:for-each>

Attributes: name, match, use, composite