Bug #5387
closedAdaptive serialization only escapes the first quotation mark in a string
100%
Description
The command xslt3ee -xp:codepoints-to-string((34,34))
produces the output
"""""
(five double quotation marks).
The result of the expression itself appears to be correct (wrapping a call of string-to-codepoints() around the expression delivers a result of 34 34
) but the way the result is displayed is rather strange.
Updated by Michael Kay almost 3 years ago
The code for the adaptive serialization method (Serialize.js line 1887) is doing
w.write('"' + applyCharMap(item.toString().replace(/"/, '""')) + '"');
The spec says: *An instance of xs:string, xs:untypedAtomic or xs:anyURI is serialized by enclosing the value in double quotation marks and doubling any quotes within the value; or optionally by enclosing the value in apostrophes and doubling any apostrophes within the value. *
The code is incorrect because replace() only replaces the first instance. If we fix it to do a global replace, we get a correct answer of six double-quotation marks (equally unreadable, but correct). But a smarter solution is to use single quotes as the delimiter if the string contains double quotes.
The use case in question is actually displaying a string that contains JSON. This is probably not uncommon. Since JSON is likely to contain double-quotes, it's worth the effort to use single-quotes as the delimiter in this case.
Updated by Michael Kay almost 3 years ago
Added QT3 test case Serialization-adaptive-85
Updated by Michael Kay almost 3 years ago
- Status changed from New to Resolved
Fixed:
(a) the replace is now global
(b) we use single quotes as delimiter if the string contains double-quotes
Updated by Michael Kay almost 3 years ago
- Subject changed from Strange display of "" in command line output to Adaptive serialization only escapes the first quotation mark in a string
- Applies to JS Branch 2, Trunk added
- Fix Committed on JS Branch 2, Trunk added
Updated by Debbie Lockett over 2 years ago
- Status changed from Resolved to Closed
- % Done changed from 0 to 100
- Fixed in JS Release set to SaxonJS 2.4
Bug fix applied in the SaxonJS 2.4 maintenance release.
Please register to edit this issue
Also available in: Atom PDF Tracking page