Adaptive serialization only escapes the first quotation mark in a string
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 over 1 year 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.
Please register to edit this issue