Re: [xsl] How to unescape slash character using xml-to-json ?

Subject: Re: [xsl] How to unescape slash character using xml-to-json ?
From: "Matthieu RICAUD-DUSSARGET m.ricaud-dussarget@xxxxxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 24 Oct 2019 08:12:22 -0000
Hi Tim, Hi Michael,

Thanks for your answers, links and explanations.

I applied Martinbs solution as suggested:

xml-to-json(.) => parse-json() => serialize(map {'method': 'json',
'use-character-maps' : map {'/' : '/'}})

Parsing the json to serialize it back with character-maps looks quite
bcomplexb for such a serialization option.
I understand the debate on this subject. Maybe having an option to let the
user choosing could have been a good compromiss ?
Anyway this method works well b:
Except it applies to the whole json : I canbt say this key value has to be
solidus escaped and this one not (I could maybe work within the map for this)

Therebs also an unexpected side effect : the output json is not in the same
order.
I also loose the indent option (which I had to move to the serialize method)

For example, for this input :
<fn:map xmlns:fn="http://www.w3.org/2005/xpath-functions";>
   <fn:string key="format">text/xml</fn:string>
   <fn:string key="contentType">text</fn:string>
   <fn:string key="rights">public</fn:string>
   <fn:string key="source">flash</fn:string>
   <fn:array key="altKeys">
      <fn:string>flash:id:d28073e1005</fn:string>
   </fn:array>
</fn:map>>

When using: xslLib:xjson2json(*, map{'indent':true()})
I get this json output:


  { "format" : "text\/xml",
    "contentType" : "text",
    "rights" : "public",
    "source" : "flash",
    "altKeys" :
    [ "flash:id:d28073e1005" ] }

And when using:
xslLib:xjson2json(*) => parse-json() => serialize(map {'method': 'json',
'indent':true(), 'use-character-maps' : map {'/' : '/'}})
I get this json output:
{
  "contentType":"text",
  "rights":"public",
  "format":"text/xml",
  "source":"flash",
  "altKeys": [
    "flash:id:d28073e1005"
   ]
 }

Which is not a probem as json properties order doesn't matter, just wondering
why actually ?

Cheers

Matthieu Ricaud-Dussarget

DeB : Michael Kay mike@xxxxxxxxxxxx
[mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx]
EnvoyC)B : jeudi 24 octobre 2019 07:15
CB : xsl-list <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
ObjetB : Re: [xsl] How to unescape slash character using xml-to-json ?

Note: the reason that solidus is escaped by default is because the JSON can
then be embedded in an HTML5 script tag. See explanation here:

https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-esca
ped

There was considerable debate about this decision, as you can imagine: it's
one that suits some use cases and not others.

Michael Kay
Saxonica


On 24 Oct 2019, at 01:25, Tim Thompson mailto:timathom@xxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:

Hi, Matthieu,

SeeB Martin Honnen's answer here:

https://stackoverflow.com/questions/44610431/json-serialization-with-xpath-3-
1-fnserialize

Updated example:B https://xsltfiddle.liberty-development.net/bwdwrV/2


--
Tim A. Thompson
Discovery Metadata Librarian
Yale University Library

On Wed, Oct 23, 2019 at 6:59 PM Matthieu RICAUD-DUSSARGET
mailto:m.ricaud-dussarget@xxxxxxxxxxxxxxxxxx
<mailto:xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote:
Hi all,
B 
I need to generate this json output :
B 
{"test":"foo/bar"}
B 
I use XSLT3 function fn:xml-to-json to do this.
My input is :
B 
<fn:map xmlns:fn="http://www.w3.org/2005/xpath-functions";>
B B B  <fn:string key="test" escaped="false">foo/bar</fn:string>
</fn:map>
B 
But I always get this result with escaped slash :
B 
{"test":"foo\/bar"}
B 
Reading the spec at https://www.w3.org/TR/xslt-30/#func-xml-to-json at
paragraph bStrings are escaped as follows:b it looks like I canbt
unescape b/b (solidus?) character.
B 
Is there really no way to do that using XSLT3 B json functionality ? Did I
missed something obvious ?
B 
Here is a running test : https://xsltfiddle.liberty-development.net/bwdwrV
B 
Thanks in advance,
B 
Cordialement,
<image002.jpg>Matthieu RICAUD-DUSSARGET
Expert XML
SI EDITORIAL
TC)l. +33 6 63 25 95 58
80, avenue de la Marne 92120 Montrouge
B 
B 
http://www.mulberrytech.com/xsl/xsl-list
http://lists.mulberrytech.com/unsub/xsl-list/885568 (by email)
http://www.mulberrytech.com/xsl/xsl-list
http://lists.mulberrytech.com/unsub/xsl-list/293509 (by email)

http://www.mulberrytech.com/xsl/xsl-list
http://lists.mulberrytech.com/unsub/xsl-list/663667 ()

Current Thread