XSLT Functions

XSLT Functions

XSLT (Extensible Stylesheet Language Transformations) provides a wide variety of built-in functions for working with data, manipulating strings, and performing calculations. Additionally, the we've added some important functions from the EXSLT (Extended Stylesheet Language Transformations) project to enhance XSLT's capabilities. Below is an organized list of the functions available in the Export Module.

String Functions

These functions allow you to manipulate and query strings in XSLT.

string()

Converts a value to a string.
  1. <xsl:value-of select="string(123)"/>
Output: 123

concat()

Concatenates two or more strings.
  1. <xsl:value-of select="concat('Hello', ' ', 'World')"/>
Output: Hello World

substring()

Extracts a substring from a string starting at a specified position.
  1. <xsl:value-of select="substring('Hello World', 7, 5)"/>
Output: World

substring-before()

Returns the part of the string before the first occurrence of a specified substring.
  1. <xsl:value-of select="substring-before('Hello World', ' ')"/>
Output: Hello

substring-after()

Returns the part of the string after the first occurrence of a specified substring.
  1. <xsl:value-of select="substring-after('Hello World', ' ')"/>
Output: World

contains()

Checks if a string contains a specified substring.
  1. <xsl:value-of select="contains('Hello World', 'World')"/>
Output: true

starts-with()

Checks if a string starts with a specified substring.
  1. <xsl:value-of select="starts-with('Hello World', 'Hello')"/>
Output: true

normalize-space()

Removes leading and trailing whitespace and replaces consecutive spaces within a string with a single space.
  1. <xsl:value-of select="normalize-space('  Hello   World  ')"/>
Output: Hello World

str:padding()

Pads a string to a specified length with a given character.
  1. <xsl:value-of select="str:padding('Hello', 10, '-')"/>
Output: Hello-----

str:replace()

Replaces occurrences of a substring in a string.
  1. <xsl:value-of select="str:replace('Hello World', 'World', 'XSLT')"/>
Output: Hello XSLT

str:split()

Splits a string into an array based on a specified delimiter.
  1. <xsl:value-of select="str:split('one,two,three', ',')"/>
Output: A sequence of items: one, two, three

str:sanitize()

Escapes special characters in a string based on the file type being exported (e.g., XML, CSV, HTML).
  1. <xsl:value-of select="str:sanitize('This & that < 5')"/>
Output (for XML): This &amp; that &lt; 5

Boolean Functions

These functions return true or false values and are used in conditional logic.

boolean()

Converts a value to a boolean (true or false).
  1. <xsl:value-of select="boolean(123)"/>
Output: true (since 123 is not zero or blank)

not()

Returns the negation of a boolean value.
  1. <xsl:value-of select="not(123)"/>
Output: false (since 123 is truthy)

true()

Returns the boolean value true.
  1. <xsl:value-of select="true()"/>
Output: true

false()

Returns the boolean value false.
  1. <xsl:value-of select="false()"/>
Output: false

Number Functions

These functions perform arithmetic calculations and manipulate numerical values.

number()

Converts a value to a number.
  1. <xsl:value-of select="number('123.45')"/>
Output: 123.45

sum()

Calculates the sum of a node set.
  1. <xsl:value-of select="sum(/price)"/>
Output: The sum of all price values in the document.

floor()

Returns the largest integer less than or equal to a given number.
  1. <xsl:value-of select="floor(12.75)"/>
Output: 12

ceiling()

Returns the smallest integer greater than or equal to a given number.
  1. <xsl:value-of select="ceiling(12.75)"/>
Output: 13

round()

Rounds a number to the nearest integer.
  1. <xsl:value-of select="round(12.5)"/>
Output: 13

abs()

Returns the absolute value of a number.
  1. <xsl:value-of select="abs(-12.75)"/>
Output: 12.75

math:max()

Returns the maximum value from a set of numbers.
  1. <xsl:value-of select="math:max(3, 5, 7)"/>
Output: 7

math:min()

Returns the minimum value from a set of numbers.
  1. <xsl:value-of select="math:min(3, 5, 7)"/>
Output: 3

math:highest()

Returns the node with the highest value from a node set.
  1. <xsl:value-of select="math:highest(/price)"/>
Output: <price item="Computer">1000.00</price>

math:lowest()

Returns the lowest value from a node set.
  1. <xsl:value-of select="math:lowest(/price)"/>
Output: <price item="Gum">1.00</price>

math:random()

Generates a random number between 0 and 1.
  1. <xsl:value-of select="math:random()"/>

Node Set Functions

These functions are used to work with node sets, such as selecting XML elements.

last()

Returns the position of the last node in a node set.
  1. <xsl:value-of select="last()"/>
Output: The position of the last node.

position()

Returns the position of the current node within the node set.
  1. <xsl:value-of select="position()"/>
Output: The position of the current node in the node set.

count()

Returns the number of nodes in a node set.
  1. <xsl:value-of select="count(//book)"/>
Output: The number of book elements in the document.

id()

Selects nodes with a specific ID.
  1. <xsl:value-of select="id('book1')"/>
Output: The element with the ID book1.

Date and Time Functions

These functions are used to parse and processing date values.

date:now()

Returns the current date and time in the specified pattern.
<xsl:value-of select="date:now('dd/MM/yy)"/>
Output: Current date and time like 25-12-24.

date:formatDate()

Formats a date according to a specified pattern.
<xsl:value-of select="date:formatDate(date:now(), 'yyyy-MM-dd')"/>
Output: A formatted date like 2024-12-16.

date:ticks()

Returns the number of ticks (milliseconds) since January 1, 1970.
<xsl:value-of select="date:ticks()"/>
Output: Current timestamp in milliseconds.

date:add()

Adds a specified number of units (e.g., days, months, etc.) to a date.
<xsl:value-of select="date:add(date:now(), 'P1D')"/>
Output: Current date plus one day.

Common Custom Format Specifiers for Day, Month, and Year

When formatting dates, you can use the following custom format specifiers to represent the day, month, and year:
  1. dd: Two-digit day of the month (e.g., 16).
  2. d: Single-digit day of the month without leading zero if it's less than 10 (e.g., 6).
  3. MM: Two-digit month (e.g., 12).
  4. M: Single-digit month without leading zero if it's less than 10 (e.g., 5).
  5. yyyy: Four-digit year (e.g., 2024).
  6. yy: Two-digit year (e.g., 24).

Next Steps

Now that you're familiar with the functions available in XSLT, it's time to apply your knowledge to real-world transformations. Here are some next steps to help you get started with different data formats:
  1. XML: Learn how to transform XML data into different structures using XSLT. Check out our Getting Started writing an XML File page for detailed guides and examples.
  2. JSON: Explore how to convert XML data into JSON format. Visit the Getting Started writing a JSON File page for step-by-step instructions and use cases.
  3. CSV: Understand how to create CSV files from XML data. Head over to the Getting Started writing a CSV File page for a comprehensive introduction.
These pages will guide you through the basics of working with these formats and how to leverage XSLT for efficient transformations.

    • Related Articles

    • An Introduction to XSLT

      XSLT 1.0 (Extensible Stylesheet Language Transformations) is a versatile language designed for transforming XML data into various output formats, including CSV, JSON, and XML. As a core part of the W3C’s XML family of technologies, XSLT enables users ...
    • Data Export Module - Resources List

      To help you get started and make the most of the Export Module, we offer a comprehensive set of resources. These articles cover everything from basic setup to advanced usage, ensuring that users of all experience levels can benefit from the module's ...
    • XSLT Basic Syntax

      XSLT 1.0 is a declarative programming language designed to transform XML documents into other formats, such as XML, HTML, CSV, or JSON. Its template-driven approach allows developers to define how data should be reorganized and outputted. In the ...
    • Creating a CSV Export

      Developing the export definition uses XSLT 1.0 as a processor to generate the text out. As you are developing, we encourage you to frequently preview the result to understand how your code is working. The source of the preview data is displayed in ...
    • Creating a JSON Export

      Developing the export definition uses XSLT 1.0 as a processor to generate the text out. As you are developing, we encourage you to frequently preview the result to understand how your code is working. The source of the preview data is displayed in ...