4

How can I use & (the ampersand character) in an XQuery concat statement? I'm using eXist DB and this works:

concat("Marvin ", "and", " Peter")

but this doesn't:

concat("Marvin ", "&", " Peter")

I'm getting the error: expecting '"', found '&'

Escaping the ampersand with a \ doesn't work.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
karkraeg
  • 445
  • 4
  • 18
  • 1
    Funnily enough the notification mail from SO gives the solution away. Your question is shown as: `How can I use & (the ampersand character) in an Xquery concat statement? I'm using eXist DB and this works: concat("Marvin ", "and", " Peter") but this doesn't: concat("Marvin ", "&", " ... ` – Leo Wörteler Feb 15 '19 at 16:44

1 Answers1

6

Since & is the escape character for XML character and entity references, it cannot be used as a literal character in either XML or XQuery strings. You have to use an entity to encode it. You can either use the predefined &, or reference it via its Unicode codepoint with &#[...]; (decimal) or &#x[...]; (hexadecimal):

concat("Marvin ", "&", " Peter"),
concat("Marvin ", "&", " Peter"),
concat("Marvin ", "&", " Peter")
Leo Wörteler
  • 4,191
  • 13
  • 10