Project

Profile

Help

Support #5225

open

While using xslt 3.0 function "xml-to-json" would like to avoid escaping '/'

Added by Juhi Gupta about 1 year ago. Updated 7 months ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
-
Sprint/Milestone:
Start date:
2022-01-24
Due date:
% Done:

0%

Estimated time:
Applies to JS Branch:
Fix Committed on JS Branch:
Fixed in JS Release:
SEF Generated with:
Platforms:
Company:
-
Contact person:
-
Additional contact persons:
-

Description

We are trying to convert xml to json, while using function "xml-to-json" it is trying to escape '/' by adding '' before it. We would like to avoid doing that and translate as is.


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/2005/xpath-functions"
  xmlns:func="http://ingka.ikea.com/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
	version="3.0">


  <xsl:output method="text"/>

  <xsl:template match="/">
    <xsl:variable name="transformed-xml">      
      <xsl:call-template name="SamplBody" />
      <xsl:copy-of select="/map"/>
    </xsl:variable>
    <xsl:value-of select="xml-to-json($transformed-xml, map { 'indent' : true() })"/>   
  </xsl:template>

    <xsl:template name="SamplBody" >
    <map>
        <string key="SamplString">n/a</string>
    </map>
  </xsl:template>	
  
  </xsl:stylesheet>

and the result of this xslt is


  { "SamplString" : "n\/a" }

We would like the result to look like


  { "SamplString" : "n/a" }

Can you suggest a solution for this?

Actions #1

Updated by Michael Kay about 1 year ago

This escaping is required by the W3C spec, and any JSON consumer should accept the output in this form, but I agree it's not universally popular. (I think the W3C justification was that an unescaped "/" causes problems in HTML script elements, though I can't say I fully understand the problem).

In the case of JSON serialization it's possible to switch it off using the rather cumbersome device of a character map that maps "/" to "/". But that doesn't apply here (unless you parse the JSON produced by xml-to-json() and then re-serialise it.... )

Post-processing using replace() is of course possible, but I don't think there's any other workaround at the moment.

Actions #2

Updated by Juhi Gupta about 1 year ago

Ah, okay! We can maybe try using replace() and check if its meeting our use case! Thankyou for your quick reply and help!

Actions #3

Updated by Michael Kay 12 months ago

I've noticed an oddity in the rules for xml-to-json. It says

Strings are escaped as follows:

If the attribute escaped="true" is present for a string value, or escaped-key="true" for a key value, then:
...
any unescaped occurrence of quotation mark, backspace, form-feed, newline, carriage return, tab, or solidus is replaced by \", \b, \f, \n, \r, \t, or \/ respectively;
...
Otherwise (that is, in the absence of the attribute escaped="true" for a string value, or escaped-key="true" for a key value):
...
any occurrence of quotation mark, backspace, form-feed, newline, carriage return, or tab is replaced by \", \b, \f, \n, \r, or \t respectively;
...

Note that "/" is escaped in the first case but not the second. I find it hard to see any rationale for the difference, and I suspect it is an oversight.

Actions #5

Updated by Juhi Gupta 12 months ago

Ah! that is an interesting find, will also subscribe the git hub issue to see the updates! Thankyou so much for the findings!

Actions #6

Updated by Michael Kay 12 months ago

Under issue #4860 we added an option number-formatter : function(string) as string to control formatting of numbers by xml-to-json. (Though we added it on SaxonJ only, it's equally applicable to SaxonJS.) I feel the right approach here might be a similar callback string-formatter that puts string formatting under user control. The only slight complication is that it might need a second argument to indicate whether the string is already escaped.

Actions #9

Updated by Michael Kay 11 months ago

Having hit this myself yesterday, I'm now inclined to do it the easy way: just add an option escape-solidus=true|false.

Note that all the other characters we escape are characters that MUST be escaped to make it valid JSON.

Actions #10

Updated by Norm Tovey-Walsh 11 months ago

Two things I observed when investigating this. First, if you put a literal bit of JSON in a select statement, the compiler does the escaping so the runtime option won't have any effect. Second, there seem to be a lot of places where this escaping occurs. I failed to find all of them, I'm sure.

Actions #12

Updated by Norm Tovey-Walsh 7 months ago

  • Sprint/Milestone set to SaxonJS 3.0

This will require changes in the XJ compiler at least, so it's unlikely before 3.0.

Please register to edit this issue

Also available in: Atom PDF Tracking page