I have a string in an XML file that looks similar to this:
M:Namespace.Class.Method(Something a, Something b)
The number of period (.) characters is abritrary, meaning it can be only 2 as in this example, but can be more.
I would like to use XSLT to get a substring of this string from the last '.' character, so that i will only be left with:
Method(Something a, Something b)
I could not achieve this using the standard substring/substring-after functions.
Is there an easy way to do this?
If you do know that you have exactly two dots in your strings then you can try:
Here is a more efficient solution O(N) vs. O(N^2) for the accepted answer:
When this transformation is applied on the following XML document:
the wanted, correct result is produced:
Explanation:
This solution doesn't contain any call to the
substring-after()
function. Instead, at each step only the one character of the string is compared for equality with the dot character. Because there are at most N characters, this is O(N) -- linear complexity.On the contrary, the accepted answer calls the
substring-after()
function on every step. In the worst case there could be N dots and thus this would be O(N^N) -- quadratic complexity.Note: We make the reasonable assumption that in both solutions locating the k-th character of a string is O(1).
In XSLT 1.0 you will need to use a recursive template, like this:
and invoke it like this:
In XSLT 2.0, you can use the tokenize() function and simply select the last item in the sequence: