xsl-list
[Top] [All Lists]

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

2019-10-24 03:12:28
Hi Tim, Hi Michael, 

Thanks for your answers, links and explanations.

I applied Martin’s 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 “complex” 
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 ☺
Except it applies to the whole json : I can’t say this key value has to be 
solidus escaped and this one not (I could maybe work within the map for this)

There’s 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

De : Michael Kay mike(_at_)saxonica(_dot_)com 
[mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com] 
Envoyé : jeudi 24 octobre 2019 07:15
À : xsl-list <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Objet : 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-escaped

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(_at_)gmail(_dot_)com 
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

Hi, Matthieu,

See Martin Honnen's answer here:

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

Updated example: 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(_dot_)ricaud-dussarget(_at_)lefebvre-sarrut(_dot_)eu 
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:
Hi all, 
 
I need to generate this json output :
 
{"test":"foo/bar"}
 
I use XSLT3 function fn:xml-to-json to do this. 
My input is : 
 
<fn:map xmlns:fn="http://www.w3.org/2005/xpath-functions";>
    <fn:string key="test" escaped="false">foo/bar</fn:string>
</fn:map>
 
But I always get this result with escaped slash :
 
{"test":"foo\/bar"}
 
Reading the spec at https://www.w3.org/TR/xslt-30/#func-xml-to-json at 
paragraph “Strings are escaped as follows:” it looks like I can’t unescape “/” 
(solidus?) character.
 
Is there really no way to do that using XSLT3  json functionality ? Did I 
missed something obvious ?
 
Here is a running test : https://xsltfiddle.liberty-development.net/bwdwrV
 
Thanks in advance, 
 
Cordialement,
<image002.jpg>Matthieu RICAUD-DUSSARGET
Expert XML
SI EDITORIAL
Tél. +33 6 63 25 95 58
80, avenue de la Marne 92120 Montrouge
 
 
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 () 
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--

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