How to replace “single quote” to “double single quote” in XSLT

前端 未结 2 1522
星月不相逢
星月不相逢 2020-11-29 13:36

How do you replace an xml value, for example:

Linda O\'Connel

to:

<
相关标签:
2条回答
  • 2020-11-29 14:13

    Assuming an XSLT 1.0 processor, you will need to use a recursive named template for this, e.g:

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

    Example of call:

    <xsl:template match="name">
        <xsl:copy>
            <xsl:call-template name="replace">
                <xsl:with-param name="text" select="."/>
            </xsl:call-template>
        </xsl:copy>
    </xsl:template>
    
    0 讨论(0)
  • 2020-11-29 14:20

    You can also try out 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>
    
    0 讨论(0)
提交回复
热议问题