xsl-list
[Top] [All Lists]

Re: [xsl] JSON-encoding strings in XSLT 2.0

2013-10-29 06:03:09
Thanks Michael. I was looking at http://json.org and here's what I came up with:

    <xsl:template match="text()" mode="json-identity">
        <xsl:value-of
select="replace(replace(replace(replace(replace(replace(., '\\',
'\\\\'), '''', '\\'''), '&quot;', '\\&quot;'), '&#09;', '\\t'),
'&#10;', '\\n'), '&#13;', '\\r')"/>
    </xsl:template>

Can this be improved?

On Tue, Oct 29, 2013 at 10:37 AM, Michael Kay <mike(_at_)saxonica(_dot_)com> 
wrote:

There's no built-in function for the job, but picking out the characters =
that need special treatment (e.g. replacing newline by "\n") isn't =
difficult. Handling astral characters is a bit tricky because JSON =
requires them to be represented as a surrogate pair, but again the logic =
for that isn't really difficult.

Michael Kay
Saxonica

On 29 Oct 2013, at 00:56, Martynas Jusevičius 
<martynas(_at_)graphity(_dot_)org> wrote:

Hey,

is there some way in XSLT 2.0 to encode strings for use in JSON? In my
case, the stylesheet has to encode all text nodes in a XHTML fragment
which then gets passed to WYSIWYM editor constructor. Could this be
done as identity transform?

I had solved this problem when I used XSLT 1.0 on PHP by calling
json_encode() as extension function, but now I'm in the Java world.
http://php.net/manual/en/function.json-encode.php

Martynas
graphityhq.com

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


<Prev in Thread] Current Thread [Next in Thread>