Introduction
XSLT survival Kit provides useful and handy templates for biginners, intermediate and advanced
XSLT developers.
I started this Kit by setting up the most useful templates on
strings. I'll try to update this Kit as much possible as I can to include useful templates.
If this article is updated, please check the
History section and the
Template List section.
Why this Kit ?
I
wrote this article in order to share the most useful XSLT templates.
And not only sharing code, but also sharing the way I've done these
templates in order to help beginners to learn how to write templates
easily and efficiently.
Before started coding the templates, I
took a pencil and a paper and I wrote the name of all the templates that
would be interesting to implement. Then, I wrote the logic of each
template on the paper then I implemented it in an XSLT stylesheet and
finally tested each one.
I advise every XSLT developer to adopt
this methodology for writing XSLT templates. If you practice this method
on very complex templates you will notice that you will spend less time
in coding.
History
Sunday, January, 29, 2012
Creating the article and adding templates on
strings.
Wednesday, February, 01, 2012
Adding the templates on file paths.
Template List
The table below contains the list of the templates in the Kit.
| Template name | Description |
startsWith | If the input string starts with the specified string, the template returns true, else It returns false. |
endsWith | If the input string ends with the specified string, the template returns true, else It returns false. |
indexOf | Returns the index of the first occurrence of the specified string in the input string, If the input string contains the specified string. Else It returns -1. |
duplicate | Duplicates a string N times depending on the value of N. |
reverse | Returns the reverse string of the input string. |
toUpper | Returns the uppercase string of the specified string. |
toLower | Returns the lowercase string of the specified string. |
replace | Replaces a string in the input string. |
getFileName | Returns the filename from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...). |
getFileExtension | Returns the file extension from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...) with the ability to include or exclude the dot. |
getFileNameWithoutExtension | Returns the filename without extension from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...). |
Prerequisite
In
this section, I describe all the native XSLT/XPATH 1.0 functions that I
used in the templates. I will update this section If I add new
templates that use other XSLT/XPATH 1.0 fucntions.
string substring (string, number, number?)
The substring function returns the substring of the first argument
starting at the position specified in the second argument
with length specified in the third argument.
For example,
substring('12345',2,3) returns '234'. If the third argument is not
specified, it returns the substring starting at the position
specified in the second argument and continuing to the end of
the string. For example, substring('12345',2) returns '2345'.
Note. In XSLT the start index is always 1 whereas in other languages like C++ or C# It is 0.
string substring-before (string, string)
The substring-before function returns the substring of the first argument string that precedes the first occurrence of the
second argument string in the first argument string, or the empty string if the first argument string does not contain the
second argument string.
For example, substring-before('1999/04/01','/') returns '1999'.
string substring-after (string, string)
The substring-after function returns the substring of the first argument
string that follows the first occurrence of the second
argument string in the first argument string, or the empty string
if the first argument string does not contain the second
argument string.
For example, substring-after('1999/04/01','/')
returns '04/01', and substring-after('1999/04/01','19') returns
'99/04/01'.
number string-length (string?)
The string-length function returns the number of characters in the string. If the argument is omitted, it defaults to the context node
converted to a string, in other words the string-value of the context node.
For example, string-lenght('Hello World') returns 11.
boolean contains (string, string)
The contains function returns true if the first argument string contains the second argument string, and otherwise returns
false.
For example, contains('Hello World', 'o') returns true, and contains('Hello World', 'z') returns false.
string concat (string, string, string*)
The concat function returns the concatenation of its arguments.
For example, concat('Hello', ' ', 'World') returns 'Hello World' and concat('Hello ', 'World') returns 'Hello World'.
string translate (string, string, string)
The translate function returns the first argument string with
occurrences of characters in the second argument string replaced
by the character at the corresponding position in the third
argument string.
For example, translate('bar','abc','ABC') returns 'BAr'.
If there is a character in the second argument
string with no character at a corresponding position in the
third argument string (because the second argument string is
longer than the third argument string), then occurrences of
that character in the first argument string are removed.
For
example, translate('--aaa--','abc-','ABC') returns 'AAA'. If
a character occurs more than once in the second argument string,
then the first occurrence determines the replacement character.
If the third argument string is longer than the second argument
string, then excess characters are ignored.
Templates
executeUnitTest
In order to test my templates, I decided to execute unit tests on each template. Thus, I created a unit test template.
<!---->
<xsl:template name="executeUnitTest">
<xsl:param name="object1"/>
<xsl:param name="object2"/>
<xsl:choose>
<xsl:when test="$object1 = $object2">passed</xsl:when>
<xsl:otherwise>failed</xsl:otherwise>
</xsl:choose>
</xsl:template>
startsWith
General description
If the input string starts with the specified string, the template returns true, else It returns false.
Template code
<!---->
<xsl:template name="startsWith">
<xsl:param name="input"/>
<xsl:param name="string"/>
<xsl:value-of select="substring($input, 1, string-length($string)) = $string"/>
</xsl:template>
Code description
In order to test that the input string starts with the specified string, I take the substring of the input string starting from the first charachter (position 1) of the input string and taking the length of the specified string. Then, I test If this substring is equal to the specified string.
Unit test code
<xsl:comment>#########################</xsl:comment>
<xsl:comment># startsWith unit tests #</xsl:comment>
<xsl:comment>#########################</xsl:comment>
<xsl:comment>startsWith('Hello World', 'Hello') = true</xsl:comment>
<unitTest id="1">
<xsl:variable name="unitTest1">
<xsl:call-template name="startsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'Hello'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest1"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>startsWith('Hello World', 'Hello ') = true</xsl:comment>
<unitTest id="2">
<xsl:variable name="unitTest2">
<xsl:call-template name="startsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'Hello '"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest2"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>startsWith('Hello World', 'World') = false</xsl:comment>
<unitTest id="3">
<xsl:variable name="unitTest3">
<xsl:call-template name="startsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'World'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest3"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="1">passed</unitTest>
<!---->
<unitTest id="2">passed</unitTest>
<!---->
<unitTest id="3">passed</unitTest>
endsWith
General description
If the input string ends with the specified string, the template returns true, else It returns false.
Template code
<!---->
<xsl:template name="endsWith">
<xsl:param name="input"/>
<xsl:param name="string"/>
<xsl:value-of select="substring($input, (string-length($input) - string-length($string)) + 1) = $string"/>
</xsl:template>
Code description
In order to test that the input string ends with the specified string, I take the substring of the input string starting from the position of the first charachter of the specified string in the input string:
(string-length($input) - string-length($string)) + 1)
Then, I test If this substring is equal to the specified string.
Unit test code
<xsl:comment>#######################</xsl:comment>
<xsl:comment># endsWith unit tests #</xsl:comment>
<xsl:comment>#######################</xsl:comment>
<xsl:comment>endsWith('Hello World', 'World') = true</xsl:comment>
<unitTest id="4">
<xsl:variable name="unitTest4">
<xsl:call-template name="endsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'World'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest4"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>endsWith('Hello World', 'orld') = true</xsl:comment>
<unitTest id="5">
<xsl:variable name="unitTest5">
<xsl:call-template name="endsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'orld'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest5"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>endsWith('Hello World', 'orl') = false</xsl:comment>
<unitTest id="6">
<xsl:variable name="unitTest6">
<xsl:call-template name="endsWith">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'orl'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest6"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="4">passed</unitTest>
<!---->
<unitTest id="5">passed</unitTest>
<!---->
<unitTest id="6">passed</unitTest>
indexOf
General description
Returns the index of the first occurrence of the specified string in the input string, If the input string contains the specified string. Else It returns -1.
Template code
<!---->
<xsl:template name="indexOf">
<xsl:param name="input"/>
<xsl:param name="string"/>
<xsl:choose>
<xsl:when test="contains($input, $string)">
<xsl:value-of select="string-length(substring-before($input, $string)) + 1"/>
</xsl:when>
<xsl:otherwise>-1</xsl:otherwise>
</xsl:choose>
</xsl:template>
Code description
If the input string does not contain the specified string, The template returs -1.
Otherwise, the template calculates the length of the substring before the first occurence of the specified string. Then the template adds one to this length in order to return the position of the first character of the first occurence of the specified string in the input string.
Unit test code
<xsl:comment>######################</xsl:comment>
<xsl:comment># indexOf unit tests #</xsl:comment>
<xsl:comment>######################</xsl:comment>
<xsl:comment>indexOf('Hello World', 'Hello') = 1</xsl:comment>
<unitTest id="7">
<xsl:variable name="unitTest7">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'Hello'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest7"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'Hello ') = 1</xsl:comment>
<unitTest id="8">
<xsl:variable name="unitTest8">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'Hello '"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest8"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'World') = 7</xsl:comment>
<unitTest id="9">
<xsl:variable name="unitTest9">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'World'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest9"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'l') = 3</xsl:comment>
<unitTest id="10">
<xsl:variable name="unitTest10">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'l'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest10"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'o') = 5</xsl:comment>
<unitTest id="11">
<xsl:variable name="unitTest11">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'o'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest11"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'd') = 11</xsl:comment>
<unitTest id="12">
<xsl:variable name="unitTest12">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'d'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest12"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'a') = -1</xsl:comment>
<unitTest id="13">
<xsl:variable name="unitTest13">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'a'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest13"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
<xsl:comment>indexOf('Hello World', 'ab') = -1</xsl:comment>
<unitTest id="14">
<xsl:variable name="unitTest14">
<xsl:call-template name="indexOf">
<xsl:with-param name="input" select="'Hello World'"/>
<xsl:with-param name="string" select="'ab'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest14"/>
<xsl:with-param name="object2" select="true()"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="7">passed</unitTest>
<!---->
<unitTest id="8">passed</unitTest>
<!---->
<unitTest id="9">passed</unitTest>
<!---->
<unitTest id="10">passed</unitTest>
<!---->
<unitTest id="11">passed</unitTest>
<!---->
<unitTest id="12">passed</unitTest>
<!---->
<unitTest id="13">passed</unitTest>
<!---->
<unitTest id="14">passed</unitTest>
duplicate
General description
Duplicates a string N times depending on the value of N.
Template code
<!---->
<xsl:template name="duplicate">
<xsl:param name="string"/>
<xsl:param name="n"/>
<xsl:if test="$n > 0">
<xsl:value-of select="$string"/>
<xsl:call-template name="duplicate">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="n" select="$n -1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Code description
This is a
recursive template. If
N is lower than or equal to
0, the template returns the empty
string.
Otherwise, the template concatenates recursively the
string and decrements
N until
N becomes equal to
0.
Unit test code
<xsl:comment>########################</xsl:comment>
<xsl:comment># duplicate unit tests #</xsl:comment>
<xsl:comment>########################</xsl:comment>
<xsl:comment>duplicate('Hello ', 0) = ''</xsl:comment>
<unitTest id="15">
<xsl:variable name="unitTest15">
<xsl:call-template name="duplicate">
<xsl:with-param name="string" select="'Hello '"/>
<xsl:with-param name="n" select="0"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest15"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>duplicate('Hello ', 1) = 'Hello '</xsl:comment>
<unitTest id="16">
<xsl:variable name="unitTest16">
<xsl:call-template name="duplicate">
<xsl:with-param name="string" select="'Hello '"/>
<xsl:with-param name="n" select="1"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest16"/>
<xsl:with-param name="object2" select="'Hello '"/>
</xsl:call-template>
</unitTest>
<xsl:comment>duplicate('Hello ', 2) = 'Hello Hello '</xsl:comment>
<unitTest id="17">
<xsl:variable name="unitTest17">
<xsl:call-template name="duplicate">
<xsl:with-param name="string" select="'Hello '"/>
<xsl:with-param name="n" select="2"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest17"/>
<xsl:with-param name="object2" select="'Hello Hello '"/>
</xsl:call-template>
</unitTest>
<xsl:comment>duplicate('Hello ', 3) = 'Hello Hello Hello '</xsl:comment>
<unitTest id="18">
<xsl:variable name="unitTest18">
<xsl:call-template name="duplicate">
<xsl:with-param name="string" select="'Hello '"/>
<xsl:with-param name="n" select="3"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest18"/>
<xsl:with-param name="object2" select="'Hello Hello Hello '"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="15">passed</unitTest>
<!---->
<unitTest id="16">passed</unitTest>
<!---->
<unitTest id="17">passed</unitTest>
<!---->
<unitTest id="18">passed</unitTest>
reverse
General description
Returns the reverse string of the input string.
Template code
<!---->
<xsl:template name="reverse">
<xsl:param name="string"/>
<xsl:if test="string-length($string) > 0">
<xsl:value-of select="substring($string, string-length($string))"/>
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="substring($string, 1, string-length($string) - 1)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Code description
This is a recursive template. In order to build the reverse string. I return the last character of the string. Then, I call the template recursively on the string without the last character If the string length is upper than 0.
Unit test code
<xsl:comment>######################</xsl:comment>
<xsl:comment># reverse unit tests #</xsl:comment>
<xsl:comment>######################</xsl:comment>
<xsl:comment>reverse('') = ''</xsl:comment>
<unitTest id="19">
<xsl:variable name="unitTest19">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest19"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>reverse('Hello World') = 'dlroW olleH'</xsl:comment>
<unitTest id="20">
<xsl:variable name="unitTest20">
<xsl:call-template name="reverse">
<xsl:with-param name="string" select="'Hello World'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest20"/>
<xsl:with-param name="object2" select="'dlroW olleH'"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="19">passed</unitTest>
<!---->
<unitTest id="20">passed</unitTest>
toUpper
General description
Returns the uppercase string of the specified string.
Template code
<!---->
<xsl:template name="toUpper">
<xsl:param name="string" />
<!---->
<xsl:variable name="lowercase_chars" select="'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüý'"/>
<xsl:variable name="uppercase_chars" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ'"/>
<xsl:value-of select="translate($string, $lowercase_chars, $uppercase_chars)"/>
</xsl:template>
Code description
This template replace each ISO 8859-1 lowercase character by Its uppercase character by using the translate function.
Unit test code
<xsl:comment>######################</xsl:comment>
<xsl:comment># toUpper unit tests #</xsl:comment>
<xsl:comment>######################</xsl:comment>
<xsl:comment>toUpper('Hello World') = 'HELLO WORLD'</xsl:comment>
<unitTest id="21">
<xsl:variable name="unitTest21">
<xsl:call-template name="toUpper">
<xsl:with-param name="string" select="'Hello World'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest21"/>
<xsl:with-param name="object2" select="'HELLO WORLD'"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="21">passed</unitTest>
toLower
General description
Returns the lowercase string of the specified string.
Template code
<!---->
<xsl:template name="toLower">
<xsl:param name="string" />
<!---->
<xsl:variable name="lowercase_chars" select="'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüý'"/>
<xsl:variable name="uppercase_chars" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ'"/>
<xsl:value-of select="translate($string, $uppercase_chars, $lowercase_chars)"/>
</xsl:template>
Code description
This template replace each ISO 8859-1 uppercase character by Its lowercase character by using the translate function.
Unit test code
<xsl:comment>######################</xsl:comment>
<xsl:comment># toLower unit tests #</xsl:comment>
<xsl:comment>######################</xsl:comment>
<xsl:comment>toLower('HELLO WORLD') = 'hello world'</xsl:comment>
<unitTest id="22">
<xsl:variable name="unitTest22">
<xsl:call-template name="toLower">
<xsl:with-param name="string" select="'HELLO WORLD'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest22"/>
<xsl:with-param name="object2" select="'hello world'"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="22">passed</unitTest>
replace
General description
Replaces a string in the input string.
Template code
<!---->
<xsl:template name="replace">
<xsl:param name="string" />
<xsl:param name="toReplace" />
<xsl:param name="replacement" />
<xsl:if test="contains($string, $toReplace)">
<xsl:value-of select="concat(substring-before($string, $toReplace), $replacement)"/>
<xsl:call-template name="replace">
<xsl:with-param name="string" select="substring-after($string, $toReplace)"/>
<xsl:with-param name="toReplace" select="$toReplace"/>
<xsl:with-param name="replacement" select="$replacement"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Code description
This is a recursive template. If the string contains the string to replace, I return the concatenation of the substring before the string to replace and the replacement string. Then, I call the template recursively in order to replace all the occurences of the string to replace by the replacement string in the string.
Unit test code
<xsl:comment>######################</xsl:comment>
<xsl:comment># replace unit tests #</xsl:comment>
<xsl:comment>######################</xsl:comment>
<xsl:comment>replace('Hello World', 'World', 'Folks') = 'Hello Folks'</xsl:comment>
<unitTest id="23">
<xsl:variable name="unitTest23">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="'Hello World'"/>
<xsl:with-param name="toReplace" select="'World'"/>
<xsl:with-param name="replacement" select="'Folks'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest23"/>
<xsl:with-param name="object2" select="'Hello Folks'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>replace('Hello World Hello World', 'World', 'Folks') = 'Hello Folks Hello Folks'</xsl:comment>
<unitTest id="24">
<xsl:variable name="unitTest24">
<xsl:call-template name="replace">
<xsl:with-param name="string" select="'Hello World Hello World'"/>
<xsl:with-param name="toReplace" select="'World'"/>
<xsl:with-param name="replacement" select="'Folks'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest24"/>
<xsl:with-param name="object2" select="'Hello Folks Hello Folks'"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="23">passed</unitTest>
<!---->
<unitTest id="24">passed</unitTest>
getFileName
General description
Returns the filename from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...).
Template code
<!---->
<xsl:template name="getFileName">
<xsl:param name="path" />
<xsl:choose>
<xsl:when test="contains($path, '\')">
<xsl:call-template name="customSubstring">
<xsl:with-param name="string" select="$path"/>
<xsl:with-param name="delimiter" select="'\'"/>
<xsl:with-param name="includeDelimiter" select="false()" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($path, '/')">
<xsl:call-template name="customSubstring">
<xsl:with-param name="string" select="$path"/>
<xsl:with-param name="delimiter" select="'/'"/>
<xsl:with-param name="includeDelimiter" select="false()" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$path"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Code description
If the file path contains a back slash, I retrieve the filename by calling the customSubstring template on the file path without including the back slash delimiter.
Else If the file path contains a slash, I retrieve the filename by calling the customSubstring template on the file path without including the slash delimiter.
Otherwise, I return the file path.
Below the customSubstring template.
<!---->
<xsl:template name="customSubstring">
<xsl:param name="string"/>
<xsl:param name="delimiter"/>
<xsl:param name="includeDelimiter"/>
<xsl:choose>
<xsl:when test="contains($string, $delimiter)">
<xsl:call-template name="customSubstringRecursive">
<xsl:with-param name="string" select="$string"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
<xsl:with-param name="includeDelimiter" select="$includeDelimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The customSubstring template simply returns the string after the last occurence of the delimiter in the string If the input string contains the delimiter. Otherwise, It returns the input string. the includeDelimiter parameter indicates whether the delimiter should be included in the result string or not. The customSubstring template calls a recursive template that performs the main substring.
Below, the customSubstringRecursive template.
<!---->
<xsl:template name="customSubstringRecursive">
<xsl:param name="string"/>
<xsl:param name="delimiter"/>
<xsl:param name="includeDelimiter"/>
<xsl:if test="contains($string, $delimiter)">
<xsl:variable name="lastCharacter" select="substring($string, string-length($string), 1)" />
<xsl:if test="$lastCharacter != $delimiter">
<xsl:call-template name="customSubstringRecursive">
<xsl:with-param name="string" select="substring($string, 1, string-length($string) - 1)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
<xsl:with-param name="includeDelimiter" select="$includeDelimiter" />
</xsl:call-template>
<xsl:if test="boolean($includeDelimiter) = false()">
<!---->
<xsl:value-of select="$lastCharacter" />
</xsl:if>
</xsl:if>
<xsl:if test="boolean($includeDelimiter) = true()">
<!---->
<xsl:value-of select="$lastCharacter" />
</xsl:if>
</xsl:if>
</xsl:template>
The customSubstringRecursive template builds recusively the string after the last occurence of the delimiter in the string If the input string contains the delimiter. Otherwise, It returns the empty string. the includeDelimiter parameter indicates whether the delimiter should be included in the result string or not.
Unit test code
<xsl:comment>##########################</xsl:comment>
<xsl:comment># getFileName unit tests #</xsl:comment>
<xsl:comment>##########################</xsl:comment>
<xsl:comment>getFileName('C:\file.xml') = 'file.xml'</xsl:comment>
<unitTest id="25">
<xsl:variable name="unitTest25">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'C:\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest25"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('C:\file.html.pdf') = 'file.html.pdf'</xsl:comment>
<unitTest id="26">
<xsl:variable name="unitTest26">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'C:\file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest26"/>
<xsl:with-param name="object2" select="'file.html.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('file.xml') = 'file.xml'</xsl:comment>
<unitTest id="27">
<xsl:variable name="unitTest27">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest27"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('file.html.pdf') = 'file.html.pdf'</xsl:comment>
<unitTest id="28">
<xsl:variable name="unitTest28">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest28"/>
<xsl:with-param name="object2" select="'file.html.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('\\file-server\file.xml') = 'file.xml'</xsl:comment>
<unitTest id="29">
<xsl:variable name="unitTest29">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'\\file-server\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest29"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('\\file-server\file.html.pdf') = 'file.html.pdf'</xsl:comment>
<unitTest id="30">
<xsl:variable name="unitTest30">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'\\file-server\file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest30"/>
<xsl:with-param name="object2" select="'file.html.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('ftp://ftp-server/file.xml') = 'file.xml'</xsl:comment>
<unitTest id="31">
<xsl:variable name="unitTest31">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'ftp://ftp-server/file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest31"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('ftp://ftp-server/file.html.pdf') = 'file.html.pdf'</xsl:comment>
<unitTest id="32">
<xsl:variable name="unitTest32">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'ftp://ftp-server/file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest32"/>
<xsl:with-param name="object2" select="'file.html.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('C:\file') = 'file'</xsl:comment>
<unitTest id="33">
<xsl:variable name="unitTest33">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'C:\file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest33"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('file') = 'file'</xsl:comment>
<unitTest id="34">
<xsl:variable name="unitTest34">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest34"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('\\file-server\file') = 'file'</xsl:comment>
<unitTest id="35">
<xsl:variable name="unitTest35">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'\\file-server\file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest35"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('ftp://ftp-server/file') = 'file'</xsl:comment>
<unitTest id="36">
<xsl:variable name="unitTest36">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'ftp://ftp-server/file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest36"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('..\..\file.xml') = 'file.xml'</xsl:comment>
<unitTest id="37">
<xsl:variable name="unitTest37">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'..\..\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest37"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('~/file.xml') = 'file.xml'</xsl:comment>
<unitTest id="38">
<xsl:variable name="unitTest38">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'~/file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest38"/>
<xsl:with-param name="object2" select="'file.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('C:\') = ''</xsl:comment>
<unitTest id="39">
<xsl:variable name="unitTest39">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'C:\'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest39"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('') = ''</xsl:comment>
<unitTest id="40">
<xsl:variable name="unitTest40">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest40"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('\\file-server\') = ''</xsl:comment>
<unitTest id="41">
<xsl:variable name="unitTest41">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'\\file-server\'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest41"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileName('ftp://ftp-server/') = ''</xsl:comment>
<unitTest id="42">
<xsl:variable name="unitTest42">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="'ftp://ftp-server/'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest42"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="25">passed</unitTest>
<!---->
<unitTest id="26">passed</unitTest>
<!---->
<unitTest id="27">passed</unitTest>
<!---->
<unitTest id="28">passed</unitTest>
<!---->
<unitTest id="29">passed</unitTest>
<!---->
<unitTest id="30">passed</unitTest>
<!---->
<unitTest id="31">passed</unitTest>
<!---->
<unitTest id="32">passed</unitTest>
<!---->
<unitTest id="33">passed</unitTest>
<!---->
<unitTest id="34">passed</unitTest>
<!---->
<unitTest id="35">passed</unitTest>
<!---->
<unitTest id="36">passed</unitTest>
<!---->
<unitTest id="37">passed</unitTest>
<!---->
<unitTest id="38">passed</unitTest>
<!---->
<unitTest id="39">passed</unitTest>
<!---->
<unitTest id="40">passed</unitTest>
<!---->
<unitTest id="41">passed</unitTest>
<!---->
<unitTest id="42">passed</unitTest>
getFileExtension
General description
Returns the file extension from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...) with the ability to include or exclude the dot.
Template code
<!---->
<xsl:template name="getFileExtension">
<xsl:param name="path" />
<xsl:param name="withDot" />
<!---->
<xsl:variable name="fileName">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="$path" />
</xsl:call-template>
</xsl:variable>
<!---->
<xsl:call-template name="customSubstringRecursive">
<xsl:with-param name="string" select="$fileName"/>
<xsl:with-param name="delimiter" select="'.'"/>
<xsl:with-param name="includeDelimiter" select="$withDot"/>
</xsl:call-template>
</xsl:template>
Code description
First of all, The filename is retrieved from the file path. Then the customSubstringRecursive is called in order to retrieve the extension with the abilty to include the dot or not.
Unit test code
<xsl:comment>###############################</xsl:comment>
<xsl:comment># getFileExtension unit tests #</xsl:comment>
<xsl:comment>###############################</xsl:comment>
<xsl:comment>getFileExtension('C:\file.xml', true()) = '.xml'</xsl:comment>
<unitTest id="43">
<xsl:variable name="unitTest43">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file.xml'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest43"/>
<xsl:with-param name="object2" select="'.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('C:\file.html.pdf', true()) = '.pdf'</xsl:comment>
<unitTest id="44">
<xsl:variable name="unitTest44">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file.html.pdf'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest44"/>
<xsl:with-param name="object2" select="'.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('file.xml', true()) = '.xml'</xsl:comment>
<unitTest id="45">
<xsl:variable name="unitTest45">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'file.xml'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest45"/>
<xsl:with-param name="object2" select="'.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('file.html.pdf', true()) = '.pdf'</xsl:comment>
<unitTest id="46">
<xsl:variable name="unitTest46">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'file.html.pdf'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest46"/>
<xsl:with-param name="object2" select="'.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file.xml', true()) = '.xml'</xsl:comment>
<unitTest id="47">
<xsl:variable name="unitTest47">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file.xml'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest47"/>
<xsl:with-param name="object2" select="'.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file.html.pdf', true()) = '.pdf'</xsl:comment>
<unitTest id="48">
<xsl:variable name="unitTest48">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file.html.pdf'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest48"/>
<xsl:with-param name="object2" select="'.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file.xml', true()) = '.xml'</xsl:comment>
<unitTest id="49">
<xsl:variable name="unitTest49">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.xml'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest49"/>
<xsl:with-param name="object2" select="'.xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file.html.pdf', true()) = '.pdf'</xsl:comment>
<unitTest id="50">
<xsl:variable name="unitTest50">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.html.pdf'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest50"/>
<xsl:with-param name="object2" select="'.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('C:\file.xml', false()) = 'xml'</xsl:comment>
<unitTest id="51">
<xsl:variable name="unitTest51">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file.xml'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest51"/>
<xsl:with-param name="object2" select="'xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('C:\file.html.pdf', false()) = 'pdf'</xsl:comment>
<unitTest id="52">
<xsl:variable name="unitTest52">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file.html.pdf'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest52"/>
<xsl:with-param name="object2" select="'pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('file.xml', false()) = 'xml'</xsl:comment>
<unitTest id="53">
<xsl:variable name="unitTest53">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'file.xml'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest53"/>
<xsl:with-param name="object2" select="'xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('file.html.pdf', false()) = 'pdf'</xsl:comment>
<unitTest id="54">
<xsl:variable name="unitTest54">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'file.html.pdf'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest54"/>
<xsl:with-param name="object2" select="'pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file.xml', false()) = 'xml'</xsl:comment>
<unitTest id="55">
<xsl:variable name="unitTest55">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file.xml'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest55"/>
<xsl:with-param name="object2" select="'xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file.html.pdf', false()) = 'pdf'</xsl:comment>
<unitTest id="56">
<xsl:variable name="unitTest56">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file.html.pdf'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest56"/>
<xsl:with-param name="object2" select="'pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file.xml', false()) = 'xml'</xsl:comment>
<unitTest id="57">
<xsl:variable name="unitTest57">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.xml'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest57"/>
<xsl:with-param name="object2" select="'xml'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file.html.pdf', false()) = 'pdf'</xsl:comment>
<unitTest id="58">
<xsl:variable name="unitTest58">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.html.pdf'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest58"/>
<xsl:with-param name="object2" select="'pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('C:\file\', true()) = ''</xsl:comment>
<unitTest id="59">
<xsl:variable name="unitTest59">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file\'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest59"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('~/file.html.pdf', true()) = '.pdf'</xsl:comment>
<unitTest id="60">
<xsl:variable name="unitTest60">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'~/file.html.pdf'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest60"/>
<xsl:with-param name="object2" select="'.pdf'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\file\', true()) = ''</xsl:comment>
<unitTest id="61">
<xsl:variable name="unitTest61">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\file\'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest61"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file\', true()) = ''</xsl:comment>
<unitTest id="62">
<xsl:variable name="unitTest62">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file\'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest62"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file/', true()) = ''</xsl:comment>
<unitTest id="63">
<xsl:variable name="unitTest63">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file/'"/>
<xsl:with-param name="withDot" select="true()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest63"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('C:\file\', false()) = ''</xsl:comment>
<unitTest id="64">
<xsl:variable name="unitTest64">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'C:\file\'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest64"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\file\', false()) = 'xml'</xsl:comment>
<unitTest id="65">
<xsl:variable name="unitTest65">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\file\'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest65"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('\\file-server\file\', false()) = ''</xsl:comment>
<unitTest id="66">
<xsl:variable name="unitTest66">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'\\file-server\file\'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest66"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('ftp://ftp-server/file/', false()) = ''</xsl:comment>
<unitTest id="67">
<xsl:variable name="unitTest67">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file/'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest67"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileExtension('~/file.html.pdf', false()) = 'pdf'</xsl:comment>
<unitTest id="68">
<xsl:variable name="unitTest68">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="'~/file.html.pdf'"/>
<xsl:with-param name="withDot" select="false()"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest68"/>
<xsl:with-param name="object2" select="'pdf'"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="43">passed</unitTest>
<!---->
<unitTest id="44">passed</unitTest>
<!---->
<unitTest id="45">passed</unitTest>
<!---->
<unitTest id="46">passed</unitTest>
<!---->
<unitTest id="47">passed</unitTest>
<!---->
<unitTest id="48">passed</unitTest>
<!---->
<unitTest id="49">passed</unitTest>
<!---->
<unitTest id="50">passed</unitTest>
<!---->
<unitTest id="51">passed</unitTest>
<!---->
<unitTest id="52">passed</unitTest>
<!---->
<unitTest id="53">passed</unitTest>
<!---->
<unitTest id="54">passed</unitTest>
<!---->
<unitTest id="55">passed</unitTest>
<!---->
<unitTest id="56">passed</unitTest>
<!---->
<unitTest id="57">passed</unitTest>
<!---->
<unitTest id="58">passed</unitTest>
<!---->
<unitTest id="59">passed</unitTest>
<!---->
<unitTest id="60">passed</unitTest>
<!---->
<unitTest id="61">passed</unitTest>
<!---->
<unitTest id="62">passed</unitTest>
<!---->
<unitTest id="63">passed</unitTest>
<!---->
<unitTest id="64">passed</unitTest>
<!---->
<unitTest id="65">passed</unitTest>
<!---->
<unitTest id="66">passed</unitTest>
<!---->
<unitTest id="67">passed</unitTest>
<!---->
<unitTest id="68">passed</unitTest>
getFileNameWithoutExtension
General description
Returns the filename without extension from the specified file path (Local, relative, Samba, FTP, SFTP, FTPS ...).
Template code
<!---->
<xsl:template name="getFileNameWithoutExtension">
<xsl:param name="path" />
<!---->
<xsl:variable name="fileName">
<xsl:call-template name="getFileName">
<xsl:with-param name="path" select="$path" />
</xsl:call-template>
</xsl:variable>
<!---->
<xsl:variable name="extension">
<xsl:call-template name="getFileExtension">
<xsl:with-param name="path" select="$path" />
<xsl:with-param name="withDot" select="true()" />
</xsl:call-template>
</xsl:variable>
<!---->
<xsl:value-of select="substring($fileName, 1, string-length($fileName) - string-length($extension))"/>
</xsl:template>
Code description
First of all, The filename and the file extension are retrieved from the file path. Then the substring without extension is returned.
Unit test code
<xsl:comment>##########################################</xsl:comment>
<xsl:comment># getFileNameWithoutExtension unit tests #</xsl:comment>
<xsl:comment>##########################################</xsl:comment>
<xsl:comment>getFileNameWithoutExtension('C:\file.xml') = 'file'</xsl:comment>
<unitTest id="69">
<xsl:variable name="unitTest69">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'C:\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest69"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('C:\file.html.pdf') = 'file.html'</xsl:comment>
<unitTest id="70">
<xsl:variable name="unitTest70">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'C:\file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest70"/>
<xsl:with-param name="object2" select="'file.html'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('file.xml') = 'file'</xsl:comment>
<unitTest id="71">
<xsl:variable name="unitTest71">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest71"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('file.html.pdf') = 'file.html'</xsl:comment>
<unitTest id="72">
<xsl:variable name="unitTest72">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest72"/>
<xsl:with-param name="object2" select="'file.html'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('\\file-server\file.xml') = 'file'</xsl:comment>
<unitTest id="73">
<xsl:variable name="unitTest73">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'\\file-server\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest73"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('\\file-server\file.html.pdf') = 'file.html'</xsl:comment>
<unitTest id="74">
<xsl:variable name="unitTest74">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'\\file-server\file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest74"/>
<xsl:with-param name="object2" select="'file.html'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('ftp://ftp-server/file.xml') = 'file'</xsl:comment>
<unitTest id="75">
<xsl:variable name="unitTest75">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest75"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('ftp://ftp-server/file.html.pdf') = 'file.html'</xsl:comment>
<unitTest id="76">
<xsl:variable name="unitTest76">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file.html.pdf'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest76"/>
<xsl:with-param name="object2" select="'file.html'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('C:\file') = 'file'</xsl:comment>
<unitTest id="77">
<xsl:variable name="unitTest77">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'C:\file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest77"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('file') = 'file'</xsl:comment>
<unitTest id="78">
<xsl:variable name="unitTest78">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest78"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('\\file-server\file') = 'file'</xsl:comment>
<unitTest id="79">
<xsl:variable name="unitTest79">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'\\file-server\file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest79"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('ftp://ftp-server/file') = 'file'</xsl:comment>
<unitTest id="80">
<xsl:variable name="unitTest80">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/file'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest80"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('..\..\file.xml') = 'file'</xsl:comment>
<unitTest id="81">
<xsl:variable name="unitTest81">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'..\..\file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest81"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('~/file.xml') = 'file'</xsl:comment>
<unitTest id="82">
<xsl:variable name="unitTest82">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'~/file.xml'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest82"/>
<xsl:with-param name="object2" select="'file'"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('C:\') = ''</xsl:comment>
<unitTest id="83">
<xsl:variable name="unitTest83">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'C:\'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest83"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('') = ''</xsl:comment>
<unitTest id="84">
<xsl:variable name="unitTest84">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="''"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest84"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('\\file-server\') = ''</xsl:comment>
<unitTest id="85">
<xsl:variable name="unitTest85">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'\\file-server\'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest85"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
<xsl:comment>getFileNameWithoutExtension('ftp://ftp-server/') = ''</xsl:comment>
<unitTest id="86">
<xsl:variable name="unitTest86">
<xsl:call-template name="getFileNameWithoutExtension">
<xsl:with-param name="path" select="'ftp://ftp-server/'"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="executeUnitTest">
<xsl:with-param name="object1" select="$unitTest86"/>
<xsl:with-param name="object2" select="''"/>
</xsl:call-template>
</unitTest>
Unit test results
<!---->
<!---->
<!---->
<!---->
<unitTest id="69">passed</unitTest>
<!---->
<unitTest id="70">passed</unitTest>
<!---->
<unitTest id="71">passed</unitTest>
<!---->
<unitTest id="72">passed</unitTest>
<!---->
<unitTest id="73">passed</unitTest>
<!---->
<unitTest id="74">passed</unitTest>
<!---->
<unitTest id="75">passed</unitTest>
<!---->
<unitTest id="76">passed</unitTest>
<!---->
<unitTest id="77">passed</unitTest>
<!---->
<unitTest id="78">passed</unitTest>
<!---->
<unitTest id="79">passed</unitTest>
<!---->
<unitTest id="80">passed</unitTest>
<!---->
<unitTest id="81">passed</unitTest>
<!---->
<unitTest id="82">passed</unitTest>
<!---->
<unitTest id="83">passed</unitTest>
<!---->
<unitTest id="84">passed</unitTest>
<!---->
<unitTest id="85">passed</unitTest>
<!---->
<unitTest id="86">passed</unitTest>
References
Some parts of the definitions in the prerequisite section are taken from W3C Recommendation 05 December 2006.