[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: JSON-encoding strings in XSLT 2.0

Subject: Re: JSON-encoding strings in XSLT 2.0
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Tue, 29 Oct 2013 11:46:12 +0000
Re:  JSON-encoding strings in XSLT 2.0
On 29 Oct 2013, at 11:02, Martynas JuseviD
ius <martynas@xxxxxxxxxxxx> wrote:

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

Well, I'm not going to check that the list of characters to be escaped is
complete, but you've got the right idea. I would code it like this for
readability:

>  <xsl:template match="text()" mode="json-identity">
       <xsl:variable name="v" select="."/>
       <xsl:variable name="v" select="replace($v, '\\', '\\\\')"/>
       <xsl:variable name="v" select="replace($v, '&quot;', '\\&quot;')"/>
       ...
       <xsl:value-of select="$v"/>
</xsl:variable>

or in 3.0 you can use the "!" operator for function chaining:

> <xsl:template match="text()" mode="json-identity">
       <xsl:value-of select="replace(., '\\', '\\\\') ! replace(., '&quot;',
'\\&quot;') ! ....."/>
</xsl:variable>

Michael Kay
Saxonica



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

>
> On Tue, Oct 29, 2013 at 10:37 AM, Michael Kay <mike@xxxxxxxxxxxx> 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 JuseviD
ius <martynas@xxxxxxxxxxxx>
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.