Replace String in Xslt 2.0 with Replace Function

Replace String in Xslt 2.0 with Replace Function

I have a string like this

"My string"

Now I want to replace my with best so that the output will be like best string. I have tried some thing like this

 <xsl:value-of select="replace( 'my string',my,best)"/>

but probably its a wrong syntax

I have googled a lot but found nothing..every where the mechanism to do this XSLT 1.0 is explained.Can any one tell me how to do it in XSLT 2.0 ,The easy way compared to 1.0

0

2 Answers

Given:

<xsl:variable name="s1" select="'My string'"/>

Simply use:

<xsl:value-of select="replace($s1, 'My', 'best')"/>

Note that a regular expression is applied. Meaning:

<xsl:value-of select="replace('test.replace', '.', ':')"/>

Becomes:

::::::::::::

Be sure to escape the characters that have special meaning to the regular expression interpreter:

<xsl:value-of select="replace('test.replace', '\.', '::')"/>

Becomes:

test::replace
2

First check, if your xslt processor (saxxon) is the latest release. Then you have to set <xsl:stylesheet version="2.0" in the head of your xslt-stylesheet. That's it. Your code was fine, besides you forgot the apostrophs:

<xsl:value-of select="replace( 'my string',my,best)"/>

must be

<xsl:value-of select="replace('my string','my','best')"/>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.