XSLT - Replacing last occurance of a character in a string

Given a String such as

"01-01-01-01h abc def"

How do i replace the last occurance of '-' with a space so that my resulting String looks like

"01-01-01 01h abc def"

I am using XSLT 2.0

Thanks
MolkoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
Maybe this will do
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:variable name="str" select="'01-01-01-01h abc def'" />
        
    <xsl:template match="/">
        <xsl:value-of select="concat(string-join(tokenize($str, '-')[position() &lt; last()], '-'), ' ', tokenize($str, '-')[position() = last()])"></xsl:value-of>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
MolkoAuthor Commented:
wow...thats fairly complicated

Would it be easier if last '-' was alway followed by 2 numerics and then a character ? ie. -01h

0
Geert BormansInformation ArchitectCommented:
It is not that complex.

I break the string in pieces at the '-'
I bring them all together except the last
I add the space
I add the last from the breakup

pretty convinced that this is the best performing solution
if you don't like the code, stuff it away in a function like this

You could do this with a regular expression if you were certain that -\d\dh would only occur once
see code in next comment
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:pfn="#internal"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:variable name="str" select="'01-01-01-01h abc def'" />
        
    <xsl:template match="/">
        <xsl:value-of select="pfn:replace-last-occurence-of($str, '-', ' ')"/>
    </xsl:template>
    
    <xsl:function name="pfn:replace-last-occurence-of">
        <xsl:param name="str"/>
        <xsl:param name="find"/>
        <xsl:param name="replace"/>
        <xsl:value-of select="concat(string-join(tokenize($str, $find)[position() &lt; last()], $find), $replace, tokenize($str, $find)[position() = last()])"></xsl:value-of>
    </xsl:function>
</xsl:stylesheet>

Open in new window

0
Geert BormansInformation ArchitectCommented:
      <xsl:value-of select="replace($str, '\-(\d\dh)', ' $1')"/>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.