how to replace one double quotes with two double quotes in XSLT

How can I replace one double quote to a two double quote in a string in XSLT

I am passing the parameter string to XSLT template contains the value as

<xsl:variable name="Description">Hi! "How are you</xsl:variable>

<xsl:variable name="VQuotes">""</xsl:variable>

I nead the output as

Hi! ""How are you.

Tried with Translate function, but it did not work out

<xsl:element name="DESCRIPTION_SHORT">
          <xsl:value-of select="translate($Description,'&quot;', VQuotes)" />
        </xsl:element>But it is giving the same result as Hi! "How are you

When I tried with

<xsl:element name="DESCRIPTION_SHORT">
          <xsl:value-of select="translate($Description,'&quot;', 'BB')" />
        </xsl:element>

It gave the result as

Hi! BHow are you.

It is replacing only one character with one. how to make it for two characters.

Am I doing anything wrong in syntax?

Please help.

February 5th, 2014 8:52am

Hi, maybe you can try this template:

http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx

/Ola

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2014 10:16am

translate() only replaces a single character based on the relative position in the second and third argument so that won't work for the single->multiple scenario.

Agreed, if you have to do it in XSLT, this sample: http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx

However, if this is for a BizTalk Map, you might find it easier to format your string before the template in a C# Functoid.

February 5th, 2014 10:56am

Hi Vignesh,

Try this.

Its a two step process:

Step1: Add the following template would be "called" to do the replacement as your want:

  <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>

Step2: Call the above templeate in the place where you want to call, like this:

<!--Define the variables-->
 <xsl:variable name="Description">Hi! "How are you</xsl:variable>
    <xsl:variable name="sQuotes">"</xsl:variable>
    <xsl:variable name="VQuotes">""</xsl:variable>

<!--Following call the template which you have defined in step1-->

   <xsl:element name="DESCRIPTION_SHORT">
        <xsl:variable name="myVar">
          <xsl:call-template name="string-replace-all">
            <xsl:with-param name="text" select="$Description" />
            <xsl:with-param name="replace" select="$sQuotes" />
            <xsl:with-param name="by" select="$VQuotes" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$myVar" />
      </xsl:element>
I have tested this and works. And outputs as the following with two-double quote as you want.

<DESCRIPTION_SHORT>Hi! ""How are you</DESCRIPTION_SHORT>


Free Windows Admin Tool Kit Click here and download it now
February 5th, 2014 11:36am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics