Concat adds double quotes

2.3k Views Asked by At

concat adds another double quote if an input string includes one. I am using XQuery 3.1 on eXide. A̶F̶A̶I̶K̶,̶ ̶e̶X̶i̶d̶e̶ ̶u̶s̶e̶s̶ ̶S̶a̶x̶o̶n̶ ̶a̶s̶ ̶X̶Q̶u̶e̶r̶y̶ ̶e̶n̶g̶i̶n̶e̶.̶ (it doesn't, see joewiz answer below).
The bug occurs when I evaluate concatenated strings.

return concat("'", 'bar') evaluates to 'bar which is expected.

return concat('"', 'bar') evaluates to ""bar.

How comes? I thought there was no difference between single and double quotes in xquery.

Here's my script:

xquery version "3.1";
let $c := concat('"','car')
return $c
2

There are 2 best solutions below

0
On BEST ANSWER

By default, eXide serializes query results using the standard adaptive output method. Since the concat() function returns a string, adaptive output wraps the strings in double quotes and escapes any double quotes within the string by doubling them. This explains the phenomenon you are seeing.

From the W3C spec linked above:

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.

If you would instead like to see your results without any such quote escaping, you can use the dropdown menu just above eXide's query output pane and select "Text Method" or "XML Method" instead.

eXide's documentation (viewable via Help > Documentation) explains its serialization feature and default setting as follows:

In 2.4.0, eXide jettisoned its longstanding “pretty-printing” library, now using eXist’s built-in serialization methods. This change means improved whitespace accuracy and speed when viewing your query results, and turns eXide into a serialization sandbox. By toggling the output methods via the “Output” dropdown menu, you can serialize query results not just as Adaptive, JSON, XML, or the old “direct” (rendered) method, but also as Text, HTML5, XHTML, XHTML5, and MicroXML. The new “Indent” checkbox lets you toggle whether query results are indented or not.

Finally, I should note that eXist has its own native XQuery engine, and eXide passes queries directly to eXist for execution, not to Saxon. eXist uses Saxon solely for XSLT - i.e., when you use the transform module to invoke an XSLT from XQuery.

2
On

That's not true universally. There is no difference in how your XQuery will be evaluated using either quote character as a string boundary. However, the characters within a quoted string expression are evaluated literally. In your example you had to use the other quote character inside the string because it won't parse (""" or ''') otherwise. But you can still use whichever literal quote character you want by escaping the quote matching the one used to bound your string expression. You can do that by using two of them consecutively:

concat('''', 'bar'), concat("""", 'bar')
=>
'bar
"bar

For improved readability you can also use the corresponding entities, &quote for " and ' for '. These rules are explained in the spec here: https://www.w3.org/TR/xquery-30/#doc-xquery30-EscapeQuot