Utility functions¶
Utility functions help you perform common operations on your data within your data mappings. You make them available by including the xmlns:su="com.spinque.tools.importStream.Utils" namespace:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:spinque="com.spinque.tools.importStream.EmitterWrapper"
xmlns:su="com.spinque.tools.importStream.Utils"
extension-element-prefixes="spinque">
<xsl:template match="table/row"/>
</xsl:stylesheet>
Below is a description of the most commonly used utility functions:
Identifiers¶
su:uri¶
Generates a URI based on a base and one to four paths provided.
If you want to create another type of identifier, use function su:e.
Input parameters¶
| parameter | type | description |
|---|---|---|
| base | String | The base of the URI to be created |
| path1 | String | A path in the URI that comes after `base` |
| path2 | String | A path in the URI that comes after `path1` |
| path3 | String | A path in the URI that comes after `path2` |
| path4 | String | A path in the URI that comes after `path3` |
Returns¶
A string containing the full URI.
Examples¶
<spinque:debug message="{su:uri('https://imdb.com', 'data')}" />
<spinque:debug message="{su:uri('https://imdb.com', 'data', 'movie')}" />
<spinque:debug message="{su:uri('https://imdb.com', 'data', 'movie', 'tt0068646')}" />
<spinque:debug message="{su:uri('https://imdb.com', 'data', 'movie', 'tt0068646', 'otherIdentifierPath')}" />
generates the debug output
https://imdb.com/data
https://imdb.com/data/movie
https://imdb.com/data/movie/tt0068646
https://imdb.com/data/movie/tt0068646/otherIdentifierPath
Standard string modifications¶
su:capitalize¶
Reformats a string value with specified capitalization style.
Input parameters¶
| parameter | type | description |
|---|---|---|
| value | String | The string to format |
| style | String | The capitalization style (e.g. `sentencecase`) |
Returns¶
A string with content reformatted according to the capitalization style
Examples¶
<spinque:debug message="{su:capitalize('this becomes sentencecase, 'sentencecase')}"/>
<spinque:debug message="{su:capitalize('this becomes titlecase', 'titlecase')}"/>
<spinque:debug message="{su:capitalize('This becomes allcaps', 'allcaps')}"/>
<spinque:debug message="{su:capitalize('This becomes ALLLOWERCASE', 'alllowercase')}"/>
<spinque:debug message="{su:capitalize('this becomes camelcase', 'camelcase')}"/>
<spinque:debug message="{su:capitalize('This becomes snakecase', 'snakecase')}"/>
<spinque:debug message="{su:capitalize('This becomes kebabcase', 'kebabcase')}"/>
generates the debug output
This becomes sentencecase
This Becomes Titlecase
THIS BECOMES ALLCAPS
this becomes allowercase
ThisBecomesCamelcase
this_becomes_snakecase
this-becomes-kebabcase
su:lowercase¶
Turns all alphabetic characters in a string content lowercase
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string in which all alphabetic characters will become lowercase |
Returns¶
A string where all alphabetic characters have become lowercase
Examples¶
<spinque:debug message="{su:lowercase('ThiS wilL bEcome lowercase!')}"/>
generates the debug output
this will become lowercase!
su:normalizeWhiteSpace¶
Replaces all occurrences of one or more consecutive whitespace characters by a single space. It also trims whitespace from the start and end of the string.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string in which normalization will happen |
Returns¶
The string with whitespaces normalized
Examples¶
<spinque:debug message="{su:normalizeWhiteSpace(' Normalize the whitespaces! ')}"/>
generates the debug output
Normalize the whitespaces!
su:replace¶
Replaces occurrences of needle in the content string with the replacement string repl.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The original string where replacements may occur |
| needle | String | The substring to be replaced |
| repl | String | The string to replace each match with |
Returns¶
A new string with all instances of needle replaced by repl
Examples¶
<spinque:debug message="{su:replace('hello world','world', 'there')}"/>
<spinque:debug message="{su:replace('abc','d', 'D')}"/>
generates the debug output
hello there
abc
su:replaceAll¶
Replaces all matches of the regular expression regex in the content string with the replacement string repl.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The original string where replacements may occur |
| regex | String | The regular expression to be replaced |
| repl | String | The string to replace each match with |
Returns¶
A new string with all occurrences of regex replaced by repl
Examples¶
<spinque:debug message="{su:replaceAll('a1b2c3','\d', '*')}"/>
<spinque:debug message="{su:replaceAll('a1b2c34d567','\d{2}', '*')}"/>
generates the debug output
a*b*c*
a1b2c*d*7
su:split¶
Splits a string content into tokens according to a separator regular expression.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to be split |
| separatorRegex | String | The separator at which `content` will be split, provided as a regular expression. If left empty (`''`), `content` will be split into separate characters. |
| limit | String | Optional. The maximum number of splits (maximum is 8192) |
Returns¶
A string array of tokens
Examples¶
<xsl:variable name="splits" select="su:split('This string needs to be split!', '\s', 3)"/>
<xsl:template match="/">
<spinque:debug message="{splits[1]}"/>
<spinque:debug message="{splits[2]}"/>
<spinque:debug message="{splits[3]}"/>
</xsl:template>
generates the debug output
This
string
needs to be split!
and
<xsl:variable name="splits" select="su:split('This string needs to be split!', '', 4)"/>
<xsl:template match="/">
<spinque:debug message="{splits[1]}"/>
<spinque:debug message="{splits[2]}"/>
<spinque:debug message="{splits[3]}"/>
<spinque:debug message="{splits[4]}"/>
</xsl:template>
generates the debug output
T
h
i
s string needs to be split!
su:trim¶
Removes all leading and trailing whitespace characters (such as spaces, tabs, and newlines) from the input string content.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The input string from which whitespaces will be removed |
Returns¶
A new string with all leading and trailing whitespace removed from content.
Example¶
<spinque:debug message="{su:trim(' hello world ')}"/>
generates the debug output
hello world
su:uppercase¶
Turns all alphabetic characters in a string content uppercase
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string in which all alphabetic characters will become uppercase |
Returns¶
A string where all alphabetic characters have become uppercase
Examples¶
<spinque:debug message="{su:uppercase('This will become uppercase!')}"/>
generates the debug output
THIS WILL BECOME UPPERCASE!
Extracting substrings¶
su:substringAfter¶
Extracts everything of the string content after the first occurrence of the substring needle.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| needle | String | The substring to find in `content` |
Returns¶
A string that contains the part of content after the first occurrence of needle. If content does not contain needle, return value is empty.
Examples¶
<spinque:debug message="{su:substringAfter('hello world', 'world')}"/>
<spinque:debug message="{su:substringAfter('hello world', 'hello')}"/>
<spinque:debug message="{su:substringAfter('hello world', 'there')}"/>
<spinque:debug message="{su:substringAfter('hello world, what a beautiful world', 'world')}"/>
generates the debug output
world
, what a beautiful world
(where the first and the third return values are empty)
su:substringAfterLast¶
Extracts everything of the string content after the last occurrence of the substring needle.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| needle | String | The substring to find in `content` |
Returns¶
A string that contains the part of content after the last occurrence of needle. If content does not contain needle, return value is empty.
Examples¶
<spinque:debug message="{su:substringAfterLast('hello world', 'world')}"/>
<spinque:debug message="{su:substringAfterLast('hello world', 'hello')}"/>
<spinque:debug message="{su:substringAfterLast('hello world', 'there')}"/>
<spinque:debug message="{su:substringAfterLast('hello world, what a beautiful world', 'world')}"/>
generates the debug output
world
(where all return values but the second are empty)
su:substringBefore¶
Extracts everything of the string content before the first occurrence of the substring needle.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| needle | String | The substring to find in `content` |
Returns¶
A string that contains the part of content before the first occurrence of needle. If content does not contain needle, return value is empty.
Examples¶
<spinque:debug message="{su:substringBefore('hello world', 'world')}"/>
<spinque:debug message="{su:substringBefore('hello world', 'hello')}"/>
<spinque:debug message="{su:substringBefore('hello world', 'there')}"/>
<spinque:debug message="{su:substringBefore('hello world, what a beautiful world', 'world')}"/>
generates the debug output
hello
hello
(where the second and third return values are empty)
su:substringBeforeLast¶
Extracts everything of the string content before the last occurrence of the substring needle.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| needle | String | The substring to find in `content` |
Returns¶
A string that contains the part of content before the last occurrence of needle. If content does not contain needle, return value is empty.
Examples¶
<spinque:debug message="{su:substringBeforeLast('hello world', 'world')}"/>
<spinque:debug message="{su:substringBeforeLast('hello world', 'hello')}"/>
<spinque:debug message="{su:substringBeforeLast('hello world', 'there')}"/>
<spinque:debug message="{su:substringBeforeLast('hello world, what a beautiful world', 'world')}"/>
generates the debug output
hello
hello world, what a beautiful
(where the second and third return values are empty)
Matching strings¶
su:endsWith¶
Checks whether a String content ends with the specified suffix.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to be searched in. |
| suffix | String | The string to search for at the end of `content`. |
Returns¶
true if content ends with suffix; otherwise, false.
Examples¶
<spinque:debug message="{su:endsWith('abc','c')}"/>
<spinque:debug message="{su:endsWith('abc','d')}"/>
generates the debug output
true
false
su:findMatches¶
Find all matches of the given expression, and return the content of those matches.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| regex | String | A regular expression |
Returns¶
An array of strings containing the individual matches of regex in content.
Examples¶
<xsl:variable name="matches" select="su:findMatches('hello world', '[a-z]+')"/>
<xsl:template match="/">
<spinque:debug message="{$matches[1]}"/>
<spinque:debug message="{$matches[2]}"/>
</xsl:template>
generates the debug output
hello
world
su:lastIndexOf¶
Determines the last position of where the needle occurs in the content
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to search in |
| needle | String | The string to find in `content` |
Returns¶
An integer: If needle is found inside content, returns the index of the last occurrence (starting with 0); -1 otherwise
Examples¶
<spinque:debug message="{su:lastIndexOf('123456789', '2')}"/>
<spinque:debug message="{su:lastIndexOf('12342567222289', '2')}"/>
<spinque:debug message="{su:lastIndexOf('123456789', ' ')}"/>
generates the debug output
1
11
-1
su:matches¶
Tests whether the input string content matches the provided regular expression regex.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to be tested |
| regex | String | The regular expression pattern used to match against `content` |
Returns¶
true if content matches the regular expression; otherwise, false
Examples¶
<spinque:debug message="{su:matches('abc','ab?c')}"/>
<spinque:debug message="{su:matches('ac','ab?c')}"/>
<spinque:debug message="{su:matches('ac','abc')}"/>
generates the debug output
true
true
false
su:startsWith¶
Checks whether a String content starts with the specified prefix.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string to be searched in. |
| prefix | String | The string to search for at the start of `content`. |
Returns¶
true if content starts with prefix; otherwise, false.
Examples¶
<spinque:debug message="{su:startsWith('abc','a')}"/>
<spinque:debug message="{su:startsWith('bc','a')}"/>
generates the debug output
true
false
Type conversions (string to number)¶
su:normalizeInteger¶
Converts a string content containing an integer to a number. Depending on the locale chosen, removes thousands separators.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string containing an integer |
| locale | String | An abbreviation indicating the locale; used to remove thousands separators. If `NL`: The dot character (`.`) will be regarded as thousands separator. Otherwise: The comma character (`,`) will be regarded as thousands separator. |
Returns¶
A number with the value of content
Examples¶
<spinque:debug message="{su:normalizeInteger('1.234', 'NL')}"/>
<spinque:debug message="{su:normalizeInteger('1234', 'NL')}"/>
<spinque:debug message="{su:normalizeInteger('1,234', 'EN')}"/>
generates the debug output
1234
1234
1234
su:normalizeDouble¶
Converts a string content containing a double (i.e. a number with decimal values) to a number. Considers decimal markers depending on the locale chosen.
Input parameters¶
| parameter | type | description |
|---|---|---|
| content | String | The string containing an integer |
| locale | String | An abbreviation indicating the locale; used to remove decimal markers. If `NL`: The comma character (`,`) will be regarded as decimal marker. Otherwise: The dot character (`.`) will be regarded as thousands separator. |
Returns¶
A number with the value of content
Examples¶
<spinque:debug message="{su:normalizeDouble('1,2345', 'NL')}"/>
<spinque:debug message="{su:normalizeDouble('12345', 'NL')}"/>
<spinque:debug message="{su:normalizeDouble('1.2345', 'EN')}"/>
generates the debug output
1.2345
12345
1.2345
su:periodToMillis¶
Parse a notation of a period into milliseconds.
Input parameters¶
| parameter | type | description |
|---|---|---|
| period | String | The duration expressed in ISO-notation. Important note: Currently only supports components that don't vary in length (for instance, months have different lengths). Supported components are: `W`: weeks; `D`: days; `T`: precedes the time components (hours, minutes, seconds); `H`: hours; `M`: minutes; `s`: seconds |
Returns¶
A number with the number of milliseconds the period represents
Examples¶
<spinque:debug message="{su:periodToMillis('P1D')}"/>
<spinque:debug message="{su:periodToMillis('P1W1DT3H2M3S')}"/>
generates the debug output
84600000
702123000
Dates¶
su:parseDate¶
Parses a date in a provided original format and converts it into a string with the format yyyy-MM-dd.
Input parameters¶
| parameter | type | description |
|---|---|---|
| date | String | The date to be parsed |
| languageTag | String | The language the date expression is provided in (e.g. `en-US`) |
| format | String | Specification of the original format of `date` (e.g., `dd/MM/yyyy`) |
| format2 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format`. |
| format3 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format2`. |
| format4 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format3`. |
| format5 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format4`. |
| format6 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format5`. |
| format7 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format6`. |
| format8 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format7`. |
| format9 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format8`. |
| format10 | String | Optional. Alternative specification of the original format of `date`. Only checked if the date does not match `format9`. |
Returns¶
A string with the reformatted date if date matches the original format (or any alternative specification) and languageTag.
Examples¶
<spinque:debug message="{su:parseDate('01/02/2024', 'en-US','dd/MM/yyyy')}" />
<spinque:debug message="{su:parseDate('01.02.2024', 'en-US','dd.MM.yyyy')}" />
<spinque:debug message="{su:parseDate('20240201', 'nl','yyyyMMdd')}" />
<spinque:debug message="{su:parseDate('Thu, Feb 4, 2024', 'en-US','EEE, MMM d, yyyy')}" />
generates the debug output
2024-02-01
2024-02-01
2024-02-01
2024-02-01