Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Bruno Alexandre 29 posts 38 karma points
    Jul 09, 2011 @ 20:33
    Bruno Alexandre
    0

    How to convert Richtext Editor into a string?

    I'm trying to convert a Richtext Editor content into a JSON string, my code for this is

    description: '<xsl:value-of select="htmlContent" disable-output-escaping="yes"/>'

    but the output I get is not quite what I wanted, I'm having this:

    description: '
    <p>This is my 1st line content</p>
    '

    Witch breaks the JSON string. I even tried replacing New Lines with

    <xsl:value-of select="Exslt.ExsltStrings:replace($varDescription, '\n', '&lt;br/&gt;')" disable-output-escaping="yes"/>

    But I got the same output :(

    Is there a way to simplify this simple task? How should I accomplish this?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Jul 10, 2011 @ 01:17
    Chriztian Steinmeier
    1

    Hi Bruno,

    There's a couple of things you can do to better control this - first of all, if you have something like this:

    <xsl:template match="/">
        description: '<xsl:value-of select="htmlContent" />'
    </xsl:template>
    
    - the template element is said to contain "mixed content", and all the whitespace before "description" and after the last apostrophe is called "significant whitespace" and will be output.
    If you do this instead:
    <xsl:template match="/">
        <xsl:text>description: '</xsl:text>
        <xsl:value-of select="htmlContent" disable-output-escaping="yes" />
        <xsl:text>'</xsl:text>
    </xsl:template>
    
    You have better control over the output, 'cause then there's no additional whitespace.
    Second, in XML all linebreaks are normalized into the character "&#10;", so your replace statement should look like this instead:
    <xsl:value-of select="Exslt.ExsltStrings:replace($varDescription, '&#10;', '&lt;br/&gt;')" />
    - but if that's not really what you want (if it's HTML already, you probably don't want extra hard breaks?), you can try the normalize-space() function:
    <xsl:value-of select="normalize-space(htmlContent)" disable-output-escaping="yes" />
    - which will strip all leading and trailing whitespace and collapse runs of whitespace between other characters into a single space.
    Try them out and see what fits you,

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft