Search code examples
xsltsaxonxslt-3.0

Regex applied to Replace function on XSLT 3 not working


I believe this is a simple problem, I'm trying to apply a Regex to my replace method in a variable in XSLT 3 (I'm also using Saxon (latest version)). I know it is possible to use replace with a regex but it seems that the regex I'm trying to use is wrong, it works in Java but not there on XSLT.

Here is my variable with the replace method:

<xsl:variable name="namePrefix" select="replace(@name, '/(.*_[^_]+)/')" />

I want this variable namePrefix to return me an specific part of the name of my Node (found under the attribute @name), here is an Name Example:

ALBA_MASTER_FIX_Test

I want this regex and replace methode to return to my variable everything before the last _.

I would like to know wheter I'm applying the regex correctly? Or if I should do it in a different way or use a different regex. Thx :)


Solution

  • everything before the last _.

    I believe that could be simply:

    replace(@name, '_[^_]*$', '')
    

    Alternatively, you could use something like:

    <xsl:value-of select="tokenize(@name, '_')[position() lt last()]" separator="_"/>