Linda O'Connel in

How to replace "single quote" with "double single quote" in XSLT

How to replace xml value like:

<name>Linda O'Connel</name>

      

in

<name>Linda O''Connel</name>

      

through XSLT?

I need this because I need to pass this value on the powershell command line and to other platforms as it requires a "double single quote" to escape the apostrophe / single quotes.

+3


source to share


2 answers


Suppose for XSLT 1.0 you need a recursive name pattern, for example:

<xsl:template name="replace">
    <xsl:param name="text"/>
    <xsl:param name="searchString">'</xsl:param>
    <xsl:param name="replaceString">''</xsl:param>
    <xsl:choose>
        <xsl:when test="contains($text,$searchString)">
            <xsl:value-of select="substring-before($text,$searchString)"/>
            <xsl:value-of select="$replaceString"/>
           <!--  recursive call -->
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="substring-after($text,$searchString)"/>
                <xsl:with-param name="searchString" select="$searchString"/>
                <xsl:with-param name="replaceString" select="$replaceString"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

      



Call example:

<xsl:template match="name">
    <xsl:copy>
        <xsl:call-template name="replace">
            <xsl:with-param name="text" select="."/>
        </xsl:call-template>
    </xsl:copy>
</xsl:template>

      

+8


source


You can also try the following.



  <xsl:variable name="temp">'</xsl:variable>
  <name>
    <xsl:value-of select="concat(substring-before(name,$temp),$temp,$temp,substring-after(name,$temp))"/>
  </name>

      

-2


source







All Articles