none
Replace " with " in a string using XSLT , XPath 1.0

    Question

  • As the title says, it must be compliant with XPath 1.0, so as far as I can understand, I must write a recursive template
    I've googling for hours, to find a recursive XSLT template to replace the " with ", but ended up writing my own, since I couldn't find anything that worked for me.

    I've put this together:

    1 <xsl:template name="SpringReplaceQuotation">  
    2     <xsl:param name="text" />   
    3     <xsl:choose>                         
    4         <xsl:when test="contains($text, '&quot;')">  
    5             <xsl:variable name="nextString">                                      
    6                 <xsl:call-template name="SpringReplaceQuotation">                                       
    7                     <xsl:with-param name="text" select="substring-after($text, '&quot;')" />                                       
    8                 </xsl:call-template>                                
    9             </xsl:variable>                                
    10             <xsl:value-of select="concat(substring-before($text, '&quot;'), '\"' ,$nextString)" /> 
    11         </xsl:when>                         
    12         <xsl:otherwise>                                
    13             <xsl:value-of select="$text" />                         
    14         </xsl:otherwise>                         
    15     </xsl:choose>    
    16 </xsl:template> 
    17

    1. problem:
    When I using contains in line 4 it can't check on '&quot;', which means it never gets into the when-clause.

    2. problem:
    I get an error in line 10, when using '\"', and can't figure out how to concat an " to my string.

    I hope that someone who isn't a XSLT newbie like me, can help me out. Thanks! :)
    • Edited by Jason.Smith Friday, June 26, 2009 6:12 PM fixing double encoded title
    Monday, February 23, 2009 4:29 PM

Answers

  • It is not clear what you want to achieve. Your subject says ' Replace &quot; with " ' which does not make it clear to me as in XML &quot; is an entity reference which is exactly representing the double quote character. And replacing " with " does not make sense.
    Please post the XML input you have and the transformation result you want your stylesheet or at least your template to create.
    As for existing solutions, check the XSLT FAQ.
    As for your error on line 10, try

    <xsl:value-of select="concat(substring-before($text, '&quot;'), '\&quot;' ,$nextString)" />  

    That would then replace a double quote with a backslash and a double quote.

    MVP XML
    Monday, February 23, 2009 5:00 PM

All replies

  • It is not clear what you want to achieve. Your subject says ' Replace &quot; with " ' which does not make it clear to me as in XML &quot; is an entity reference which is exactly representing the double quote character. And replacing " with " does not make sense.
    Please post the XML input you have and the transformation result you want your stylesheet or at least your template to create.
    As for existing solutions, check the XSLT FAQ.
    As for your error on line 10, try

    <xsl:value-of select="concat(substring-before($text, '&quot;'), '\&quot;' ,$nextString)" />  

    That would then replace a double quote with a backslash and a double quote.

    MVP XML
    Monday, February 23, 2009 5:00 PM
  • I'm using it for a custom Content Query Webpart for MOSS 2007. I'm displaying the latest item in a list with a picture, the title, and a preview of the body-text.
    I've customised a standard template in ItemStyle.xsl, and it works like a charm.

    But it can't display the RichHTML format that the body-text is in, so I've also got a template that removes all HTML markup, so I only got a simple string.
    But that string still contains &quot; and MOSS won't display it as a normal double quotation mark, so I just wan't to replace the &quot; with " in the string.

    I'll try out that escape sign, thanks!
    Tuesday, February 24, 2009 8:46 AM
  • For Content Query Webparts in MOSS 2007 use:

    <xsl:template name="escapeQuot">
        <xsl:param name="text"/>
        <xsl:choose>
          <xsl:when test="contains($text, '&amp;quot;')">
            <xsl:variable name="bufferBefore" select="substring-before($text,'&amp;quot;')"/>
            <xsl:variable name="newBuffer" select="substring-after($text,'&amp;quot;')"/>
            <xsl:value-of select="$bufferBefore"/><xsl:text>&quot;</xsl:text>
            <xsl:call-template name="escapeQuot">
              <xsl:with-param name="text" select="$newBuffer"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$text"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    • Proposed as answer by gollum27 Wednesday, April 01, 2009 8:47 AM
    Wednesday, April 01, 2009 8:47 AM