Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT string replace

I don't really know XSL but I need to fix this code, I have reduced it to make it simpler.
I am getting this error

Invalid XSLT/XPath function

on this line

<xsl:variable name="text" select="replace($text,'a','b')"/> 

This is the XSL

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:inm="http://www.inmagic.com/webpublisher/query" version="1.0">     <xsl:output method="text" encoding="UTF-8" />      <xsl:preserve-space elements="*" />     <xsl:template match="text()" />      <xsl:template match="mos">         <xsl:apply-templates />          <xsl:for-each select="mosObj">           'Notes or subject'             <xsl:call-template                 name="rem-html">                 <xsl:with-param name="text" select="SBS_ABSTRACT" />             </xsl:call-template>         </xsl:for-each>     </xsl:template>      <xsl:template name="rem-html">         <xsl:param name="text" />         <xsl:variable name="text" select="replace($text, 'a', 'b')" />     </xsl:template> </xsl:stylesheet> 

Can anyone tell me what's wrong with it?

like image 434
Aximili Avatar asked Jun 18 '10 03:06

Aximili


People also ask

How do I replace text in XSLT?

With the help of the XSLT2. 0 replace () function removes undesired characters from a string in powerful regular expressions. XSLT replaces corresponding single characters, not the entire string. The dollar sign ($) interprets the rightmost characters in the string as literal.

What is translation in XSLT?

translate( ) is a versatile string function that is often used to compensate for missing string-processing capabilities in XSLT. Here you use the fact that translate( ) will not copy characters in the input string that are in the from string but do not have a corresponding character in the to string.

What is substring after in XSLT?

Definition of XSLT substring-after Function. The substring-after function returns a part out of the string declared in the string argument that is specified after the substring. If a second string is empty it returns an empty string as a result. String functions determine the manipulation of strings of text.

How do I concatenate in XSLT?

Concat function in XSLT helps to concatenate two strings into single strings. The function Concat() takes any arguments that are not the string is been converted to strings. The cardinality function passed over here in each argument should satisfy zero or one function() respectively.


2 Answers

replace isn't available for XSLT 1.0.

Codesling has a template for string-replace you can use as a substitute for the function:

<xsl:template name="string-replace-all">     <xsl:param name="text" />     <xsl:param name="replace" />     <xsl:param name="by" />     <xsl:choose>         <xsl:when test="$text = '' or $replace = ''or not($replace)" >             <!-- Prevent this routine from hanging -->             <xsl:value-of select="$text" />         </xsl:when>         <xsl:when test="contains($text, $replace)">             <xsl:value-of select="substring-before($text,$replace)" />             <xsl:value-of select="$by" />             <xsl:call-template name="string-replace-all">                 <xsl:with-param name="text" select="substring-after($text,$replace)" />                 <xsl:with-param name="replace" select="$replace" />                 <xsl:with-param name="by" select="$by" />             </xsl:call-template>         </xsl:when>         <xsl:otherwise>             <xsl:value-of select="$text" />         </xsl:otherwise>     </xsl:choose> </xsl:template> 

invoked as:

<xsl:variable name="newtext">     <xsl:call-template name="string-replace-all">         <xsl:with-param name="text" select="$text" />         <xsl:with-param name="replace" select="a" />         <xsl:with-param name="by" select="b" />     </xsl:call-template> </xsl:variable> 

On the other hand, if you literally only need to replace one character with another, you can call translate which has a similar signature. Something like this should work fine:

<xsl:variable name="newtext" select="translate($text,'a','b')"/> 

Also, note, in this example, I changed the variable name to "newtext", in XSLT variables are immutable, so you can't do the equivalent of $foo = $foo like you had in your original code.

like image 116
Mark Elliot Avatar answered Oct 02 '22 11:10

Mark Elliot


Here is the XSLT function which will work similar to the String.Replace() function of C#.

This template has the 3 Parameters as below

text :- your main string

replace :- the string which you want to replace

by :- the string which will reply by new string

Below are the Template

<xsl:template name="string-replace-all">   <xsl:param name="text" />   <xsl:param name="replace" />   <xsl:param name="by" />   <xsl:choose>     <xsl:when test="contains($text, $replace)">       <xsl:value-of select="substring-before($text,$replace)" />       <xsl:value-of select="$by" />       <xsl:call-template name="string-replace-all">         <xsl:with-param name="text" select="substring-after($text,$replace)" />         <xsl:with-param name="replace" select="$replace" />         <xsl:with-param name="by" select="$by" />       </xsl:call-template>     </xsl:when>     <xsl:otherwise>       <xsl:value-of select="$text" />     </xsl:otherwise>   </xsl:choose> </xsl:template> 

Below sample shows how to call it

<xsl:variable name="myVariable ">   <xsl:call-template name="string-replace-all">     <xsl:with-param name="text" select="'This is a {old} text'" />     <xsl:with-param name="replace" select="'{old}'" />     <xsl:with-param name="by" select="'New'" />   </xsl:call-template> </xsl:variable> 

You can also refer the below URL for the details.

like image 37
Optimus Avatar answered Oct 02 '22 11:10

Optimus