Support #5225
closedWhile using xslt 3.0 function "xml-to-json" would like to avoid escaping '/'
0%
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?
Updated by Michael Kay almost 3 years 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.
Updated by Juhi Gupta almost 3 years ago
Ah, okay! We can maybe try using replace() and check if its meeting our use case! Thankyou for your quick reply and help!
Updated by Michael Kay over 2 years 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.
Updated by Michael Kay over 2 years ago
Updated by Juhi Gupta over 2 years ago
Ah! that is an interesting find, will also subscribe the git hub issue to see the updates! Thankyou so much for the findings!
Updated by Michael Kay over 2 years 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.
Updated by Michael Kay over 2 years 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.
Updated by Norm Tovey-Walsh over 2 years 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.
Updated by Norm Tovey-Walsh about 2 years 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.
Updated by Norm Tovey-Walsh 4 months ago
- Status changed from New to Resolved
- Applies to JS Branch 2 added
- Fix Committed on JS Branch Trunk added
Now that SaxonJ supports the escape-solidus
option, it seems practical to add it here as well. I've added support for the escape-solidus
option to the serialize()
function (when using method="json"
) and the xml-to-json
function.
See also #6488
Please register to edit this issue
Also available in: Atom PDF Tracking page