Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace string in xslt 2.0 with replace function

Tags:

xslt

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

like image 330
None Avatar asked Nov 04 '13 11:11

None


People also ask

How do you replace in XSLT?

XSLT replace is deterministic and does string manipulation that replaces a sequence of characters defined inside a string that matches an expression. In simple terms, it does string substitution in the specified place by replacing any substrings. Fn: replace function is not available in XSLT1.

Is XSLT 2.0 backward compatibility?

Backwards CompatibilityThe XSLT 2.0 engine is backwards compatible.

What is text () in XSLT?

XSLT <xsl:text> The <xsl:text> element is used to write literal text to the output. Tip: This element may contain literal text, entity references, and #PCDATA.

What is number () in XSLT?

Definition and Usage. The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number.


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
like image 107
Martin Honnen Avatar answered Oct 17 '22 03:10

Martin Honnen


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')"/>
like image 37
michael Avatar answered Oct 17 '22 03:10

michael