Monthly Archives: November 2012

Getting a substring before the last occurrence of a character in XSLT 1.0

With XSLT 2.0 a method is provided, which splits a string by a specific delimiter (e.g. commas, spaces or other) quite easily.

<xsl:variable name="subString" 
        select="substring-before-last($arg, $delim)"></xsl:variable>

This function returns the part of $arg which appears before the last occurrence of $delim. If $arg does not contain $delim, a zero-length string is returned.

If you split the string “XPath-is-fun” as $arg with $delim “-” you will get the following result:

XPath-is

Unfortunately, this function is not provided in XSLT 1.0. But there is a possibility of realizing this without any 3rd party libraries (e.g. EXSLT). The following example shows, how this can be done with XSLT 1.0.

Readmore…

Tagged , ,