Search code examples
xslt-2.0

Substring in XSLT2.0


I need help with one scenario: I need to substring before the last occurrence of space, hyphen or full stop characters in a text. I tried with substring-before

For example, If text is 'SIR WILLIAM SIEMENS SQUARE', then I need to have 'SIR WILLIAM SIEMENS' as first text string and 'SQUARE'. Using Substring before I am getting 'SIR' and 'WILLIAM SIEMENS SQUARE'.


Solution

  • First you need to replace (. and -) with space and then it will work. Try like this.

    <xsl:template match="/">
        <root>
            <xsl:variable name="maintext" select="replace(replace(/root/a, '\.', '. '), '-', '- ')"/>
            <a><xsl:value-of select="tokenize($maintext, ' ')[position() != last()]"/></a>
            <b><xsl:value-of select="tokenize($maintext, ' ')[last()]"/></b>
        </root>
    </xsl:template>